* TreeView.cs: Don't draw the selected node when we lose
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridDrawingLogic.cs
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 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //
25 // Datagrid drawing logic
26 //
27
28 // NOT COMPLETE
29
30 using System.Drawing;
31 using System.Drawing.Drawing2D;
32
33 namespace System.Windows.Forms
34 {
35         internal class DataGridDrawing
36         {
37                 #region Local Variables
38
39                 private DataGrid grid;
40
41                 // Areas
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
51
52
53                 public DataGridDrawing (DataGrid datagrid)
54                 {
55                          grid = datagrid;                        
56                 }
57
58                 #region Public Instance Methods
59
60                 // Calc the max with of all columns
61                 internal int CalcAllColumnsWidth ()
62                 {
63                         int width = 0;
64                         int cnt = grid.CurrentTableStyle.GridColumnStyles.Count;
65
66                         for (int col = 0; col < cnt; col++) {
67                                 width += grid.CurrentTableStyle.GridColumnStyles[col].Width;
68                         }
69
70                         return width;
71                 }
72
73                 // Gets a column from a pixel
74                 public int FromPixelToColumn (int pixel)
75                 {
76                         int width = 0;
77                         int cnt = grid.CurrentTableStyle.GridColumnStyles.Count;
78
79                         if (cnt == 0)
80                                 return 0;
81                                 
82                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible)
83                                 width += rowshdrs_area.X + rowshdrs_area.Width;
84
85                         for (int col = 0; col < cnt; col++) {
86                                 width += grid.CurrentTableStyle.GridColumnStyles[col].Width;
87
88                                 if (pixel < width)
89                                         return col;
90                         }
91
92                         return cnt - 1;
93                 }
94
95                 //
96                 public int GetColumnStartingPixel (int my_col)
97                 {
98                         int width = 0;
99                         int cnt = grid.CurrentTableStyle.GridColumnStyles.Count;
100
101                         for (int col = 0; col < cnt; col++) {
102
103                                 if (my_col == col)
104                                         return width;
105
106                                 width += grid.CurrentTableStyle.GridColumnStyles[col].Width;
107                         }
108
109                         return 0;
110                 }
111                 
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)
114                 {
115                         int new_col = column;
116                         int width = 0;
117                         
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;
121                                         
122                                         if (width >= cells_area.Width)
123                                                 return new_col + 1;
124                                                 //return new_col < grid.CurrentTableStyle.GridColumnStyles.Count ? new_col + 1 : grid.CurrentTableStyle.GridColumnStyles.Count;
125                                 }
126                                 return 0;
127                         } else {                                
128                                 return  column;
129                         }                       
130                 }
131
132                 public void CalcGridAreas ()
133                 {
134                         if (grid.IsHandleCreated == false) // Delay calculations until the handle is created
135                                 return;
136
137                         /* Order is important. E.g. row headers max. height depends on caption */
138                         grid.horz_pixeloffset = 0;                      
139                         CalcCaption ();
140                         CalcParentRows ();
141                         CalcRowsHeaders ();
142                         CalcColumnsHeader ();
143                         CalcCellsArea ();
144
145                         UpdateVisibleRowCount (); // need it to be able to calcultate the need of horz scrollbar
146                         if (SetUpVerticalScrollBar ()) { // We need a Vertical ScrollBar
147                                 
148                                 if (grid.ShowParentRowsVisible) {
149                                         parent_rows.Width -= grid.vert_scrollbar.Width;
150                                 }
151
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;
155                                         }
156                                 }
157
158                                 if (cells_area.X + cells_area.Width >= grid.vert_scrollbar.Location.X) {
159                                         cells_area.Width -= grid.vert_scrollbar.Width;
160                                 }
161                         }
162
163                         if (SetUpHorizontalScrollBar ()) { // We need a Horizontal ScrollBar
164                                 cells_area.Height -= grid.horiz_scrollbar.Height;
165
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;
169                                 }
170                         }
171
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;
176                         }
177
178                         UpdateVisibleColumn ();
179                         UpdateVisibleRowCount ();
180
181                         //Console.WriteLine ("DataGridDrawing.CalcGridAreas cells:{0}", cells_area);
182                 }
183
184                 public void CalcCaption ()
185                 {
186                         if (grid.caption_visible == false) {
187                                 caption_area = Rectangle.Empty;
188                                 return;
189                         }
190
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;
195
196                         //Console.WriteLine ("DataGridDrawing.CalcCaption {0}", caption_area);
197                 }
198
199                 public void CalcCellsArea ()
200                 {
201                         if (grid.caption_visible) {
202                                 cells_area.Y = caption_area.Y + caption_area.Height;
203                         } else {
204                                 cells_area.Y = grid.ClientRectangle.Y;
205                         }
206
207                         if (grid.ShowParentRowsVisible) {
208                                 cells_area.Y += parent_rows.Height;
209                         }
210
211                         if (grid.columnheaders_visible) {
212                                 cells_area.Y += columnshdrs_area.Height;
213                         }
214
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;
218
219                         //Console.WriteLine ("DataGridDrawing.CalcCellsArea {0}", cells_area);
220                 }
221
222                 public void CalcColumnsHeader ()
223                 {
224                         int width_all_cols, max_width_cols;
225
226                         if (grid.columnheaders_visible == false) {
227                                 columnshdrs_area = Rectangle.Empty;
228                                 return;
229                         }
230
231                         if (grid.caption_visible) {
232                                 columnshdrs_area.Y = caption_area.Y + caption_area.Height;
233                         } else {
234                                 columnshdrs_area.Y = grid.ClientRectangle.Y;
235                         }
236
237                         if (grid.ShowParentRowsVisible) {
238                                 columnshdrs_area.Y += parent_rows.Height;
239                         }
240
241                         columnshdrs_area.X = grid.ClientRectangle.X;
242                         columnshdrs_area.Height = ColumnsHeaderHeight;
243                         width_all_cols = CalcAllColumnsWidth ();
244
245                         // TODO: take into account Scrollbars
246                         columnshdrs_maxwidth = grid.ClientRectangle.X + grid.ClientRectangle.Width - columnshdrs_area.X;
247                         max_width_cols = columnshdrs_maxwidth;
248
249                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
250                                 max_width_cols -= grid.RowHeaderWidth;
251                         }
252
253                         if (width_all_cols > max_width_cols) {
254                                 columnshdrs_area.Width = columnshdrs_maxwidth;
255                         } else {
256                                 columnshdrs_area.Width = width_all_cols;
257
258                                 if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
259                                         columnshdrs_area.Width += grid.RowHeaderWidth;
260                                 }
261                         }
262
263                         //Console.WriteLine ("DataGridDrawing.CalcColumnsHeader {0}", columnshdrs_area);
264                 }
265
266                 public void CalcParentRows ()
267                 {
268                         if (grid.ShowParentRowsVisible == false) {
269                                 parent_rows = Rectangle.Empty;
270                                 return;
271                         }
272
273                         if (grid.caption_visible) {
274                                 parent_rows.Y = caption_area.Y + caption_area.Height;
275
276                         } else {
277                                 parent_rows.Y = grid.ClientRectangle.Y;
278                         }
279
280                         parent_rows.X = grid.ClientRectangle.X;
281                         parent_rows.Width = grid.ClientRectangle.Width;
282                         parent_rows.Height = grid.CaptionFont.Height + 3;
283
284                         //Console.WriteLine ("DataGridDrawing.CalcParentRows {0}", parent_rows);
285                 }
286
287                 public void CalcRowsHeaders ()
288                 {
289                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible == false) {
290                                 rowshdrs_area = Rectangle.Empty;
291                                 return;
292                         }
293
294                         if (grid.caption_visible) {
295                                 rowshdrs_area.Y = caption_area.Y + caption_area.Height;
296                         } else {
297                                 rowshdrs_area.Y = grid.ClientRectangle.Y;
298                         }
299
300                         if (grid.ShowParentRowsVisible) {
301                                 rowshdrs_area.Y += parent_rows.Height;
302                         }
303
304                         if (grid.columnheaders_visible) { // first block is painted by ColumnHeader
305                                 rowshdrs_area.Y += ColumnsHeaderHeight;
306                         }
307
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;
312
313                         //Console.WriteLine ("DataGridDrawing.CalcRowsHeaders {0} {1}", rowshdrs_area,
314                         //      rowshdrs_maxheight);
315                 }
316
317                 public void UpdateVisibleColumn ()
318                 {
319                         if (grid.CurrentTableStyle.GridColumnStyles.Count == 0) {
320                                 grid.visiblecolumn_count = 0;
321                                 return; 
322                         }
323                         
324                         int col;
325                         int max_pixel = grid.horz_pixeloffset + cells_area.Width;
326                         grid.first_visiblecolumn = FromPixelToColumn (grid.horz_pixeloffset);
327
328                         col = FromPixelToColumn (max_pixel);
329                         
330                         grid.visiblecolumn_count = 1 + col - grid.first_visiblecolumn;
331                         
332                         if (grid.first_visiblecolumn + grid.visiblecolumn_count + 1 < grid.CurrentTableStyle.GridColumnStyles.Count) { 
333                                 grid.visiblecolumn_count++; // Partially visible column
334                         }
335                 }
336
337                 public void UpdateVisibleRowCount ()
338                 {
339                         int max_height = cells_area.Height;
340                         int total_rows = grid.RowsCount;
341                         
342                         if (grid.ShowEditRow) {
343                                 total_rows++;
344                         }
345
346                         int rows_height = (total_rows - grid.first_visiblerow) * grid.RowHeight;
347                         int max_rows = max_height / grid.RowHeight;
348
349                         //Console.WriteLine ("UpdateVisibleRowCount {0} {1}/{2} (row h) {3}",
350                         //      max_rows, max_height, grid.RowHeight, cells_area.Height);
351
352                         if (max_rows > total_rows) {
353                                 max_rows = total_rows;
354                         }
355
356                         if (rows_height > cells_area.Height) {
357                                 grid.visiblerow_count = max_rows;
358                         } else {
359                                 grid.visiblerow_count = total_rows;
360                         }       
361
362                         CalcRowsHeaders (); // Height depends on num of visible rows            
363                         
364                         if (grid.visiblerow_count + grid.first_visiblerow > total_rows)
365                                 grid.visiblerow_count = total_rows - grid.first_visiblerow;
366
367                         if (grid.visiblerow_count < max_rows) {
368                                 grid.visiblerow_count = max_rows;
369                                 grid.first_visiblerow = total_rows - max_rows;
370                                 grid.Invalidate ();
371                         }               
372                         
373                 }
374
375                 // From Point to Cell
376                 public DataGrid.HitTestInfo HitTest (int x, int y)
377                 {
378                         DataGrid.HitTestInfo hit = new DataGrid.HitTestInfo ();
379
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);
384                                 return hit;
385                         }
386
387                         if (rowshdrs_area.Contains (x, y)) {
388                                 hit.type = DataGrid.HitTestType.RowHeader;
389                                 int posy;
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
394                                                 hit.row = r;
395                                                 break;
396                                         }
397                                 }
398                                 return hit;
399                         }
400
401                         if (caption_area.Contains (x, y)) {
402                                 hit.type = DataGrid.HitTestType.Caption;
403                                 return hit;
404                         }
405
406                         if (parent_rows.Contains (x, y)) {
407                                 hit.type = DataGrid.HitTestType.ParentRows;
408                                 return hit;
409                         }
410
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);
415
416                                 if (y <= pos_y + grid.RowHeight) { // Found row
417                                         hit.row = row;
418                                         hit.type = DataGrid.HitTestType.Cell;                                   
419                                         int col_pixel;
420                                         int column_cnt = grid.first_visiblecolumn + grid.visiblecolumn_count;
421                                         for (int column = grid.first_visiblecolumn; column < column_cnt; column++) {
422
423                                                 col_pixel = GetColumnStartingPixel (column);
424                                                 pos_x = cells_area.X + col_pixel - grid.horz_pixeloffset;
425                                                 width = grid.CurrentTableStyle.GridColumnStyles[column].Width;
426
427                                                 if (x <= pos_x + width) { // Column found
428                                                         hit.column = column;
429                                                         break;
430                                                 }
431                                         }
432
433                                         break;
434                                 }
435                         }
436
437                         return hit;
438                 }
439
440                 public Rectangle GetCellBounds (int row, int col)
441                 {
442                         Rectangle bounds = new Rectangle ();
443                         int col_pixel;
444
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;
450                         return bounds;
451                 }
452
453                 public void InvalidateCaption ()
454                 {
455                         if (caption_area.IsEmpty)
456                                 return;
457
458                         grid.Invalidate (caption_area);
459                 }
460                 
461                 
462                 public void InvalidateRow (int row)
463                 {
464                         if (row < grid.FirstVisibleRow || row > grid.FirstVisibleRow + grid.VisibleRowCount) {
465                                 return;
466                         }
467
468                         Rectangle rect_row = new Rectangle ();
469
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);
475                 }
476
477                 public void InvalidateRowHeader (int row)
478                 {
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);
485                 }       
486
487                 public void InvalidateColumn (DataGridColumnStyle column)
488                 {
489                         Rectangle rect_col = new Rectangle ();
490                         int col_pixel;
491                         int col = -1;
492
493                         col = grid.CurrentTableStyle.GridColumnStyles.IndexOf (column);
494
495                         if (col == -1) {
496                                 return;
497                         }
498
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);
505                 }
506                                 
507                 // Return true if the scrollbar is needed
508                 public bool SetUpHorizontalScrollBar ()
509                 {
510                         int width_all = CalcAllColumnsWidth ();
511
512                         if (width_all <= cells_area.Width) {
513                                 grid.horiz_scrollbar.Visible = false;
514                                 grid.Controls.Remove (grid.horiz_scrollbar);
515                                 return false;
516                         }
517
518                         grid.horiz_scrollbar.Location = new Point (grid.ClientRectangle.X, grid.ClientRectangle.Y +
519                                 grid.ClientRectangle.Height - grid.horiz_scrollbar.Height);
520
521                         grid.horiz_scrollbar.Size = new Size (grid.ClientRectangle.Width,
522                                 grid.horiz_scrollbar.Height);
523
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;
528                         return true;
529                 }
530
531                 // Return true if the scrollbar is needed
532                 public bool SetUpVerticalScrollBar ()
533                 {
534                         int y, height;
535                         int allrows = grid.RowsCount;
536
537                         if (grid.ShowEditRow) {
538                                 allrows++;
539                         }
540                         
541                         if (grid.visiblerow_count == allrows) {
542                                 grid.vert_scrollbar.Visible = false;
543                                 grid.Controls.Remove (grid.vert_scrollbar);
544                                 return false;
545                         }
546                         
547                         if (grid.caption_visible) {
548                                 y = grid.ClientRectangle.Y + caption_area.Height;
549                                 height = grid.ClientRectangle.Height - caption_area.Height;
550                         } else {
551                                 y = grid.ClientRectangle.Y;
552                                 height = grid.ClientRectangle.Height;
553                         }
554
555                         grid.vert_scrollbar.Location = new Point (grid.ClientRectangle.X +
556                                 grid.ClientRectangle.Width - grid.vert_scrollbar.Width, y);
557
558                         grid.vert_scrollbar.Size = new Size (grid.vert_scrollbar.Width,
559                                 height);
560
561                         grid.vert_scrollbar.Maximum = grid.RowsCount;
562                         
563                         if (grid.ShowEditRow) {
564                                 grid.vert_scrollbar.Maximum++;  
565                         }
566                         
567                         grid.vert_scrollbar.LargeChange = VLargeChange;
568
569                         grid.Controls.Add (grid.vert_scrollbar);
570                         grid.vert_scrollbar.Visible = true;
571                         return true;
572                 }
573
574                 #endregion // Public Instance Methods
575
576                 #region Instance Properties
577                 public Rectangle CellsArea {
578                         get {
579                                 return cells_area;
580                         }
581                 }
582
583                 // Returns the ColumnsHeader area excluding the rectangle shared with RowsHeader
584                 public Rectangle ColumnsHeadersArea {
585                         get {
586                                 Rectangle columns_area = columnshdrs_area;
587
588                                 if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
589                                         columns_area.X += grid.RowHeaderWidth;
590                                         columns_area.Width -= grid.RowHeaderWidth;
591                                 }
592                                 return columns_area;
593                         }
594                 }
595
596                 public int ColumnsHeaderHeight {
597                         get {
598                                 return grid.CurrentTableStyle.HeaderFont.Height + 6;
599                         }
600                 }
601
602                 public Rectangle RowsHeadersArea {
603                         get {
604                                 return rowshdrs_area;
605                         }
606                 }
607
608                 public int VLargeChange {
609                         get {
610                                 return cells_area.Height / grid.RowHeight;
611                         }
612                 }
613
614                 #endregion Instance Properties
615         }
616 }