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"
23 #include "wx/textctrl.h"
27 #include "wx/generic/gridctrl.h"
28 #include "wx/tokenzr.h"
30 // ----------------------------------------------------------------------------
31 // wxGridCellDateTimeRenderer
32 // ----------------------------------------------------------------------------
34 // Enables a grid cell to display a formated date and or time
36 wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(wxString outformat
, wxString informat
)
39 m_oformat
= outformat
;
40 m_tz
= wxDateTime::Local
;
41 m_dateDef
= wxDefaultDateTime
;
44 wxGridCellRenderer
*wxGridCellDateTimeRenderer::Clone() const
46 wxGridCellDateTimeRenderer
*renderer
= new wxGridCellDateTimeRenderer
;
47 renderer
->m_iformat
= m_iformat
;
48 renderer
->m_oformat
= m_oformat
;
49 renderer
->m_dateDef
= m_dateDef
;
50 renderer
->m_tz
= m_tz
;
55 wxString
wxGridCellDateTimeRenderer::GetString(wxGrid
& grid
, int row
, int col
)
57 wxGridTableBase
*table
= grid
.GetTable();
59 bool hasDatetime
= FALSE
;
62 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_DATETIME
) )
64 void * tempval
= table
->GetValueAsCustom(row
, col
,wxGRID_VALUE_DATETIME
);
67 val
= *((wxDateTime
*)tempval
);
69 delete (wxDateTime
*)tempval
;
76 text
= table
->GetValue(row
, col
);
77 hasDatetime
= (val
.ParseFormat( text
, m_iformat
, m_dateDef
) != (wxChar
*)NULL
) ;
81 text
= val
.Format(m_oformat
, m_tz
);
83 //If we faild to parse string just show what we where given?
87 void wxGridCellDateTimeRenderer::Draw(wxGrid
& grid
,
90 const wxRect
& rectCell
,
94 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
96 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
98 // draw the text right aligned by default
100 attr
.GetAlignment(&hAlign
, &vAlign
);
103 wxRect rect
= rectCell
;
106 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
109 wxSize
wxGridCellDateTimeRenderer::GetBestSize(wxGrid
& grid
,
110 wxGridCellAttr
& attr
,
114 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
117 void wxGridCellDateTimeRenderer::SetParameters(const wxString
& params
){
118 if (!params
.IsEmpty())
122 // ----------------------------------------------------------------------------
123 // wxGridCellChoiceNumberRenderer
124 // ----------------------------------------------------------------------------
125 // Renders a number as a textual equivalent.
126 // eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob"
129 wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString
& choices
)
131 if (!choices
.IsEmpty())
132 SetParameters(choices
);
135 wxGridCellRenderer
*wxGridCellEnumRenderer::Clone() const
137 wxGridCellEnumRenderer
*renderer
= new wxGridCellEnumRenderer
;
138 renderer
->m_choices
= m_choices
;
142 wxString
wxGridCellEnumRenderer::GetString(wxGrid
& grid
, int row
, int col
)
144 wxGridTableBase
*table
= grid
.GetTable();
146 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
148 int choiceno
= table
->GetValueAsLong(row
, col
);
149 text
.Printf(_T("%s"), m_choices
[ choiceno
].c_str() );
153 text
= table
->GetValue(row
, col
);
157 //If we faild to parse string just show what we where given?
161 void wxGridCellEnumRenderer::Draw(wxGrid
& grid
,
162 wxGridCellAttr
& attr
,
164 const wxRect
& rectCell
,
168 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
170 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
172 // draw the text right aligned by default
174 attr
.GetAlignment(&hAlign
, &vAlign
);
177 wxRect rect
= rectCell
;
180 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
183 wxSize
wxGridCellEnumRenderer::GetBestSize(wxGrid
& grid
,
184 wxGridCellAttr
& attr
,
188 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
191 void wxGridCellEnumRenderer::SetParameters(const wxString
& params
)
201 wxStringTokenizer
tk(params
, _T(','));
202 while ( tk
.HasMoreTokens() )
204 m_choices
.Add(tk
.GetNextToken());
208 // ----------------------------------------------------------------------------
209 // wxGridCellEnumEditor
210 // ----------------------------------------------------------------------------
211 // A cell editor which displays an enum number as a textual equivalent.
212 // eg data in cell is 0,1,2 ... n the cell could be displayed as "John","Fred"..."Bob"
213 // in the combo choice box
215 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
216 : wxGridCellChoiceEditor()
220 if (!choices
.IsEmpty())
221 SetParameters(choices
);
224 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
226 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
227 editor
->m_startint
= m_startint
;
231 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
233 wxASSERT_MSG(m_control
,
234 wxT("The wxGridCellEnumEditor must be Created first!"));
236 wxGridTableBase
*table
= grid
->GetTable();
238 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
240 m_startint
= table
->GetValueAsLong(row
, col
);
244 wxString startValue
= table
->GetValue(row
, col
);
245 if (startValue
.IsNumber() && !startValue
.IsEmpty())
247 startValue
.ToLong(&m_startint
);
255 Combo()->SetSelection(m_startint
);
256 Combo()->SetInsertionPointEnd();
261 bool wxGridCellEnumEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
263 int pos
= Combo()->GetSelection();
264 bool changed
= (pos
!= m_startint
);
267 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
268 grid
->GetTable()->SetValueAsLong(row
, col
, pos
);
270 grid
->GetTable()->SetValue(row
, col
,wxString::Format(wxT("%i"),pos
));
278 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
280 wxEvtHandler
* evtHandler
)
282 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
283 wxDefaultPosition
, wxDefaultSize
,
284 wxTE_MULTILINE
| wxTE_RICH
);
287 wxGridCellEditor::Create(parent
, id
, evtHandler
);
291 wxGridCellAutoWrapStringRenderer::Draw(wxGrid
& grid
,
292 wxGridCellAttr
& attr
,
294 const wxRect
& rectCell
,
299 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
301 // now we only have to draw the text
302 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
304 int horizAlign
, vertAlign
;
305 attr
.GetAlignment(&horizAlign
, &vertAlign
);
307 wxRect rect
= rectCell
;
310 grid
.DrawTextRectangle(dc
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
),
311 rect
, horizAlign
, vertAlign
);
316 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid
& grid
,
318 wxGridCellAttr
& attr
,
322 wxString data
= grid
.GetCellValue(row
, col
);
325 dc
.SetFont(attr
.GetFont());
327 //Taken from wxGrid again!
328 wxCoord x
= 0, y
= 0, curr_x
= 0;
329 wxCoord max_x
= rect
.GetWidth();
331 dc
.SetFont(attr
.GetFont());
332 wxStringTokenizer
tk(data
, _T(" \n\t\r"));
333 wxString
thisline("");
335 while ( tk
.HasMoreTokens() )
337 wxString tok
= tk
.GetNextToken();
338 //FIXME: this causes us to print an extra unnecesary
339 // space at the end of the line. But it
340 // is invisible , simplifies the size calculation
341 // and ensures tokens are seperated in the display
344 dc
.GetTextExtent(tok
, &x
, &y
);
345 if ( curr_x
+ x
> max_x
) {
346 lines
.Add( wxString(thisline
) );
356 lines
.Add( wxString(thisline
) );
363 wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid
& grid
,
364 wxGridCellAttr
& attr
,
368 wxCoord x
,y
, height
, width
= grid
.GetColSize(col
) -10;
369 int count
= 250; //Limit iterations..
371 wxRect
rect(0,0,width
,10);
373 // M is a nice large character 'y' gives descender!.
374 dc
.GetTextExtent("My", &x
, &y
);
379 rect
.SetWidth(width
);
380 height
= y
*( GetTextLines(grid
,dc
,attr
,rect
,row
,col
).GetCount());
382 // Search for a shape no taller than the golden ratio.
383 } while (count
&& (width
< (height
*1.68)) );
386 return wxSize(width
,height
);