Click here to Skip to main content
13,803,513 members
Click here to Skip to main content

Stats

3M views
20.1K downloads
995 bookmarked
Posted 19 Jan 2003
Licenced MIT

SourceGrid - Open Source C# Grid Control

, 4 Aug 2013
SourceGrid is a free open source grid control. Supports virtual grid, custom cells and editors, advanced formatting options and many others features
SourceGrid2
References
SourceLibrary.dll
vssver.scc
SampleProject
App.ico
bin
Release
SampleProject.exe
SourceGrid2.dll
SourceLibrary.dll
Extensions
Add.ico
ArrowDown.ICO
ArrowUp.ICO
Delete.ICO
Refresh.ico
vssver.scc
Samples
CalcioSmall.bmp
FACE00.ICO
FACE01.ICO
FACE02.ICO
FACE04.ICO
new.bmp
vssver.scc
SourceGrid
BehaviorModels
vssver.scc
bin
Release
SourceGrid2.dll
SourceLibrary.dll
vssver.scc
Cells
vssver.scc
Common
Icons
CheckBoxChecked.ico
CheckBoxCheckedDisable.ico
CheckBoxCheckedSel.ico
CheckBoxUnChecked.ico
CheckBoxUnCheckedDisable.ico
CheckBoxUnCheckedSel.ico
clear.ico
copy.ico
cut.ico
DeleteCol.ico
DeleteRow.ico
InsertCol.ico
InsertRow.ico
paste.ico
properties.ico
SortDown.ico
SortUp.ico
Thumbs.db
vssver.scc
vssver.scc
DataModels
vssver.scc
Grid.bmp
Grids
vssver.scc
GridVirtual.bmp
SourceGrid2.snk
VisualModels
vssver.scc
SourceGrid
DemoProject
App.ico
bin
Release
DemoProject.exe
SourceGrid.dll
SourceLibrary.dll
FACE00.ICO
FACE01.ICO
FACE02.ICO
FACE04.ICO
new.bmp
PropertyStyleGrid
WorksheetStyleGrid
References
SourceLibrary.dll
SourceGrid
bin
Release
SourceGrid.dll
Cells
Models
PropertyStyleGrid
SourceGrid.snk
TypeCreator
Utility
Chris.Beckett.MenuImageLib
SubItem16.ico
Control
Controls
Icons
CheckBoxChecked.ico
CheckBoxCheckedDisable.ico
CheckBoxCheckedSel.ico
CheckBoxUnChecked.ico
CheckBoxUnCheckedDisable.ico
CheckBoxUnCheckedSel.ico
clear.ico
copy.ico
cut.ico
DeleteCol.ico
DeleteRow.ico
InsertCol.ico
InsertRow.ico
paste.ico
properties.ico
SortDown.ico
SortUp.ico
WorksheetStyleGrid
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace SourceGrid
{
	/// <summary>
	/// Class to manage the visual aspect of a cell. This class can be shared beetween multiple cells.
	/// </summary>
	[Serializable]
	public class CellVisualProperties : ICloneable
	{
		/// <summary>
		/// Rappresent a default CellVisualProperties
		/// </summary>
		public readonly static CellVisualProperties Default = new CellVisualProperties(true);

		private bool m_bIsReadOnly;

		#region Constructors

		/// <summary>
		/// Use default setting and construct a read and write CellVisualProperties
		/// </summary>
		public CellVisualProperties():this(false)
		{
		}

		/// <summary>
		/// Use default setting
		/// </summary>
		/// <param name="p_bReadOnly"></param>
		public CellVisualProperties(bool p_bReadOnly)
		{
			m_bIsReadOnly = p_bReadOnly;

			//Default Properties

			m_Cursor = null;

			m_BackColor = Color.FromKnownColor(KnownColor.Window);
			m_ForeColor = Color.FromKnownColor(KnownColor.WindowText);
			m_SelectionBackColor = Color.FromKnownColor(KnownColor.Highlight);
			m_SelectionForeColor = Color.FromKnownColor(KnownColor.HighlightText);
			m_FocusBackColor = ControlPaint.LightLight(m_SelectionBackColor);
			m_FocusForeColor = m_ForeColor;

			m_Font = null; // new Font(FontFamily.GenericSansSerif,8.25f); if null the grid font is used
			m_Image = null;
			m_ImageAlignment = ContentAlignment.MiddleLeft;
			m_imgStretch = false;
			m_AlignTextToImage = true;

			m_StringFormat = (StringFormat)StringFormat.GenericDefault.Clone();
			//change the default text alignment
			TextAlignment = ContentAlignment.MiddleLeft;

			m_EditableMode = EditableMode.F2Key|EditableMode.DoubleClick;

			//Border
			m_Border = new CellBorder( new Border(Color.LightGray,1), new Border(Color.LightGray,1) );
			m_FocusBorder = m_Border;
			m_SelectionBorder = m_Border;

//			m_FocusBorder = new Border(Color.FromKnownColor(KnownColor.Highlight),2);
//			m_BottomBorder = new Border(Color.LightGray,1);
//			m_TopBorder = new Border(Color.LightGray,1);
//			m_LeftBorder = new Border(Color.LightGray,1);
//			m_RightBorder = new Border(Color.LightGray,1);
		}


		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="p_bReadOnly"></param>
		/// <param name="p_BackColor"></param>
		/// <param name="p_ForeColor"></param>
		/// <param name="p_SelectionBackColor"></param>
		/// <param name="p_SelectionForeColor"></param>
		/// <param name="p_FocusBackColor"></param>
		/// <param name="p_FocusForeColor"></param>
		/// <param name="p_Font"></param>
		/// <param name="p_StringFormat"></param>
		/// <param name="p_Image"></param>
		/// <param name="p_ImageAlignment"></param>
		/// <param name="p_bImgStretch"></param>
		/// <param name="p_bAlignTextToImage"></param>
		/// <param name="p_Cursor"></param>
		/// <param name="p_EditableMode"></param>
		/// <param name="p_Border"></param>
		/// <param name="p_FocusBorder"></param>
		/// <param name="p_SelectionBorder"></param>
		public CellVisualProperties(bool p_bReadOnly,
			Color p_BackColor, 
			Color p_ForeColor,
			Color p_SelectionBackColor,
			Color p_SelectionForeColor,
			Color p_FocusBackColor,
			Color p_FocusForeColor,
			Font p_Font,
			StringFormat p_StringFormat,
			Image p_Image,
			ContentAlignment p_ImageAlignment,
			bool p_bImgStretch,
			bool p_bAlignTextToImage,
			Cursor p_Cursor,
			EditableMode p_EditableMode,
			CellBorder p_Border,
			CellBorder p_FocusBorder,
			CellBorder p_SelectionBorder)
		{
			m_bIsReadOnly = p_bReadOnly;

			m_BackColor = p_BackColor;
			m_Cursor = p_Cursor;
			m_FocusBorder = p_FocusBorder;
			m_Font = p_Font;
			m_ForeColor = p_ForeColor;
			m_Image = p_Image;
			m_ImageAlignment = p_ImageAlignment;
			m_imgStretch = p_bImgStretch;
			m_AlignTextToImage = p_bAlignTextToImage;
			m_EditableMode = p_EditableMode;
			m_SelectionBackColor = p_SelectionBackColor;
			m_SelectionForeColor = p_SelectionForeColor;
			m_FocusBackColor = p_FocusBackColor;
			m_FocusForeColor = p_FocusForeColor;
			m_StringFormat = p_StringFormat;
			m_Border = p_Border;
			m_FocusBorder = p_FocusBorder;
			m_SelectionBorder = p_SelectionBorder;
		}
		#endregion

		#region Events
		public event EventHandler Change;
		protected virtual void OnChange()
		{
			if (Change!=null)
				Change(this,EventArgs.Empty);
		}
		#endregion

		#region Format
		private StringFormat m_StringFormat = StringFormat.GenericDefault;
		[System.ComponentModel.Browsable(false)]
		public StringFormat StringFormat
		{
			get{return m_StringFormat;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_StringFormat = value;
				OnChange();
			}
		}

		private Font m_Font;

		/// <summary>
		/// If null the grid font is used
		/// </summary>
		public Font Font
		{
			get{return m_Font;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_Font = value;
				OnChange();
			}
		}
		#region BackColor/ForeColor
		private Color m_BackColor; 
		public Color BackColor
		{
			get{return m_BackColor;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_BackColor = value;
				OnChange();
			}
		}
		private Color m_ForeColor; 
		public Color ForeColor
		{
			get{return m_ForeColor;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_ForeColor = value;
				OnChange();
			}
		}

		private Color m_SelectionForeColor; 
		public Color SelectionForeColor
		{
			get{return m_SelectionForeColor;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_SelectionForeColor = value;
				OnChange();
			}
		}

		private Color m_SelectionBackColor; 
		public Color SelectionBackColor
		{
			get{return m_SelectionBackColor;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_SelectionBackColor = value;
				OnChange();
			}
		}


		private Color m_FocusForeColor; 
		public Color FocusForeColor
		{
			get{return m_FocusForeColor;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_FocusForeColor = value;
				OnChange();
			}
		}

		private Color m_FocusBackColor; 
		public Color FocusBackColor
		{
			get{return m_FocusBackColor;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_FocusBackColor = value;
				OnChange();
			}
		}
		#endregion

		private Cursor m_Cursor = null;
		public Cursor Cursor
		{
			get{return m_Cursor;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_Cursor = value;
				OnChange();
			}
		}

		private Image m_Image = null;
		public Image Image
		{
			get{return m_Image;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_Image = value;
				OnChange();
			}
		}
		private bool m_imgStretch = false;
		/// <summary>
		/// True to stretch the image otherwise false
		/// </summary>
		public bool ImageStretch
		{
			get{return m_imgStretch;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_imgStretch = value;
				OnChange();
			}
		}

		private bool m_AlignTextToImage = true;
		/// <summary>
		/// True to align the text with the image.
		/// </summary>
		public bool AlignTextToImage
		{
			get{return m_AlignTextToImage;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_AlignTextToImage = value;
				OnChange();
			}
		}

		private ContentAlignment m_ImageAlignment = ContentAlignment.MiddleLeft;
		/// <summary>
		/// Image Alignment
		/// </summary>
		public ContentAlignment ImageAlignment
		{
			get{return m_ImageAlignment;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_ImageAlignment = value;
				OnChange();
			}
		}

		private EditableMode m_EditableMode = EditableMode.F2Key|EditableMode.DoubleClick;
		/// <summary>
		/// Editable Mode
		/// </summary>
		public EditableMode EditableMode
		{
			get{return m_EditableMode;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_EditableMode = value;
				OnChange();
			}
		}

		#region Border

		private CellBorder m_Border;
		/// <summary>
		/// The normal border of a cell
		/// </summary>
		public CellBorder Border
		{
			get{return m_Border;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_Border = value;
				OnChange();
			}
		}

		private CellBorder m_FocusBorder;
		/// <summary>
		/// The border of a cell when have the focus
		/// </summary>
		public CellBorder FocusBorder
		{
			get{return m_FocusBorder;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_FocusBorder = value;
				OnChange();
			}
		}

		private CellBorder m_SelectionBorder;
		/// <summary>
		/// The border of the cell when is selected
		/// </summary>
		public CellBorder SelectionBorder
		{
			get{return m_SelectionBorder;}
			set
			{
				if (m_bIsReadOnly)
					throw new ObjectIsReadOnlyException("CellVisualProperties is readonly.");
				m_SelectionBorder = value;
				OnChange();
			}
		}
		#endregion
		#endregion


		#region Clone
		public object Clone()
		{
			return Clone(m_bIsReadOnly);
		}
		public object Clone(bool p_bReadOnly)
		{
			Image l_tmpImage = null;
			if (m_Image!=null)
				l_tmpImage = Utility.ImageClone(m_Image);
			Font l_tmpFont = null;
			if (m_Font!=null)
				l_tmpFont = (Font)m_Font.Clone();
			StringFormat l_tmpStringFormat = null;
			if (m_StringFormat!=null)
				l_tmpStringFormat = (StringFormat)m_StringFormat.Clone();

			return new CellVisualProperties(p_bReadOnly,m_BackColor,m_ForeColor,m_SelectionBackColor,m_SelectionForeColor, m_FocusBackColor, m_FocusForeColor,l_tmpFont,l_tmpStringFormat,l_tmpImage,m_ImageAlignment,m_imgStretch,m_AlignTextToImage,m_Cursor,m_EditableMode,m_Border,m_FocusBorder,m_SelectionBorder);
		}
		#endregion

		#region Utility
		/// <summary>
		/// Word Wrap.  This property is only a wrapper around StringFormat
		/// </summary>
		public bool WordWrap
		{
			get
			{
				if ( (StringFormat.FormatFlags & StringFormatFlags.NoWrap) == StringFormatFlags.NoWrap)
					return false;
				else
					return true;
			}
			set
			{
				if (value && WordWrap == false)
					StringFormat.FormatFlags = StringFormat.FormatFlags ^ StringFormatFlags.NoWrap;
				else if (value == false)
					StringFormat.FormatFlags = StringFormat.FormatFlags | StringFormatFlags.NoWrap;
			}
		}

		/// <summary>
		/// Text Alignment. This property is only a wrapper around StringFormat
		/// </summary>
		public ContentAlignment TextAlignment
		{
			get
			{
				if (Utility.IsBottom(StringFormat) && Utility.IsLeft(StringFormat))
					return ContentAlignment.BottomLeft;
				else if (Utility.IsBottom(StringFormat) && Utility.IsRight(StringFormat))
					return ContentAlignment.BottomRight;
				else if (Utility.IsBottom(StringFormat) && Utility.IsCenter(StringFormat))
					return ContentAlignment.BottomCenter;

				else if (Utility.IsTop(StringFormat) && Utility.IsLeft(StringFormat))
					return ContentAlignment.TopLeft;
				else if (Utility.IsTop(StringFormat) && Utility.IsRight(StringFormat))
					return ContentAlignment.TopRight;
				else if (Utility.IsTop(StringFormat) && Utility.IsCenter(StringFormat))
					return ContentAlignment.TopCenter;

				else if (Utility.IsMiddle(StringFormat) && Utility.IsLeft(StringFormat))
					return ContentAlignment.MiddleLeft;
				else if (Utility.IsMiddle(StringFormat) && Utility.IsRight(StringFormat))
					return ContentAlignment.MiddleRight;
				else //if (Utility.IsMiddle(StringFormat) && Utility.IsCenter(StringFormat))
					return ContentAlignment.MiddleCenter;
			}

			set
			{
				if (Utility.IsBottom(value))
					StringFormat.LineAlignment = StringAlignment.Far;
				else if (Utility.IsTop(value))
					StringFormat.LineAlignment = StringAlignment.Near;
				else //if (Utility.IsMiddle(value))
					StringFormat.LineAlignment = StringAlignment.Center;

				if (Utility.IsLeft(value))
					StringFormat.Alignment = StringAlignment.Near;
				else if (Utility.IsRight(value))
					StringFormat.Alignment = StringAlignment.Far;
				else //if (Utility.IsCenter(value))
					StringFormat.Alignment = StringAlignment.Center;
			}
		}
		#endregion
	}


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Davide Icardi
Software Developer
Italy Italy
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181215.1 | Last Updated 4 Aug 2013
Article Copyright 2003 by Davide Icardi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid