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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Jordi Mas i Hernandez <jordi@ximian.com>
25 // Datagrid drawing logic
31 using System.Drawing.Drawing2D;
33 namespace System.Windows.Forms
35 internal class DataGridDrawing
37 #region Local Variables
39 private DataGrid grid;
42 internal Rectangle caption_area;
43 internal Rectangle parent_rows;
44 internal Rectangle columnshdrs_area; // Used columns header area
45 internal int columnshdrs_maxwidth; // Total width (max width) for columns headrs
46 internal Rectangle rowshdrs_area; // Used Headers rows area
47 internal int rowshdrs_maxheight; // Total height for rows (max height)
48 internal Rectangle cells_area;
49 internal Font font_newrow = new Font (FontFamily.GenericSansSerif, 16);
50 #endregion // Local Variables
53 public DataGridDrawing (DataGrid datagrid)
58 #region Public Instance Methods
60 // Calc the max with of all columns
61 internal int CalcAllColumnsWidth ()
64 int cnt = grid.CurrentTableStyle.GridColumnStyles.Count;
66 for (int col = 0; col < cnt; col++) {
67 width += grid.CurrentTableStyle.GridColumnStyles[col].Width;
73 // Gets a column from a pixel
74 public int FromPixelToColumn (int pixel)
77 int cnt = grid.CurrentTableStyle.GridColumnStyles.Count;
82 if (grid.CurrentTableStyle.CurrentRowHeadersVisible)
83 width += rowshdrs_area.X + rowshdrs_area.Width;
85 for (int col = 0; col < cnt; col++) {
86 width += grid.CurrentTableStyle.GridColumnStyles[col].Width;
96 public int GetColumnStartingPixel (int my_col)
99 int cnt = grid.CurrentTableStyle.GridColumnStyles.Count;
101 for (int col = 0; col < cnt; col++) {
106 width += grid.CurrentTableStyle.GridColumnStyles[col].Width;
112 // Which column has to be the first visible column to ensure a column visibility
113 public int GetFirstColumnForColumnVisilibility (int current_first_visiblecolumn, int column)
115 int new_col = column;
118 if (column > current_first_visiblecolumn) { // Going forward
119 for (new_col = column; new_col >= 0; new_col--){
120 width += grid.CurrentTableStyle.GridColumnStyles[new_col].Width;
122 if (width >= cells_area.Width)
124 //return new_col < grid.CurrentTableStyle.GridColumnStyles.Count ? new_col + 1 : grid.CurrentTableStyle.GridColumnStyles.Count;
132 public void CalcGridAreas ()
134 if (grid.IsHandleCreated == false) // Delay calculations until the handle is created
137 /* Order is important. E.g. row headers max. height depends on caption */
138 grid.horz_pixeloffset = 0;
142 CalcColumnsHeader ();
145 UpdateVisibleRowCount (); // need it to be able to calcultate the need of horz scrollbar
146 if (SetUpVerticalScrollBar ()) { // We need a Vertical ScrollBar
148 if (grid.ShowParentRowsVisible) {
149 parent_rows.Width -= grid.vert_scrollbar.Width;
152 if (grid.columnheaders_visible) {
153 if (columnshdrs_area.X + columnshdrs_area.Width > grid.vert_scrollbar.Location.X) {
154 columnshdrs_area.Width -= grid.vert_scrollbar.Width;
158 if (cells_area.X + cells_area.Width >= grid.vert_scrollbar.Location.X) {
159 cells_area.Width -= grid.vert_scrollbar.Width;
163 if (SetUpHorizontalScrollBar ()) { // We need a Horizontal ScrollBar
164 cells_area.Height -= grid.horiz_scrollbar.Height;
166 if (rowshdrs_area.Y + rowshdrs_area.Height > grid.ClientRectangle.Y + grid.ClientRectangle.Height) {
167 rowshdrs_area.Height -= grid.horiz_scrollbar.Height;
168 rowshdrs_maxheight -= grid.horiz_scrollbar.Height;
172 // Reajust scrollbars to avoid overlapping at the corners
173 if (grid.vert_scrollbar.Visible && grid.horiz_scrollbar.Visible) {
174 grid.horiz_scrollbar.Width -= grid.vert_scrollbar.Width;
175 grid.vert_scrollbar.Height -= grid.horiz_scrollbar.Height;
178 UpdateVisibleColumn ();
179 UpdateVisibleRowCount ();
181 //Console.WriteLine ("DataGridDrawing.CalcGridAreas cells:{0}", cells_area);
184 public void CalcCaption ()
186 if (grid.caption_visible == false) {
187 caption_area = Rectangle.Empty;
191 caption_area.X = grid.ClientRectangle.X;
192 caption_area.Y = grid.ClientRectangle.Y;
193 caption_area.Width = grid.ClientRectangle.Width;
194 caption_area.Height = grid.CaptionFont.Height + 6;
196 //Console.WriteLine ("DataGridDrawing.CalcCaption {0}", caption_area);
199 public void CalcCellsArea ()
201 if (grid.caption_visible) {
202 cells_area.Y = caption_area.Y + caption_area.Height;
204 cells_area.Y = grid.ClientRectangle.Y;
207 if (grid.ShowParentRowsVisible) {
208 cells_area.Y += parent_rows.Height;
211 if (grid.columnheaders_visible) {
212 cells_area.Y += columnshdrs_area.Height;
215 cells_area.X = grid.ClientRectangle.X + rowshdrs_area.Width;
216 cells_area.Width = grid.ClientRectangle.X + grid.ClientRectangle.Width - cells_area.X;
217 cells_area.Height = grid.ClientRectangle.Y + grid.ClientRectangle.Height - cells_area.Y;
219 //Console.WriteLine ("DataGridDrawing.CalcCellsArea {0}", cells_area);
222 public void CalcColumnsHeader ()
224 int width_all_cols, max_width_cols;
226 if (grid.columnheaders_visible == false) {
227 columnshdrs_area = Rectangle.Empty;
231 if (grid.caption_visible) {
232 columnshdrs_area.Y = caption_area.Y + caption_area.Height;
234 columnshdrs_area.Y = grid.ClientRectangle.Y;
237 if (grid.ShowParentRowsVisible) {
238 columnshdrs_area.Y += parent_rows.Height;
241 columnshdrs_area.X = grid.ClientRectangle.X;
242 columnshdrs_area.Height = ColumnsHeaderHeight;
243 width_all_cols = CalcAllColumnsWidth ();
245 // TODO: take into account Scrollbars
246 columnshdrs_maxwidth = grid.ClientRectangle.X + grid.ClientRectangle.Width - columnshdrs_area.X;
247 max_width_cols = columnshdrs_maxwidth;
249 if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
250 max_width_cols -= grid.RowHeaderWidth;
253 if (width_all_cols > max_width_cols) {
254 columnshdrs_area.Width = columnshdrs_maxwidth;
256 columnshdrs_area.Width = width_all_cols;
258 if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
259 columnshdrs_area.Width += grid.RowHeaderWidth;
263 //Console.WriteLine ("DataGridDrawing.CalcColumnsHeader {0}", columnshdrs_area);
266 public void CalcParentRows ()
268 if (grid.ShowParentRowsVisible == false) {
269 parent_rows = Rectangle.Empty;
273 if (grid.caption_visible) {
274 parent_rows.Y = caption_area.Y + caption_area.Height;
277 parent_rows.Y = grid.ClientRectangle.Y;
280 parent_rows.X = grid.ClientRectangle.X;
281 parent_rows.Width = grid.ClientRectangle.Width;
282 parent_rows.Height = grid.CaptionFont.Height + 3;
284 //Console.WriteLine ("DataGridDrawing.CalcParentRows {0}", parent_rows);
287 public void CalcRowsHeaders ()
289 if (grid.CurrentTableStyle.CurrentRowHeadersVisible == false) {
290 rowshdrs_area = Rectangle.Empty;
294 if (grid.caption_visible) {
295 rowshdrs_area.Y = caption_area.Y + caption_area.Height;
297 rowshdrs_area.Y = grid.ClientRectangle.Y;
300 if (grid.ShowParentRowsVisible) {
301 rowshdrs_area.Y += parent_rows.Height;
304 if (grid.columnheaders_visible) { // first block is painted by ColumnHeader
305 rowshdrs_area.Y += ColumnsHeaderHeight;
308 rowshdrs_area.X = grid.ClientRectangle.X;
309 rowshdrs_area.Width = grid.RowHeaderWidth;
310 rowshdrs_area.Height = grid.visiblerow_count * grid.RowHeight;
311 rowshdrs_maxheight = grid.ClientRectangle.Height + grid.ClientRectangle.Y - rowshdrs_area.Y;
313 //Console.WriteLine ("DataGridDrawing.CalcRowsHeaders {0} {1}", rowshdrs_area,
314 // rowshdrs_maxheight);
317 public void UpdateVisibleColumn ()
319 if (grid.CurrentTableStyle.GridColumnStyles.Count == 0) {
320 grid.visiblecolumn_count = 0;
325 int max_pixel = grid.horz_pixeloffset + cells_area.Width;
326 grid.first_visiblecolumn = FromPixelToColumn (grid.horz_pixeloffset);
328 col = FromPixelToColumn (max_pixel);
330 grid.visiblecolumn_count = 1 + col - grid.first_visiblecolumn;
332 if (grid.first_visiblecolumn + grid.visiblecolumn_count + 1 < grid.CurrentTableStyle.GridColumnStyles.Count) {
333 grid.visiblecolumn_count++; // Partially visible column
337 public void UpdateVisibleRowCount ()
339 int max_height = cells_area.Height;
340 int total_rows = grid.RowsCount;
342 if (grid.ShowEditRow) {
346 int rows_height = (total_rows - grid.first_visiblerow) * grid.RowHeight;
347 int max_rows = max_height / grid.RowHeight;
349 //Console.WriteLine ("UpdateVisibleRowCount {0} {1}/{2} (row h) {3}",
350 // max_rows, max_height, grid.RowHeight, cells_area.Height);
352 if (max_rows > total_rows) {
353 max_rows = total_rows;
356 if (rows_height > cells_area.Height) {
357 grid.visiblerow_count = max_rows;
359 grid.visiblerow_count = total_rows;
362 CalcRowsHeaders (); // Height depends on num of visible rows
364 if (grid.visiblerow_count + grid.first_visiblerow > total_rows)
365 grid.visiblerow_count = total_rows - grid.first_visiblerow;
367 if (grid.visiblerow_count < max_rows) {
368 grid.visiblerow_count = max_rows;
369 grid.first_visiblerow = total_rows - max_rows;
375 // From Point to Cell
376 public DataGrid.HitTestInfo HitTest (int x, int y)
378 DataGrid.HitTestInfo hit = new DataGrid.HitTestInfo ();
380 // TODO: Add missing ColumnResize and RowResize checks
381 if (columnshdrs_area.Contains (x, y)) {
382 hit.type = DataGrid.HitTestType.ColumnHeader;
383 hit.column = FromPixelToColumn (x + grid.horz_pixeloffset);
387 if (rowshdrs_area.Contains (x, y)) {
388 hit.type = DataGrid.HitTestType.RowHeader;
390 int rcnt = grid.FirstVisibleRow + grid.VisibleRowCount;
391 for (int r = grid.FirstVisibleRow; r < rcnt; r++) {
392 posy = cells_area.Y + ((r - grid.FirstVisibleRow) * grid.RowHeight);
393 if (y <= posy + grid.RowHeight) { // Found row
401 if (caption_area.Contains (x, y)) {
402 hit.type = DataGrid.HitTestType.Caption;
406 if (parent_rows.Contains (x, y)) {
407 hit.type = DataGrid.HitTestType.ParentRows;
411 int pos_y, pos_x, width;
412 int rowcnt = grid.FirstVisibleRow + grid.VisibleRowCount;
413 for (int row = grid.FirstVisibleRow; row < rowcnt; row++) {
414 pos_y = cells_area.Y + ((row - grid.FirstVisibleRow) * grid.RowHeight);
416 if (y <= pos_y + grid.RowHeight) { // Found row
418 hit.type = DataGrid.HitTestType.Cell;
420 int column_cnt = grid.first_visiblecolumn + grid.visiblecolumn_count;
421 for (int column = grid.first_visiblecolumn; column < column_cnt; column++) {
423 col_pixel = GetColumnStartingPixel (column);
424 pos_x = cells_area.X + col_pixel - grid.horz_pixeloffset;
425 width = grid.CurrentTableStyle.GridColumnStyles[column].Width;
427 if (x <= pos_x + width) { // Column found
440 public Rectangle GetCellBounds (int row, int col)
442 Rectangle bounds = new Rectangle ();
445 bounds.Width = grid.CurrentTableStyle.GridColumnStyles[col].Width;
446 bounds.Height = grid.RowHeight;
447 bounds.Y = cells_area.Y + ((row - grid.FirstVisibleRow) * grid.RowHeight);
448 col_pixel = GetColumnStartingPixel (col);
449 bounds.X = cells_area.X + col_pixel - grid.horz_pixeloffset;
453 public void InvalidateCaption ()
455 if (caption_area.IsEmpty)
458 grid.Invalidate (caption_area);
462 public void InvalidateRow (int row)
464 if (row < grid.FirstVisibleRow || row > grid.FirstVisibleRow + grid.VisibleRowCount) {
468 Rectangle rect_row = new Rectangle ();
470 rect_row.X = cells_area.X;
471 rect_row.Width = cells_area.Width;
472 rect_row.Height = grid.RowHeight;
473 rect_row.Y = cells_area.Y + ((row - grid.FirstVisibleRow) * grid.RowHeight);
474 grid.Invalidate (rect_row);
477 public void InvalidateRowHeader (int row)
479 Rectangle rect_rowhdr = new Rectangle ();
480 rect_rowhdr.X = rowshdrs_area.X;
481 rect_rowhdr.Width = rowshdrs_area.Width;
482 rect_rowhdr.Height = grid.RowHeight;
483 rect_rowhdr.Y = rowshdrs_area.Y + ((row - grid.FirstVisibleRow) * grid.RowHeight);
484 grid.Invalidate (rect_rowhdr);
487 public void InvalidateColumn (DataGridColumnStyle column)
489 Rectangle rect_col = new Rectangle ();
493 col = grid.CurrentTableStyle.GridColumnStyles.IndexOf (column);
499 rect_col.Width = column.Width;
500 col_pixel = GetColumnStartingPixel (col);
501 rect_col.X = cells_area.X + col_pixel - grid.horz_pixeloffset;
502 rect_col.Y = cells_area.Y;
503 rect_col.Height = cells_area.Height;
504 grid.Invalidate (rect_col);
507 // Return true if the scrollbar is needed
508 public bool SetUpHorizontalScrollBar ()
510 int width_all = CalcAllColumnsWidth ();
512 if (width_all <= cells_area.Width) {
513 grid.horiz_scrollbar.Visible = false;
514 grid.Controls.Remove (grid.horiz_scrollbar);
518 grid.horiz_scrollbar.Location = new Point (grid.ClientRectangle.X, grid.ClientRectangle.Y +
519 grid.ClientRectangle.Height - grid.horiz_scrollbar.Height);
521 grid.horiz_scrollbar.Size = new Size (grid.ClientRectangle.Width,
522 grid.horiz_scrollbar.Height);
524 grid.horiz_scrollbar.Maximum = width_all;// - cells_area.Width;
525 grid.horiz_scrollbar.LargeChange = cells_area.Width;
526 grid.Controls.Add (grid.horiz_scrollbar);
527 grid.horiz_scrollbar.Visible = true;
531 // Return true if the scrollbar is needed
532 public bool SetUpVerticalScrollBar ()
535 int allrows = grid.RowsCount;
537 if (grid.ShowEditRow) {
541 if (grid.visiblerow_count == allrows) {
542 grid.vert_scrollbar.Visible = false;
543 grid.Controls.Remove (grid.vert_scrollbar);
547 if (grid.caption_visible) {
548 y = grid.ClientRectangle.Y + caption_area.Height;
549 height = grid.ClientRectangle.Height - caption_area.Height;
551 y = grid.ClientRectangle.Y;
552 height = grid.ClientRectangle.Height;
555 grid.vert_scrollbar.Location = new Point (grid.ClientRectangle.X +
556 grid.ClientRectangle.Width - grid.vert_scrollbar.Width, y);
558 grid.vert_scrollbar.Size = new Size (grid.vert_scrollbar.Width,
561 grid.vert_scrollbar.Maximum = grid.RowsCount;
563 if (grid.ShowEditRow) {
564 grid.vert_scrollbar.Maximum++;
567 grid.vert_scrollbar.LargeChange = VLargeChange;
569 grid.Controls.Add (grid.vert_scrollbar);
570 grid.vert_scrollbar.Visible = true;
574 #endregion // Public Instance Methods
576 #region Instance Properties
577 public Rectangle CellsArea {
583 // Returns the ColumnsHeader area excluding the rectangle shared with RowsHeader
584 public Rectangle ColumnsHeadersArea {
586 Rectangle columns_area = columnshdrs_area;
588 if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
589 columns_area.X += grid.RowHeaderWidth;
590 columns_area.Width -= grid.RowHeaderWidth;
596 public int ColumnsHeaderHeight {
598 return grid.CurrentTableStyle.HeaderFont.Height + 6;
602 public Rectangle RowsHeadersArea {
604 return rowshdrs_area;
608 public int VLargeChange {
610 return cells_area.Height / grid.RowHeight;
614 #endregion Instance Properties