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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gridctrl.h"
16 #include "wx/wxprec.h"
22 #if wxUSE_GRID || wxUSE_NEW_GRID
25 #include "wx/textctrl.h"
29 #include "wx/generic/gridctrl.h"
30 #include "wx/tokenzr.h"
32 // ----------------------------------------------------------------------------
33 // wxGridCellDateTimeRenderer
34 // ----------------------------------------------------------------------------
36 // Enables a grid cell to display a formated date and or time
38 wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(wxString outformat
, 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(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
, 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
){
120 if (!params
.IsEmpty())
124 // ----------------------------------------------------------------------------
125 // wxGridCellChoiceNumberRenderer
126 // ----------------------------------------------------------------------------
127 // Renders a number as a textual equivalent.
128 // eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob"
131 wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString
& choices
)
133 if (!choices
.IsEmpty())
134 SetParameters(choices
);
137 wxGridCellRenderer
*wxGridCellEnumRenderer::Clone() const
139 wxGridCellEnumRenderer
*renderer
= new wxGridCellEnumRenderer
;
140 renderer
->m_choices
= m_choices
;
144 wxString
wxGridCellEnumRenderer::GetString(wxGrid
& grid
, int row
, int col
)
146 wxGridTableBase
*table
= grid
.GetTable();
148 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
150 int choiceno
= table
->GetValueAsLong(row
, col
);
151 text
.Printf(_T("%s"), m_choices
[ choiceno
].c_str() );
155 text
= table
->GetValue(row
, col
);
159 //If we faild to parse string just show what we where given?
163 void wxGridCellEnumRenderer::Draw(wxGrid
& grid
,
164 wxGridCellAttr
& attr
,
166 const wxRect
& rectCell
,
170 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
172 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
174 // draw the text right aligned by default
176 attr
.GetAlignment(&hAlign
, &vAlign
);
179 wxRect rect
= rectCell
;
182 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
185 wxSize
wxGridCellEnumRenderer::GetBestSize(wxGrid
& grid
,
186 wxGridCellAttr
& attr
,
190 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
193 void wxGridCellEnumRenderer::SetParameters(const wxString
& params
)
203 wxStringTokenizer
tk(params
, _T(','));
204 while ( tk
.HasMoreTokens() )
206 m_choices
.Add(tk
.GetNextToken());
212 // ----------------------------------------------------------------------------
213 // wxGridCellEnumEditor
214 // ----------------------------------------------------------------------------
216 // A cell editor which displays an enum number as a textual equivalent. eg
217 // data in cell is 0,1,2 ... n the cell could be displayed as
218 // "John","Fred"..."Bob" in the combo choice box
220 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
221 : wxGridCellChoiceEditor()
225 if (!choices
.IsEmpty())
226 SetParameters(choices
);
229 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
231 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
232 editor
->m_startint
= m_startint
;
236 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
238 wxASSERT_MSG(m_control
,
239 wxT("The wxGridCellEnumEditor must be Created first!"));
241 wxGridTableBase
*table
= grid
->GetTable();
243 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
245 m_startint
= table
->GetValueAsLong(row
, col
);
249 wxString startValue
= table
->GetValue(row
, col
);
250 if (startValue
.IsNumber() && !startValue
.IsEmpty())
252 startValue
.ToLong(&m_startint
);
260 Combo()->SetSelection(m_startint
);
261 Combo()->SetInsertionPointEnd();
266 bool wxGridCellEnumEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
268 int pos
= Combo()->GetSelection();
269 bool changed
= (pos
!= m_startint
);
272 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
273 grid
->GetTable()->SetValueAsLong(row
, col
, pos
);
275 grid
->GetTable()->SetValue(row
, col
,wxString::Format(wxT("%i"),pos
));
281 #endif // wxUSE_COMBOBOX
283 // ----------------------------------------------------------------------------
284 // wxGridCellAutoWrapStringEditor
285 // ----------------------------------------------------------------------------
288 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
290 wxEvtHandler
* evtHandler
)
292 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
293 wxDefaultPosition
, wxDefaultSize
,
294 wxTE_MULTILINE
| wxTE_RICH
);
297 wxGridCellEditor::Create(parent
, id
, evtHandler
);
301 wxGridCellAutoWrapStringRenderer::Draw(wxGrid
& grid
,
302 wxGridCellAttr
& attr
,
304 const wxRect
& rectCell
,
309 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
311 // now we only have to draw the text
312 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
314 int horizAlign
, vertAlign
;
315 attr
.GetAlignment(&horizAlign
, &vertAlign
);
317 wxRect rect
= rectCell
;
320 grid
.DrawTextRectangle(dc
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
),
321 rect
, horizAlign
, vertAlign
);
326 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid
& grid
,
328 wxGridCellAttr
& attr
,
332 wxString data
= grid
.GetCellValue(row
, col
);
335 dc
.SetFont(attr
.GetFont());
337 //Taken from wxGrid again!
338 wxCoord x
= 0, y
= 0, curr_x
= 0;
339 wxCoord max_x
= rect
.GetWidth();
341 dc
.SetFont(attr
.GetFont());
342 wxStringTokenizer
tk(data
, _T(" \n\t\r"));
343 wxString
thisline(wxT(""));
345 while ( tk
.HasMoreTokens() )
347 wxString tok
= tk
.GetNextToken();
348 //FIXME: this causes us to print an extra unnecesary
349 // space at the end of the line. But it
350 // is invisible , simplifies the size calculation
351 // and ensures tokens are seperated in the display
354 dc
.GetTextExtent(tok
, &x
, &y
);
355 if ( curr_x
+ x
> max_x
) {
356 lines
.Add( wxString(thisline
) );
366 lines
.Add( wxString(thisline
) );
373 wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid
& grid
,
374 wxGridCellAttr
& attr
,
378 wxCoord x
,y
, height
, width
= grid
.GetColSize(col
) -10;
379 int count
= 250; //Limit iterations..
381 wxRect
rect(0,0,width
,10);
383 // M is a nice large character 'y' gives descender!.
384 dc
.GetTextExtent(wxT("My"), &x
, &y
);
389 rect
.SetWidth(width
);
390 height
= y
*( GetTextLines(grid
,dc
,attr
,rect
,row
,col
).GetCount());
392 // Search for a shape no taller than the golden ratio.
393 } while (count
&& (width
< (height
*1.68)) );
396 return wxSize(width
,height
);