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
){
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
)
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()
219 SetParameters(choices
);
222 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
224 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
225 editor
->m_startint
= m_startint
;
229 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
231 wxASSERT_MSG(m_control
,
232 wxT("The wxGridCellEnumEditor must be Created first!"));
234 wxGridTableBase
*table
= grid
->GetTable();
236 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
238 m_startint
= table
->GetValueAsLong(row
, col
);
242 wxString startValue
= table
->GetValue(row
, col
);
243 if (startValue
.IsNumber() && !startValue
.IsEmpty())
245 startValue
.ToLong(&m_startint
);
253 Combo()->SetSelection(m_startint
);
254 Combo()->SetInsertionPointEnd();
259 bool wxGridCellEnumEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
261 int pos
= Combo()->GetSelection();
262 bool changed
= (pos
!= m_startint
);
265 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
266 grid
->GetTable()->SetValueAsLong(row
, col
, pos
);
268 grid
->GetTable()->SetValue(row
, col
,wxString::Format(wxT("%i"),pos
));
276 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
278 wxEvtHandler
* evtHandler
)
280 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
281 wxDefaultPosition
, wxDefaultSize
,
282 wxTE_MULTILINE
| wxTE_RICH
);
285 wxGridCellEditor::Create(parent
, id
, evtHandler
);
289 wxGridCellAutoWrapStringRenderer::Draw(wxGrid
& grid
,
290 wxGridCellAttr
& attr
,
292 const wxRect
& rectCell
,
297 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
299 // now we only have to draw the text
300 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
302 int horizAlign
, vertAlign
;
303 attr
.GetAlignment(&horizAlign
, &vertAlign
);
305 wxRect rect
= rectCell
;
308 grid
.DrawTextRectangle(dc
, GetTextLines(grid
,dc
,attr
,rect
,row
,col
),
309 rect
, horizAlign
, vertAlign
);
314 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid
& grid
,
316 wxGridCellAttr
& attr
,
320 wxString data
= grid
.GetCellValue(row
, col
);
323 dc
.SetFont(attr
.GetFont());
325 //Taken from wxGrid again!
326 wxCoord x
= 0, y
= 0, curr_x
= 0;
327 wxCoord max_x
= rect
.GetWidth();
329 dc
.SetFont(attr
.GetFont());
330 wxStringTokenizer
tk(data
, _T(" \n\t\r"));
331 wxString
thisline("");
333 while ( tk
.HasMoreTokens() )
335 wxString tok
= tk
.GetNextToken();
336 //FIXME: this causes us to print an extra unnecesary
337 // space at the end of the line. But it
338 // is invisible , simplifies the size calculation
339 // and ensures tokens are seperated in the display
342 dc
.GetTextExtent(tok
, &x
, &y
);
343 if ( curr_x
+ x
> max_x
) {
344 lines
.Add( wxString(thisline
) );
354 lines
.Add( wxString(thisline
) );
361 wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid
& grid
,
362 wxGridCellAttr
& attr
,
366 int x
,y
, height
, width
= grid
.GetColSize(col
) -10;
367 int count
= 250; //Limit iterations..
369 wxRect
rect(0,0,width
,10);
371 // M is a nice large character 'y' gives descender!.
372 dc
.GetTextExtent("My", &x
, &y
);
377 rect
.SetWidth(width
);
378 height
= y
*( GetTextLines(grid
,dc
,attr
,rect
,row
,col
).GetCount());
380 // Search for a shape no taller than the golden ratio.
381 } while (count
&& (width
< (height
*1.68)) );
384 return wxSize(width
,height
);