1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/gridctrl.cpp
3 // Purpose: wxGrid controls
4 // Author: Paul Gammans, Roger Gammans
8 // Copyright: (c) The Computer Surgery (paul@compsurg.co.uk)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/generic/gridctrl.h"
21 #include "wx/generic/grideditors.h"
24 #include "wx/textctrl.h"
26 #include "wx/combobox.h"
27 #include "wx/settings.h"
29 #include "wx/checkbox.h"
32 #include "wx/tokenzr.h"
33 #include "wx/renderer.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 void wxGridCellRenderer::Draw(wxGrid
& grid
,
44 int WXUNUSED(row
), int WXUNUSED(col
),
47 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
50 if ( grid
.IsThisEnabled() )
54 if ( grid
.HasFocus() )
55 clr
= grid
.GetSelectionBackground();
57 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
61 clr
= attr
.GetBackgroundColour();
64 else // grey out fields if the grid is disabled
66 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
70 dc
.SetPen( *wxTRANSPARENT_PEN
);
71 dc
.DrawRectangle(rect
);
75 // ----------------------------------------------------------------------------
76 // wxGridCellDateTimeRenderer
77 // ----------------------------------------------------------------------------
81 // Enables a grid cell to display a formatted date and or time
83 wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(const wxString
& outformat
, const wxString
& informat
)
86 m_oformat
= outformat
;
87 m_tz
= wxDateTime::Local
;
88 m_dateDef
= wxDefaultDateTime
;
91 wxGridCellRenderer
*wxGridCellDateTimeRenderer::Clone() const
93 wxGridCellDateTimeRenderer
*renderer
= new wxGridCellDateTimeRenderer
;
94 renderer
->m_iformat
= m_iformat
;
95 renderer
->m_oformat
= m_oformat
;
96 renderer
->m_dateDef
= m_dateDef
;
97 renderer
->m_tz
= m_tz
;
102 wxString
wxGridCellDateTimeRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
104 wxGridTableBase
*table
= grid
.GetTable();
106 bool hasDatetime
= false;
109 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_DATETIME
) )
111 void * tempval
= table
->GetValueAsCustom(row
, col
,wxGRID_VALUE_DATETIME
);
115 val
= *((wxDateTime
*)tempval
);
117 delete (wxDateTime
*)tempval
;
124 text
= table
->GetValue(row
, col
);
125 const char * const end
= val
.ParseFormat(text
, m_iformat
, m_dateDef
);
126 hasDatetime
= end
&& !*end
;
130 text
= val
.Format(m_oformat
, m_tz
);
132 // If we failed to parse string just show what we where given?
136 void wxGridCellDateTimeRenderer::Draw(wxGrid
& grid
,
137 wxGridCellAttr
& attr
,
139 const wxRect
& rectCell
,
143 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
145 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
147 // draw the text right aligned by default
149 attr
.GetAlignment(&hAlign
, &vAlign
);
152 wxRect rect
= rectCell
;
155 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
158 wxSize
wxGridCellDateTimeRenderer::GetBestSize(wxGrid
& grid
,
159 wxGridCellAttr
& attr
,
163 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
166 void wxGridCellDateTimeRenderer::SetParameters(const wxString
& params
)
172 #endif // wxUSE_DATETIME
174 // ----------------------------------------------------------------------------
175 // wxGridCellChoiceNumberRenderer
176 // ----------------------------------------------------------------------------
177 // Renders a number as a textual equivalent.
178 // eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob"
181 wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString
& choices
)
183 if (!choices
.empty())
184 SetParameters(choices
);
187 wxGridCellRenderer
*wxGridCellEnumRenderer::Clone() const
189 wxGridCellEnumRenderer
*renderer
= new wxGridCellEnumRenderer
;
190 renderer
->m_choices
= m_choices
;
194 wxString
wxGridCellEnumRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
196 wxGridTableBase
*table
= grid
.GetTable();
198 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
200 int choiceno
= table
->GetValueAsLong(row
, col
);
201 text
.Printf(wxT("%s"), m_choices
[ choiceno
].c_str() );
205 text
= table
->GetValue(row
, col
);
209 //If we faild to parse string just show what we where given?
213 void wxGridCellEnumRenderer::Draw(wxGrid
& grid
,
214 wxGridCellAttr
& attr
,
216 const wxRect
& rectCell
,
220 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
222 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
224 // draw the text right aligned by default
226 attr
.GetAlignment(&hAlign
, &vAlign
);
229 wxRect rect
= rectCell
;
232 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
235 wxSize
wxGridCellEnumRenderer::GetBestSize(wxGrid
& grid
,
236 wxGridCellAttr
& attr
,
240 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
243 void wxGridCellEnumRenderer::SetParameters(const wxString
& params
)
253 wxStringTokenizer
tk(params
, wxT(','));
254 while ( tk
.HasMoreTokens() )
256 m_choices
.Add(tk
.GetNextToken());
261 // ----------------------------------------------------------------------------
262 // wxGridCellAutoWrapStringRenderer
263 // ----------------------------------------------------------------------------
267 wxGridCellAutoWrapStringRenderer::Draw(wxGrid
& grid
,
268 wxGridCellAttr
& attr
,
270 const wxRect
& rectCell
,
275 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
277 // now we only have to draw the text
278 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
280 int horizAlign
, vertAlign
;
281 attr
.GetAlignment(&horizAlign
, &vertAlign
);
283 wxRect rect
= rectCell
;
286 grid
.DrawTextRectangle(dc
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
),
287 rect
, horizAlign
, vertAlign
);
292 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid
& grid
,
294 const wxGridCellAttr
& attr
,
298 wxString data
= grid
.GetCellValue(row
, col
);
301 dc
.SetFont(attr
.GetFont());
303 //Taken from wxGrid again!
304 wxCoord x
= 0, y
= 0, curr_x
= 0;
305 wxCoord max_x
= rect
.GetWidth();
307 dc
.SetFont(attr
.GetFont());
308 wxStringTokenizer
tk(data
, wxT(" \n\t\r"));
309 wxString thisline
= wxEmptyString
;
311 while ( tk
.HasMoreTokens() )
313 wxString tok
= tk
.GetNextToken();
314 //FIXME: this causes us to print an extra unnecesary
315 // space at the end of the line. But it
316 // is invisible , simplifies the size calculation
317 // and ensures tokens are separated in the display
320 dc
.GetTextExtent(tok
, &x
, &y
);
321 if ( curr_x
+ x
> max_x
)
325 // this means that a single token is wider than the maximal
326 // width -- still use it as is as we need to show at least the
327 // part of it which fits
344 lines
.Add( wxString(thisline
) );
351 wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid
& grid
,
352 wxGridCellAttr
& attr
,
356 wxCoord x
,y
, height
, width
= grid
.GetColSize(col
) -20;
357 // for width, subtract 20 because ColSize includes a magin of 10 pixels
358 // that we do not want here and because we always start with an increment
359 // by 10 in the loop below.
360 int count
= 250; //Limit iterations..
362 wxRect
rect(0,0,width
,10);
364 // M is a nice large character 'y' gives descender!.
365 dc
.GetTextExtent(wxT("My"), &x
, &y
);
370 rect
.SetWidth(width
);
371 height
= y
* (wx_truncate_cast(wxCoord
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
).GetCount()));
373 // Search for a shape no taller than the golden ratio.
374 } while (count
&& (width
< (height
*1.68)) );
377 return wxSize(width
,height
);
381 // ----------------------------------------------------------------------------
382 // wxGridCellStringRenderer
383 // ----------------------------------------------------------------------------
385 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
386 const wxGridCellAttr
& attr
,
390 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
392 // TODO some special colours for attr.IsReadOnly() case?
394 // different coloured text when the grid is disabled
395 if ( grid
.IsThisEnabled() )
400 if ( grid
.HasFocus() )
401 clr
= grid
.GetSelectionBackground();
403 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
404 dc
.SetTextBackground( clr
);
405 dc
.SetTextForeground( grid
.GetSelectionForeground() );
409 dc
.SetTextBackground( attr
.GetBackgroundColour() );
410 dc
.SetTextForeground( attr
.GetTextColour() );
415 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
416 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
419 dc
.SetFont( attr
.GetFont() );
422 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
424 const wxString
& text
)
426 wxCoord x
= 0, y
= 0, max_x
= 0;
427 dc
.SetFont(attr
.GetFont());
428 wxStringTokenizer
tk(text
, wxT('\n'));
429 while ( tk
.HasMoreTokens() )
431 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
432 max_x
= wxMax(max_x
, x
);
435 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
437 return wxSize(max_x
, y
);
440 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
441 wxGridCellAttr
& attr
,
445 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
448 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
449 wxGridCellAttr
& attr
,
451 const wxRect
& rectCell
,
455 wxRect rect
= rectCell
;
458 // erase only this cells background, overflow cells should have been erased
459 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
462 attr
.GetAlignment(&hAlign
, &vAlign
);
464 int overflowCols
= 0;
466 if (attr
.GetOverflow())
468 int cols
= grid
.GetNumberCols();
469 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
470 int cell_rows
, cell_cols
;
471 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
472 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
474 int i
, c_cols
, c_rows
;
475 for (i
= col
+cell_cols
; i
< cols
; i
++)
477 bool is_empty
= true;
478 for (int j
=row
; j
< row
+ cell_rows
; j
++)
480 // check w/ anchor cell for multicell block
481 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
484 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
493 rect
.width
+= grid
.GetColSize(i
);
501 if (rect
.width
>= best_width
)
505 overflowCols
= i
- col
- cell_cols
+ 1;
506 if (overflowCols
>= cols
)
507 overflowCols
= cols
- 1;
510 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
512 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
514 clip
.x
+= rectCell
.width
;
515 // draw each overflow cell individually
516 int col_end
= col
+ cell_cols
+ overflowCols
;
517 if (col_end
>= grid
.GetNumberCols())
518 col_end
= grid
.GetNumberCols() - 1;
519 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
521 clip
.width
= grid
.GetColSize(i
) - 1;
522 dc
.DestroyClippingRegion();
523 dc
.SetClippingRegion(clip
);
525 SetTextColoursAndFont(grid
, attr
, dc
,
526 grid
.IsInSelection(row
,i
));
528 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
529 rect
, hAlign
, vAlign
);
530 clip
.x
+= grid
.GetColSize(i
) - 1;
536 dc
.DestroyClippingRegion();
540 // now we only have to draw the text
541 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
543 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
544 rect
, hAlign
, vAlign
);
547 // ----------------------------------------------------------------------------
548 // wxGridCellNumberRenderer
549 // ----------------------------------------------------------------------------
551 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
553 wxGridTableBase
*table
= grid
.GetTable();
555 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
557 text
.Printf(wxT("%ld"), table
->GetValueAsLong(row
, col
));
561 text
= table
->GetValue(row
, col
);
567 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
568 wxGridCellAttr
& attr
,
570 const wxRect
& rectCell
,
574 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
576 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
578 // draw the text right aligned by default
580 attr
.GetAlignment(&hAlign
, &vAlign
);
581 hAlign
= wxALIGN_RIGHT
;
583 wxRect rect
= rectCell
;
586 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
589 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
590 wxGridCellAttr
& attr
,
594 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
597 // ----------------------------------------------------------------------------
598 // wxGridCellFloatRenderer
599 // ----------------------------------------------------------------------------
601 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
604 SetPrecision(precision
);
607 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
609 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
610 renderer
->m_width
= m_width
;
611 renderer
->m_precision
= m_precision
;
612 renderer
->m_format
= m_format
;
617 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
619 wxGridTableBase
*table
= grid
.GetTable();
624 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
626 val
= table
->GetValueAsDouble(row
, col
);
631 text
= table
->GetValue(row
, col
);
632 hasDouble
= text
.ToDouble(&val
);
641 if ( m_precision
== -1 )
643 // default width/precision
644 m_format
= wxT("%f");
648 m_format
.Printf(wxT("%%.%df"), m_precision
);
651 else if ( m_precision
== -1 )
654 m_format
.Printf(wxT("%%%d.f"), m_width
);
658 m_format
.Printf(wxT("%%%d.%df"), m_width
, m_precision
);
662 text
.Printf(m_format
, val
);
665 //else: text already contains the string
670 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
671 wxGridCellAttr
& attr
,
673 const wxRect
& rectCell
,
677 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
679 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
681 // draw the text right aligned by default
683 attr
.GetAlignment(&hAlign
, &vAlign
);
684 hAlign
= wxALIGN_RIGHT
;
686 wxRect rect
= rectCell
;
689 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
692 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
693 wxGridCellAttr
& attr
,
697 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
700 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
710 wxString tmp
= params
.BeforeFirst(wxT(','));
714 if ( tmp
.ToLong(&width
) )
716 SetWidth((int)width
);
720 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
724 tmp
= params
.AfterFirst(wxT(','));
728 if ( tmp
.ToLong(&precision
) )
730 SetPrecision((int)precision
);
734 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
740 // ----------------------------------------------------------------------------
741 // wxGridCellBoolRenderer
742 // ----------------------------------------------------------------------------
744 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
746 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
747 wxGridCellAttr
& WXUNUSED(attr
),
752 // compute it only once (no locks for MT safeness in GUI thread...)
753 if ( !ms_sizeCheckMark
.x
)
755 ms_sizeCheckMark
= wxRendererNative::Get().GetCheckBoxSize(&grid
);
758 return ms_sizeCheckMark
;
761 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
762 wxGridCellAttr
& attr
,
768 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
770 // draw a check mark in the centre (ignoring alignment - TODO)
771 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
773 // don't draw outside the cell
774 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
775 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
777 // and even leave (at least) 1 pixel margin
778 size
.x
= size
.y
= minSize
;
781 // draw a border around checkmark
783 attr
.GetAlignment(&hAlign
, &vAlign
);
786 if (hAlign
== wxALIGN_CENTRE
)
788 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
789 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
790 rectBorder
.width
= size
.x
;
791 rectBorder
.height
= size
.y
;
793 else if (hAlign
== wxALIGN_LEFT
)
795 rectBorder
.x
= rect
.x
+ 2;
796 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
797 rectBorder
.width
= size
.x
;
798 rectBorder
.height
= size
.y
;
800 else if (hAlign
== wxALIGN_RIGHT
)
802 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
803 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
804 rectBorder
.width
= size
.x
;
805 rectBorder
.height
= size
.y
;
809 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
811 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
815 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
816 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
821 flags
|= wxCONTROL_CHECKED
;
823 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);