/// <summary> /// Gets the image to display in the cell /// </summary> /// <param name="value">The value to be use in determining the image</param> /// <param name="rowIndex">The index of the cell's parent row</param> /// <returns>The image that should be displayed in the cell</returns> protected override object GetCellImage(object value, int rowIndex) { IndicatorColumn owner = base.OwningColumn as IndicatorColumn; if (owner != null && owner.ImageList != null) { return(owner.DrawImage(value, rowIndex)); } return(null); }
/// <summary> /// Change the mouse pointer to a hand when the mouse moves over one of the images /// </summary> /// <param name="e">The event arguments</param> protected override void OnMouseMove(DataGridViewCellMouseEventArgs e) { IndicatorColumn owner = base.OwningColumn as IndicatorColumn; Rectangle content = base.GetContentBounds(e.RowIndex); Cursor newCursor = null; int index, width, spacing, lastMouseIndex = this.MouseImageIndex; base.OnMouseMove(e); this.MouseImageIndex = -1; // Figure out if the mouse is actually over an image if (owner != null && owner.ImageList != null) { newCursor = owner.OriginalCursor; if (content.Contains(e.Location)) { width = owner.ImageList.ImageSize.Width; spacing = owner.ImageSpacing; index = (e.Location.X - content.X) / (width + spacing); if (e.Location.X - content.X - (index * (width + spacing)) < width) { newCursor = Cursors.Hand; this.MouseImageIndex = index; } } } // Update the cursor and the cell tool tip if necessary if (base.DataGridView != null) { if (this.IsClickable && newCursor != null && base.DataGridView.Cursor != newCursor) { base.DataGridView.Cursor = newCursor; } if (this.MouseImageIndex != lastMouseIndex) { if (this.MouseImageIndex != -1) { DataGridViewHelper.ActivateToolTip(base.DataGridView, true, base.ToolTipText, e.ColumnIndex, e.RowIndex); } else { DataGridViewHelper.ActivateToolTip(base.DataGridView, false, null, e.ColumnIndex, e.RowIndex); } } } }
//===================================================================== /// <summary> /// This is used to clone the column /// </summary> /// <returns>A clone of the column</returns> public override object Clone() { IndicatorColumn clone = (IndicatorColumn)base.Clone(); clone.ImageList = images; clone.ImageSpacing = this.ImageSpacing; clone.IsBitFlags = this.IsBitFlags; clone.IsClickable = this.IsClickable; return(clone); }
/// <summary> /// Change the mouse cursor back to the default when leaving the cell /// </summary> /// <param name="rowIndex">The row index of the cell</param> protected override void OnMouseLeave(int rowIndex) { IndicatorColumn owner = base.OwningColumn as IndicatorColumn; if (this.IsClickable && owner != null && base.DataGridView.Cursor != owner.OriginalCursor) { base.DataGridView.Cursor = owner.OriginalCursor; } this.MouseImageIndex = -1; base.OnMouseLeave(rowIndex); }
/// <summary> /// This is overridden to raise the <see cref="IndicatorColumn.IndicatorClicked"/> event when an image /// is clicked. The event is not raised if <see cref="IsClickable"/> is set to false. /// </summary> /// <param name="e">The event arguments</param> protected override void OnContentClick(DataGridViewCellEventArgs e) { IndicatorColumn owner = base.OwningColumn as IndicatorColumn; if (this.IsClickable && base.DataGridView != null && owner != null && this.MouseImageIndex != -1 && this.MouseImageIndex < owner.ImageList.Images.Count) { IndicatorClickEventArgs clickArgs = new IndicatorClickEventArgs(e.ColumnIndex, e.RowIndex, this.MouseImageIndex, base.NewValue); owner.OnIndicatorClicked(clickArgs); if (!base.ReadOnly && ((clickArgs.Value == null && base.NewValue != null) || (clickArgs.Value != null && !clickArgs.Value.Equals(base.NewValue)))) { base.NewValue = clickArgs.Value; base.DataGridView.NotifyCurrentCellDirty(true); base.DataGridView.InvalidateCell(this); } } }
/// <summary> /// Calculates the preferred size, in pixels, of the cell /// </summary> /// <param name="graphics">The graphics context for the cell</param> /// <param name="cellStyle">The cell style</param> /// <param name="rowIndex">The index of the cell's parent row</param> /// <param name="constraintSize">The cell's maximum allowable size</param> /// <returns>The preferred cell size in pixels</returns> protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize) { Size size; int widthOffset, heightOffset, freeDimension; if (base.DataGridView == null || base.OwningColumn == null) { return(new Size(-1, -1)); } if (cellStyle == null) { throw new ArgumentNullException("cellStyle"); } if (constraintSize.Width == 0) { if (constraintSize.Height == 0) { freeDimension = 0; // Both free } else { freeDimension = 2; // Width is free } } else { freeDimension = 1; // Height is free } DataGridViewAdvancedBorderStyle borderStylePlaceholder = new DataGridViewAdvancedBorderStyle(); DataGridViewAdvancedBorderStyle advancedBorderStyle = base.AdjustCellBorderStyle( base.DataGridView.AdvancedCellBorderStyle, borderStylePlaceholder, false, false, false, false); Rectangle borderWidths = base.BorderWidths(advancedBorderStyle); widthOffset = (borderWidths.Left + borderWidths.Width) + cellStyle.Padding.Horizontal; heightOffset = (borderWidths.Top + borderWidths.Height) + cellStyle.Padding.Vertical; IndicatorColumn owner = base.OwningColumn as IndicatorColumn; if (owner.ImageList == null) { size = new Size(16, 16); } else { size = new Size((owner.ImageList.ImageSize.Width + owner.ImageSpacing) * owner.ImageList.Images.Count, owner.ImageList.ImageSize.Height); } switch (freeDimension) { case 1: size.Width = 0; break; case 2: size.Height = 0; break; } if (freeDimension != 1) { size.Width += widthOffset + owner.ImageSpacing; if (base.DataGridView.ShowCellErrors) { size.Width = Math.Max(size.Width, widthOffset + 16); } } if (freeDimension != 2) { size.Height += heightOffset; if (base.DataGridView.ShowCellErrors) { size.Height = Math.Max(size.Height, heightOffset + 16); } } return(size); }