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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "gridctrl.h"
16 #include "wx/wxprec.h"
25 #include "wx/textctrl.h"
29 #include "wx/generic/gridctrl.h"
30 #include "wx/tokenzr.h"
32 // ----------------------------------------------------------------------------
33 // wxGridCellDateTimeRenderer
34 // ----------------------------------------------------------------------------
38 // Enables a grid cell to display a formated date and or time
40 wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(wxString outformat
, wxString informat
)
43 m_oformat
= outformat
;
44 m_tz
= wxDateTime::Local
;
45 m_dateDef
= wxDefaultDateTime
;
48 wxGridCellRenderer
*wxGridCellDateTimeRenderer::Clone() const
50 wxGridCellDateTimeRenderer
*renderer
= new wxGridCellDateTimeRenderer
;
51 renderer
->m_iformat
= m_iformat
;
52 renderer
->m_oformat
= m_oformat
;
53 renderer
->m_dateDef
= m_dateDef
;
54 renderer
->m_tz
= m_tz
;
59 wxString
wxGridCellDateTimeRenderer::GetString(wxGrid
& grid
, int row
, int col
)
61 wxGridTableBase
*table
= grid
.GetTable();
63 bool hasDatetime
= FALSE
;
66 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_DATETIME
) )
68 void * tempval
= table
->GetValueAsCustom(row
, col
,wxGRID_VALUE_DATETIME
);
71 val
= *((wxDateTime
*)tempval
);
73 delete (wxDateTime
*)tempval
;
80 text
= table
->GetValue(row
, col
);
81 hasDatetime
= (val
.ParseFormat( text
, m_iformat
, m_dateDef
) != (wxChar
*)NULL
) ;
85 text
= val
.Format(m_oformat
, m_tz
);
87 //If we faild to parse string just show what we where given?
91 void wxGridCellDateTimeRenderer::Draw(wxGrid
& grid
,
94 const wxRect
& rectCell
,
98 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
100 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
102 // draw the text right aligned by default
104 attr
.GetAlignment(&hAlign
, &vAlign
);
107 wxRect rect
= rectCell
;
110 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
113 wxSize
wxGridCellDateTimeRenderer::GetBestSize(wxGrid
& grid
,
114 wxGridCellAttr
& attr
,
118 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
121 void wxGridCellDateTimeRenderer::SetParameters(const wxString
& params
){
122 if (!params
.IsEmpty())
126 #endif // wxUSE_DATETIME
128 // ----------------------------------------------------------------------------
129 // wxGridCellChoiceNumberRenderer
130 // ----------------------------------------------------------------------------
131 // Renders a number as a textual equivalent.
132 // eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob"
135 wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString
& choices
)
137 if (!choices
.IsEmpty())
138 SetParameters(choices
);
141 wxGridCellRenderer
*wxGridCellEnumRenderer::Clone() const
143 wxGridCellEnumRenderer
*renderer
= new wxGridCellEnumRenderer
;
144 renderer
->m_choices
= m_choices
;
148 wxString
wxGridCellEnumRenderer::GetString(wxGrid
& grid
, int row
, int col
)
150 wxGridTableBase
*table
= grid
.GetTable();
152 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
154 int choiceno
= table
->GetValueAsLong(row
, col
);
155 text
.Printf(_T("%s"), m_choices
[ choiceno
].c_str() );
159 text
= table
->GetValue(row
, col
);
163 //If we faild to parse string just show what we where given?
167 void wxGridCellEnumRenderer::Draw(wxGrid
& grid
,
168 wxGridCellAttr
& attr
,
170 const wxRect
& rectCell
,
174 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
176 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
178 // draw the text right aligned by default
180 attr
.GetAlignment(&hAlign
, &vAlign
);
183 wxRect rect
= rectCell
;
186 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
189 wxSize
wxGridCellEnumRenderer::GetBestSize(wxGrid
& grid
,
190 wxGridCellAttr
& attr
,
194 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
197 void wxGridCellEnumRenderer::SetParameters(const wxString
& params
)
207 wxStringTokenizer
tk(params
, _T(','));
208 while ( tk
.HasMoreTokens() )
210 m_choices
.Add(tk
.GetNextToken());
216 // ----------------------------------------------------------------------------
217 // wxGridCellEnumEditor
218 // ----------------------------------------------------------------------------
220 // A cell editor which displays an enum number as a textual equivalent. eg
221 // data in cell is 0,1,2 ... n the cell could be displayed as
222 // "John","Fred"..."Bob" in the combo choice box
224 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
225 : wxGridCellChoiceEditor()
229 if (!choices
.IsEmpty())
230 SetParameters(choices
);
233 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
235 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
236 editor
->m_startint
= m_startint
;
240 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
242 wxASSERT_MSG(m_control
,
243 wxT("The wxGridCellEnumEditor must be Created first!"));
245 wxGridTableBase
*table
= grid
->GetTable();
247 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
249 m_startint
= table
->GetValueAsLong(row
, col
);
253 wxString startValue
= table
->GetValue(row
, col
);
254 if (startValue
.IsNumber() && !startValue
.IsEmpty())
256 startValue
.ToLong(&m_startint
);
264 Combo()->SetSelection(m_startint
);
265 Combo()->SetInsertionPointEnd();
270 bool wxGridCellEnumEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
272 int pos
= Combo()->GetSelection();
273 bool changed
= (pos
!= m_startint
);
276 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
277 grid
->GetTable()->SetValueAsLong(row
, col
, pos
);
279 grid
->GetTable()->SetValue(row
, col
,wxString::Format(wxT("%i"),pos
));
285 #endif // wxUSE_COMBOBOX
287 // ----------------------------------------------------------------------------
288 // wxGridCellAutoWrapStringEditor
289 // ----------------------------------------------------------------------------
292 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
294 wxEvtHandler
* evtHandler
)
296 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
297 wxDefaultPosition
, wxDefaultSize
,
298 wxTE_MULTILINE
| wxTE_RICH
);
301 wxGridCellEditor::Create(parent
, id
, evtHandler
);
305 wxGridCellAutoWrapStringRenderer::Draw(wxGrid
& grid
,
306 wxGridCellAttr
& attr
,
308 const wxRect
& rectCell
,
313 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
315 // now we only have to draw the text
316 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
318 int horizAlign
, vertAlign
;
319 attr
.GetAlignment(&horizAlign
, &vertAlign
);
321 wxRect rect
= rectCell
;
324 grid
.DrawTextRectangle(dc
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
),
325 rect
, horizAlign
, vertAlign
);
330 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid
& grid
,
332 wxGridCellAttr
& attr
,
336 wxString data
= grid
.GetCellValue(row
, col
);
339 dc
.SetFont(attr
.GetFont());
341 //Taken from wxGrid again!
342 wxCoord x
= 0, y
= 0, curr_x
= 0;
343 wxCoord max_x
= rect
.GetWidth();
345 dc
.SetFont(attr
.GetFont());
346 wxStringTokenizer
tk(data
, _T(" \n\t\r"));
347 wxString
thisline(wxT(""));
349 while ( tk
.HasMoreTokens() )
351 wxString tok
= tk
.GetNextToken();
352 //FIXME: this causes us to print an extra unnecesary
353 // space at the end of the line. But it
354 // is invisible , simplifies the size calculation
355 // and ensures tokens are seperated in the display
358 dc
.GetTextExtent(tok
, &x
, &y
);
359 if ( curr_x
+ x
> max_x
) {
360 lines
.Add( wxString(thisline
) );
370 lines
.Add( wxString(thisline
) );
377 wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid
& grid
,
378 wxGridCellAttr
& attr
,
382 wxCoord x
,y
, height
, width
= grid
.GetColSize(col
) -10;
383 int count
= 250; //Limit iterations..
385 wxRect
rect(0,0,width
,10);
387 // M is a nice large character 'y' gives descender!.
388 dc
.GetTextExtent(wxT("My"), &x
, &y
);
393 rect
.SetWidth(width
);
394 height
= y
*( GetTextLines(grid
,dc
,attr
,rect
,row
,col
).GetCount());
396 // Search for a shape no taller than the golden ratio.
397 } while (count
&& (width
< (height
*1.68)) );
400 return wxSize(width
,height
);