//######################################################################################### //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★ //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★ //######################################################################################### /* * Copyright ?2005, Mathew Hall * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ using System; using System.Drawing; using System.Windows.Forms; using XPTable.Editors; using XPTable.Events; using XPTable.Models; using XPTable.Themes; namespace XPTable.Renderers { /// /// Base class for Renderers that draw Cells /// public abstract class CellRenderer : Renderer, ICellRenderer { #region Class Data /// /// A string that specifies how a Cells contents are formatted /// private string format; /// /// The Brush used to draw disabled text /// private SolidBrush grayTextBrush; /// /// The amount of padding for the cell being rendered /// private CellPadding padding; #endregion #region Constructor /// /// Initializes a new instance of the CellRenderer class with default settings /// protected CellRenderer() : base() { this.format = ""; this.grayTextBrush = new SolidBrush(SystemColors.GrayText); this.padding = CellPadding.Empty; } #endregion #region Methods /// /// Releases the unmanaged resources used by the Renderer and /// optionally releases the managed resources /// public override void Dispose() { base.Dispose(); if (this.grayTextBrush != null) { this.grayTextBrush.Dispose(); this.grayTextBrush = null; } } /// /// Gets the renderer specific data used by the Renderer from /// the specified Cell /// /// The Cell to get the renderer data for /// The renderer data for the specified Cell protected object GetRendererData(Cell cell) { return cell.RendererData; } /// /// Sets the specified renderer specific data used by the Renderer for /// the specified Cell /// /// The Cell for which the data is to be stored /// The renderer specific data to be stored protected void SetRendererData(Cell cell, object value) { cell.RendererData = value; } #endregion #region Properties /// /// Overrides Renderer.ClientRectangle /// public override Rectangle ClientRectangle { get { Rectangle client = new Rectangle(this.Bounds.Location, this.Bounds.Size); // take borders into account client.Width -= Renderer.BorderWidth; client.Height -= Renderer.BorderWidth; // take cell padding into account client.X += this.Padding.Left + 1; client.Y += this.Padding.Top; client.Width -= this.Padding.Left + this.Padding.Right + 1; client.Height -= this.Padding.Top + this.Padding.Bottom; return client; } } /// /// Gets or sets the string that specifies how a Cells contents are formatted /// protected string Format { get { return this.format; } set { this.format = value; } } /// /// Gets the Brush used to draw disabled text /// protected Brush GrayTextBrush { get { return this.grayTextBrush; } } /// /// Gets or sets the amount of padding around the Cell being rendered /// protected CellPadding Padding { get { return this.padding; } set { this.padding = value; } } #endregion #region Events #region Focus /// /// Raises the GotFocus event /// /// A CellFocusEventArgs that contains the event data public virtual void OnGotFocus(CellFocusEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } e.Table.Invalidate(e.CellRect); } /// /// Raises the LostFocus event /// /// A CellFocusEventArgs that contains the event data public virtual void OnLostFocus(CellFocusEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } e.Table.Invalidate(e.CellRect); } #endregion #region Keys /// /// Raises the KeyDown event /// /// A CellKeyEventArgs that contains the event data public virtual void OnKeyDown(CellKeyEventArgs e) { } /// /// Raises the KeyUp event /// /// A CellKeyEventArgs that contains the event data public virtual void OnKeyUp(CellKeyEventArgs e) { } #endregion #region Mouse #region MouseEnter /// /// Raises the MouseEnter event /// /// A CellMouseEventArgs that contains the event data public virtual void OnMouseEnter(CellMouseEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } bool tooltipActive = e.Table.ToolTip.Active; if (tooltipActive) { e.Table.ToolTip.Active = false; } e.Table.ResetMouseEventArgs(); e.Table.ToolTip.SetToolTip(e.Table, e.Cell.ToolTipText); if (tooltipActive) { e.Table.ToolTip.Active = true; } } #endregion #region MouseLeave /// /// Raises the MouseLeave event /// /// A CellMouseEventArgs that contains the event data public virtual void OnMouseLeave(CellMouseEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } } #endregion #region MouseUp /// /// Raises the MouseUp event /// /// A CellMouseEventArgs that contains the event data public virtual void OnMouseUp(CellMouseEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } } #endregion #region MouseDown /// /// Raises the MouseDown event /// /// A CellMouseEventArgs that contains the event data public virtual void OnMouseDown(CellMouseEventArgs e) { if (!e.Table.Focused) { if (!(e.Table.IsEditing && e.Table.EditingCell == e.CellPos && e.Table.EditingCellEditor is IEditorUsesRendererButtons)) { e.Table.Focus(); } } this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } } #endregion #region MouseMove /// /// Raises the MouseMove event /// /// A CellMouseEventArgs that contains the event data public virtual void OnMouseMove(CellMouseEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } } #endregion #region Click /// /// Raises the Click event /// /// A CellMouseEventArgs that contains the event data public virtual void OnClick(CellMouseEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } if (e.Table.EditStartAction == EditStartAction.SingleClick && e.Table.IsCellEditable(e.CellPos)) { e.Table.EditCell(e.CellPos); } } /// /// Raises the DoubleClick event /// /// A CellMouseEventArgs that contains the event data public virtual void OnDoubleClick(CellMouseEventArgs e) { this.Bounds = e.CellRect; if (e.Cell == null) { this.Padding = CellPadding.Empty; } else { this.Padding = e.Cell.Padding; } if (e.Table.EditStartAction == EditStartAction.DoubleClick && e.Table.IsCellEditable(e.CellPos)) { e.Table.EditCell(e.CellPos); } } #endregion #endregion #region Paint /// /// Raises the PaintCell event /// /// A PaintCellEventArgs that contains the event data public virtual void OnPaintCell(PaintCellEventArgs e) { this.Bounds = e.CellRect; if (e.Cell != null) { this.Padding = e.Cell.Padding; this.Alignment = e.Table.ColumnModel.Columns[e.Column].Alignment; this.LineAlignment = e.Table.TableModel.Rows[e.Row].Alignment; this.Format = e.Table.ColumnModel.Columns[e.Column].Format; this.Font = e.Cell.Font; } else { this.Padding = CellPadding.Empty; this.Alignment = ColumnAlignment.Left; this.LineAlignment = RowAlignment.Center; this.Format = ""; this.Font = null; } // if the font is null, use the default font if (this.Font == null) { this.Font = Control.DefaultFont; } // paint the Cells background this.OnPaintBackground(e); // paint the Cells foreground this.OnPaint(e); } /// /// Raises the PaintBackground event /// /// A PaintCellEventArgs that contains the event data protected virtual void OnPaintBackground(PaintCellEventArgs e) { if (e.Selected && (!e.Table.HideSelection || (e.Table.HideSelection && (e.Table.Focused || e.Table.IsEditing)))) { if (e.Table.Focused || e.Table.IsEditing) { this.ForeColor = e.Table.SelectionForeColor; this.BackColor = e.Table.SelectionBackColor; } else { this.BackColor = e.Table.UnfocusedSelectionBackColor; this.ForeColor = e.Table.UnfocusedSelectionForeColor; } if (this.BackColor.A != 0) { e.Graphics.FillRectangle(this.BackBrush, e.CellRect); } } else { this.ForeColor = e.Cell != null ? e.Cell.ForeColor : Color.Black; if (!e.Sorted || (e.Sorted && e.Table.SortedColumnBackColor.A < 255)) { if (e.Cell != null) { if (e.Cell.BackColor.A < 255) { if (e.Row % 2 == 1) { if (e.Table.AlternatingRowColor.A != 0) { this.BackColor = e.Table.AlternatingRowColor; e.Graphics.FillRectangle(this.BackBrush, e.CellRect); } } this.BackColor = e.Cell.BackColor; if (e.Cell.BackColor.A != 0) { e.Graphics.FillRectangle(this.BackBrush, e.CellRect); } } else { this.BackColor = e.Cell.BackColor; if (e.Cell.BackColor.A != 0) { e.Graphics.FillRectangle(this.BackBrush, e.CellRect); } } } else { if (e.Row % 2 == 1) { if (e.Table.AlternatingRowColor.A != 0) { this.BackColor = e.Table.AlternatingRowColor; e.Graphics.FillRectangle(this.BackBrush, e.CellRect); } } } if (e.Sorted) { this.BackColor = e.Table.SortedColumnBackColor; if (e.Table.SortedColumnBackColor.A != 0) { e.Graphics.FillRectangle(this.BackBrush, e.CellRect); } } } else { this.BackColor = e.Table.SortedColumnBackColor; e.Graphics.FillRectangle(this.BackBrush, e.CellRect); } } } /// /// Raises the Paint event /// /// A PaintCellEventArgs that contains the event data protected virtual void OnPaint(PaintCellEventArgs e) { } /// /// Raises the PaintBorder event /// /// A PaintCellEventArgs that contains the event data /// The pen used to draw the border protected virtual void OnPaintBorder(PaintCellEventArgs e, Pen pen) { // bottom e.Graphics.DrawLine(pen, e.CellRect.Left, e.CellRect.Bottom, e.CellRect.Right, e.CellRect.Bottom); // right e.Graphics.DrawLine(pen, e.CellRect.Right, e.CellRect.Top, e.CellRect.Right, e.CellRect.Bottom); } #endregion #endregion } }