1
///////////////////////////////////////////////////////////////////////////
2
// Name: generic/gridctrl.cpp
3
// Purpose: wxGrid controls
4
// Author: Paul Gammans, Roger Gammans
7
// RCS-ID: $Id: gridctrl.cpp,v 1.17.2.1 2005/10/18 14:33:33 MW Exp $
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)
127
#endif // wxUSE_DATETIME
129
// ----------------------------------------------------------------------------
130
// wxGridCellChoiceNumberRenderer
131
// ----------------------------------------------------------------------------
132
// Renders a number as a textual equivalent.
133
// eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob"
136
wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString& choices)
138
if (!choices.empty())
139
SetParameters(choices);
142
wxGridCellRenderer *wxGridCellEnumRenderer::Clone() const
144
wxGridCellEnumRenderer *renderer = new wxGridCellEnumRenderer;
145
renderer->m_choices = m_choices;
149
wxString wxGridCellEnumRenderer::GetString(wxGrid& grid, int row, int col)
151
wxGridTableBase *table = grid.GetTable();
153
if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
155
int choiceno = table->GetValueAsLong(row, col);
156
text.Printf(_T("%s"), m_choices[ choiceno ].c_str() );
160
text = table->GetValue(row, col);
164
//If we faild to parse string just show what we where given?
168
void wxGridCellEnumRenderer::Draw(wxGrid& grid,
169
wxGridCellAttr& attr,
171
const wxRect& rectCell,
175
wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
177
SetTextColoursAndFont(grid, attr, dc, isSelected);
179
// draw the text right aligned by default
181
attr.GetAlignment(&hAlign, &vAlign);
184
wxRect rect = rectCell;
187
grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
190
wxSize wxGridCellEnumRenderer::GetBestSize(wxGrid& grid,
191
wxGridCellAttr& attr,
195
return DoGetBestSize(attr, dc, GetString(grid, row, col));
198
void wxGridCellEnumRenderer::SetParameters(const wxString& params)
208
wxStringTokenizer tk(params, _T(','));
209
while ( tk.HasMoreTokens() )
211
m_choices.Add(tk.GetNextToken());
217
// ----------------------------------------------------------------------------
218
// wxGridCellEnumEditor
219
// ----------------------------------------------------------------------------
221
// A cell editor which displays an enum number as a textual equivalent. eg
222
// data in cell is 0,1,2 ... n the cell could be displayed as
223
// "John","Fred"..."Bob" in the combo choice box
225
wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString& choices)
226
:wxGridCellChoiceEditor()
230
if (!choices.empty())
231
SetParameters(choices);
234
wxGridCellEditor *wxGridCellEnumEditor::Clone() const
236
wxGridCellEnumEditor *editor = new wxGridCellEnumEditor();
237
editor->m_startint = m_startint;
241
void wxGridCellEnumEditor::BeginEdit(int row, int col, wxGrid* grid)
243
wxASSERT_MSG(m_control,
244
wxT("The wxGridCellEnumEditor must be Created first!"));
246
wxGridTableBase *table = grid->GetTable();
248
if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
250
m_startint = table->GetValueAsLong(row, col);
254
wxString startValue = table->GetValue(row, col);
255
if (startValue.IsNumber() && !startValue.empty())
257
startValue.ToLong(&m_startint);
265
Combo()->SetSelection(m_startint);
266
Combo()->SetInsertionPointEnd();
271
bool wxGridCellEnumEditor::EndEdit(int row, int col, wxGrid* grid)
273
int pos = Combo()->GetSelection();
274
bool changed = (pos != m_startint);
277
if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
278
grid->GetTable()->SetValueAsLong(row, col, pos);
280
grid->GetTable()->SetValue(row, col,wxString::Format(wxT("%i"),pos));
286
#endif // wxUSE_COMBOBOX
288
// ----------------------------------------------------------------------------
289
// wxGridCellAutoWrapStringEditor
290
// ----------------------------------------------------------------------------
293
wxGridCellAutoWrapStringEditor::Create(wxWindow* parent,
295
wxEvtHandler* evtHandler)
297
m_control = new wxTextCtrl(parent, id, wxEmptyString,
298
wxDefaultPosition, wxDefaultSize,
299
wxTE_MULTILINE | wxTE_RICH);
302
wxGridCellEditor::Create(parent, id, evtHandler);
306
wxGridCellAutoWrapStringRenderer::Draw(wxGrid& grid,
307
wxGridCellAttr& attr,
309
const wxRect& rectCell,
314
wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
316
// now we only have to draw the text
317
SetTextColoursAndFont(grid, attr, dc, isSelected);
319
int horizAlign, vertAlign;
320
attr.GetAlignment(&horizAlign, &vertAlign);
322
wxRect rect = rectCell;
325
grid.DrawTextRectangle(dc, GetTextLines(grid,dc,attr,rect,row,col),
326
rect, horizAlign, vertAlign);
331
wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid& grid,
333
wxGridCellAttr& attr,
337
wxString data = grid.GetCellValue(row, col);
340
dc.SetFont(attr.GetFont());
342
//Taken from wxGrid again!
343
wxCoord x = 0, y = 0, curr_x = 0;
344
wxCoord max_x = rect.GetWidth();
346
dc.SetFont(attr.GetFont());
347
wxStringTokenizer tk(data , _T(" \n\t\r"));
348
wxString thisline = wxEmptyString;
350
while ( tk.HasMoreTokens() )
352
wxString tok = tk.GetNextToken();
353
//FIXME: this causes us to print an extra unnecesary
354
// space at the end of the line. But it
355
// is invisible , simplifies the size calculation
356
// and ensures tokens are separated in the display
359
dc.GetTextExtent(tok, &x, &y);
360
if ( curr_x + x > max_x)
362
lines.Add( wxString(thisline) );
373
lines.Add( wxString(thisline) );
380
wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid& grid,
381
wxGridCellAttr& attr,
385
wxCoord x,y, height , width = grid.GetColSize(col) -10;
386
int count = 250; //Limit iterations..
388
wxRect rect(0,0,width,10);
390
// M is a nice large character 'y' gives descender!.
391
dc.GetTextExtent(wxT("My"), &x, &y);
396
rect.SetWidth(width);
397
height = y * (wx_truncate_cast(wxCoord, GetTextLines(grid,dc,attr,rect,row,col).GetCount()));
399
// Search for a shape no taller than the golden ratio.
400
} while (count && (width < (height*1.68)) );
403
return wxSize(width,height);