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"
23 #include "wx/textctrl.h"
25 #include "wx/combobox.h"
28 #include "wx/tokenzr.h"
30 // ----------------------------------------------------------------------------
31 // wxGridCellDateTimeRenderer
32 // ----------------------------------------------------------------------------
36 // Enables a grid cell to display a formatted date and or time
38 wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(const wxString
& outformat
, const wxString
& informat
)
41 m_oformat
= outformat
;
42 m_tz
= wxDateTime::Local
;
43 m_dateDef
= wxDefaultDateTime
;
46 wxGridCellRenderer
*wxGridCellDateTimeRenderer::Clone() const
48 wxGridCellDateTimeRenderer
*renderer
= new wxGridCellDateTimeRenderer
;
49 renderer
->m_iformat
= m_iformat
;
50 renderer
->m_oformat
= m_oformat
;
51 renderer
->m_dateDef
= m_dateDef
;
52 renderer
->m_tz
= m_tz
;
57 wxString
wxGridCellDateTimeRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
59 wxGridTableBase
*table
= grid
.GetTable();
61 bool hasDatetime
= false;
64 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_DATETIME
) )
66 void * tempval
= table
->GetValueAsCustom(row
, col
,wxGRID_VALUE_DATETIME
);
69 val
= *((wxDateTime
*)tempval
);
71 delete (wxDateTime
*)tempval
;
78 text
= table
->GetValue(row
, col
);
79 hasDatetime
= (val
.ParseFormat(text
.c_str(), m_iformat
, m_dateDef
) != (wxChar
*)NULL
) ;
83 text
= val
.Format(m_oformat
, m_tz
);
85 //If we faild to parse string just show what we where given?
89 void wxGridCellDateTimeRenderer::Draw(wxGrid
& grid
,
92 const wxRect
& rectCell
,
96 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
98 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
100 // draw the text right aligned by default
102 attr
.GetAlignment(&hAlign
, &vAlign
);
105 wxRect rect
= rectCell
;
108 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
111 wxSize
wxGridCellDateTimeRenderer::GetBestSize(wxGrid
& grid
,
112 wxGridCellAttr
& attr
,
116 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
119 void wxGridCellDateTimeRenderer::SetParameters(const wxString
& params
)
125 #endif // wxUSE_DATETIME
127 // ----------------------------------------------------------------------------
128 // wxGridCellChoiceNumberRenderer
129 // ----------------------------------------------------------------------------
130 // Renders a number as a textual equivalent.
131 // eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob"
134 wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString
& choices
)
136 if (!choices
.empty())
137 SetParameters(choices
);
140 wxGridCellRenderer
*wxGridCellEnumRenderer::Clone() const
142 wxGridCellEnumRenderer
*renderer
= new wxGridCellEnumRenderer
;
143 renderer
->m_choices
= m_choices
;
147 wxString
wxGridCellEnumRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
149 wxGridTableBase
*table
= grid
.GetTable();
151 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
153 int choiceno
= table
->GetValueAsLong(row
, col
);
154 text
.Printf(_T("%s"), m_choices
[ choiceno
].c_str() );
158 text
= table
->GetValue(row
, col
);
162 //If we faild to parse string just show what we where given?
166 void wxGridCellEnumRenderer::Draw(wxGrid
& grid
,
167 wxGridCellAttr
& attr
,
169 const wxRect
& rectCell
,
173 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
175 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
177 // draw the text right aligned by default
179 attr
.GetAlignment(&hAlign
, &vAlign
);
182 wxRect rect
= rectCell
;
185 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
188 wxSize
wxGridCellEnumRenderer::GetBestSize(wxGrid
& grid
,
189 wxGridCellAttr
& attr
,
193 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
196 void wxGridCellEnumRenderer::SetParameters(const wxString
& params
)
206 wxStringTokenizer
tk(params
, _T(','));
207 while ( tk
.HasMoreTokens() )
209 m_choices
.Add(tk
.GetNextToken());
215 // ----------------------------------------------------------------------------
216 // wxGridCellEnumEditor
217 // ----------------------------------------------------------------------------
219 // A cell editor which displays an enum number as a textual equivalent. eg
220 // data in cell is 0,1,2 ... n the cell could be displayed as
221 // "John","Fred"..."Bob" in the combo choice box
223 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
224 :wxGridCellChoiceEditor()
228 if (!choices
.empty())
229 SetParameters(choices
);
232 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
234 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
235 editor
->m_startint
= m_startint
;
239 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
241 wxASSERT_MSG(m_control
,
242 wxT("The wxGridCellEnumEditor must be Created first!"));
244 wxGridTableBase
*table
= grid
->GetTable();
246 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
248 m_startint
= table
->GetValueAsLong(row
, col
);
252 wxString startValue
= table
->GetValue(row
, col
);
253 if (startValue
.IsNumber() && !startValue
.empty())
255 startValue
.ToLong(&m_startint
);
263 Combo()->SetSelection(m_startint
);
264 Combo()->SetInsertionPointEnd();
269 bool wxGridCellEnumEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
271 int pos
= Combo()->GetSelection();
272 bool changed
= (pos
!= m_startint
);
275 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
276 grid
->GetTable()->SetValueAsLong(row
, col
, pos
);
278 grid
->GetTable()->SetValue(row
, col
,wxString::Format(wxT("%i"),pos
));
284 #endif // wxUSE_COMBOBOX
286 // ----------------------------------------------------------------------------
287 // wxGridCellAutoWrapStringEditor
288 // ----------------------------------------------------------------------------
291 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
293 wxEvtHandler
* evtHandler
)
295 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
296 wxTE_MULTILINE
| wxTE_RICH
);
300 wxGridCellAutoWrapStringRenderer::Draw(wxGrid
& grid
,
301 wxGridCellAttr
& attr
,
303 const wxRect
& rectCell
,
308 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
310 // now we only have to draw the text
311 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
313 int horizAlign
, vertAlign
;
314 attr
.GetAlignment(&horizAlign
, &vertAlign
);
316 wxRect rect
= rectCell
;
319 grid
.DrawTextRectangle(dc
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
),
320 rect
, horizAlign
, vertAlign
);
325 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid
& grid
,
327 const wxGridCellAttr
& attr
,
331 wxString data
= grid
.GetCellValue(row
, col
);
334 dc
.SetFont(attr
.GetFont());
336 //Taken from wxGrid again!
337 wxCoord x
= 0, y
= 0, curr_x
= 0;
338 wxCoord max_x
= rect
.GetWidth();
340 dc
.SetFont(attr
.GetFont());
341 wxStringTokenizer
tk(data
, _T(" \n\t\r"));
342 wxString thisline
= wxEmptyString
;
344 while ( tk
.HasMoreTokens() )
346 wxString tok
= tk
.GetNextToken();
347 //FIXME: this causes us to print an extra unnecesary
348 // space at the end of the line. But it
349 // is invisible , simplifies the size calculation
350 // and ensures tokens are separated in the display
353 dc
.GetTextExtent(tok
, &x
, &y
);
354 if ( curr_x
+ x
> max_x
)
356 lines
.Add( wxString(thisline
) );
367 lines
.Add( wxString(thisline
) );
374 wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid
& grid
,
375 wxGridCellAttr
& attr
,
379 wxCoord x
,y
, height
, width
= grid
.GetColSize(col
) -10;
380 int count
= 250; //Limit iterations..
382 wxRect
rect(0,0,width
,10);
384 // M is a nice large character 'y' gives descender!.
385 dc
.GetTextExtent(wxT("My"), &x
, &y
);
390 rect
.SetWidth(width
);
391 height
= y
* (wx_truncate_cast(wxCoord
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
).GetCount()));
393 // Search for a shape no taller than the golden ratio.
394 } while (count
&& (width
< (height
*1.68)) );
397 return wxSize(width
,height
);