1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //	Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25 
26 using System.ComponentModel;
27 using System.Drawing;
28 
29 namespace System.Windows.Forms {
30 	public class DataGridViewHeaderCell : DataGridViewCell {
31 
32 		private ButtonState buttonState;
33 
DataGridViewHeaderCell()34 		public DataGridViewHeaderCell ()
35 		{
36 			buttonState = ButtonState.Normal;
37 		}
38 
39 		[Browsable (false)]
40 		public override bool Displayed {
41 			get { return base.Displayed; }
42 		}
43 
44 		public override Type FormattedValueType {
45 			get { return typeof(string); } //base.FormattedValueType; }
46 		}
47 
48 		[Browsable (false)]
49 		public override bool Frozen {
50 			get { return base.Frozen; }
51 		}
52 
53 		[Browsable (false)]
54 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
55 		public override bool ReadOnly {
56 			get { return base.ReadOnly; }
57 			set { base.ReadOnly = value; }
58 		}
59 
60 		[Browsable (false)]
61 		public override bool Resizable {
62 			get { return base.Resizable; }
63 		}
64 
65 		[Browsable (false)]
66 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
67 		public override bool Selected {
68 			get { return base.Selected; }
69 			set { base.Selected = value; }
70 		}
71 
72 		public override Type ValueType {
73 			get { return base.ValueType; }
74 			set { base.ValueType = value; }
75 		}
76 
77 		[Browsable (false)]
78 		public override bool Visible {
79 			get { return base.Visible; }
80 		}
81 
Clone()82 		public override object Clone ()
83 		{
84 			DataGridViewHeaderCell result = new DataGridViewHeaderCell();
85 			return result;
86 		}
87 
Dispose(bool disposing)88 		protected override void Dispose (bool disposing)
89 		{
90 		}
91 
GetInheritedContextMenuStrip(int rowIndex)92 		public override ContextMenuStrip GetInheritedContextMenuStrip (int rowIndex)
93 		{
94 			if (DataGridView == null)
95 				return null;
96 
97 			if (ContextMenuStrip != null)
98 				return ContextMenuStrip;
99 			if (DataGridView.ContextMenuStrip != null)
100 				return DataGridView.ContextMenuStrip;
101 
102 			return null;
103 		}
104 
GetInheritedState(int rowIndex)105 		public override DataGridViewElementStates GetInheritedState (int rowIndex)
106 		{
107 			DataGridViewElementStates result;
108 
109 			result = DataGridViewElementStates.ResizableSet | State;
110 
111 			return result;
112 		}
113 
ToString()114 		public override string ToString ()
115 		{
116 			return string.Format ("DataGridViewHeaderCell {{ ColumnIndex={0}, RowIndex={1} }}", ColumnIndex, RowIndex);
117 		}
118 
GetSize(int rowIndex)119 		protected override Size GetSize (int rowIndex)
120 		{
121 			if (DataGridView == null && rowIndex != -1)
122 				throw new ArgumentOutOfRangeException ("rowIndex");
123 			if (OwningColumn != null && rowIndex != -1)
124 				throw new ArgumentOutOfRangeException ("rowIndex");
125 			if (OwningRow != null && (rowIndex < 0 || rowIndex >= DataGridView.Rows.Count))
126 				throw new ArgumentOutOfRangeException ("rowIndex");
127 			if (OwningColumn == null && OwningRow == null && rowIndex != -1)
128 				throw new ArgumentOutOfRangeException ("rowIndex");
129 			if (OwningRow != null && OwningRow.Index != rowIndex)
130 				throw new ArgumentException ("rowIndex");
131 
132 			if (DataGridView == null)
133 				return new Size (-1, -1);
134 
135 			if (this is DataGridViewTopLeftHeaderCell)
136 				return new Size (DataGridView.RowHeadersWidth, DataGridView.ColumnHeadersHeight);
137 			if (this is DataGridViewColumnHeaderCell)
138 				return new Size (100, DataGridView.ColumnHeadersHeight);
139 			if (this is DataGridViewRowHeaderCell)
140 				return new Size (DataGridView.RowHeadersWidth, 22);
141 
142 			return Size.Empty;
143 		}
144 
GetValue(int rowIndex)145 		protected override object GetValue (int rowIndex)
146 		{
147 			return base.GetValue (rowIndex);
148 		}
149 
MouseDownUnsharesRow(DataGridViewCellMouseEventArgs e)150 		protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e)
151 		{
152 			if (DataGridView == null)
153 				return false;
154 
155 			if (e.Button == MouseButtons.Left && Application.RenderWithVisualStyles && DataGridView.EnableHeadersVisualStyles)
156 				return true;
157 
158 			return false;
159 		}
160 
MouseEnterUnsharesRow(int rowIndex)161 		protected override bool MouseEnterUnsharesRow (int rowIndex)
162 		{
163 			if (DataGridView == null)
164 				return false;
165 
166 			if (Application.RenderWithVisualStyles && DataGridView.EnableHeadersVisualStyles)
167 				return true;
168 
169 			return false;
170 		}
171 
MouseLeaveUnsharesRow(int rowIndex)172 		protected override bool MouseLeaveUnsharesRow (int rowIndex)
173 		{
174 			if (DataGridView == null)
175 				return false;
176 
177 			if (ButtonState != ButtonState.Normal && Application.RenderWithVisualStyles && DataGridView.EnableHeadersVisualStyles)
178 				return true;
179 
180 			return false;
181 		}
182 
MouseUpUnsharesRow(DataGridViewCellMouseEventArgs e)183 		protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)
184 		{
185 			if (DataGridView == null)
186 				return false;
187 
188 			if (e.Button == MouseButtons.Left && Application.RenderWithVisualStyles && DataGridView.EnableHeadersVisualStyles)
189 				return true;
190 
191 			return false;
192 		}
193 
OnMouseDown(DataGridViewCellMouseEventArgs e)194 		protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
195 		{
196 			base.OnMouseDown (e);
197 		}
198 
OnMouseEnter(int rowIndex)199 		protected override void OnMouseEnter (int rowIndex)
200 		{
201 			base.OnMouseEnter (rowIndex);
202 		}
203 
OnMouseLeave(int rowIndex)204 		protected override void OnMouseLeave (int rowIndex)
205 		{
206 			base.OnMouseLeave (rowIndex);
207 		}
208 
OnMouseUp(DataGridViewCellMouseEventArgs e)209 		protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
210 		{
211 			base.OnMouseUp (e);
212 		}
213 
Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)214 		protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
215 		{
216 			base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
217 		}
218 
219 		protected ButtonState ButtonState {
220 			get { return buttonState; }
221 		}
222 
223 	}
224 
225 }
226 
227