]> git.saurik.com Git - wxWidgets.git/blob - src/generic/gridctrl.cpp
applied patch for compilation with gcc 3.0
[wxWidgets.git] / src / generic / gridctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/gridctrl.cpp
3 // Purpose: wxGrid controls
4 // Author: Paul Gammans, Roger Gammans
5 // Modified by:
6 // Created: 11/04/2001
7 // RCS-ID: $Id$
8 // Copyright: (c) The Computer Surgery (paul@compsurg.co.uk)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "gridctrl.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/textctrl.h"
24 #include "wx/dc.h"
25 #endif // WX_PRECOMP
26
27 #include "wx/generic/gridctrl.h"
28 #include "wx/tokenzr.h"
29
30 // ----------------------------------------------------------------------------
31 // wxGridCellDateTimeRenderer
32 // ----------------------------------------------------------------------------
33
34 // Enables a grid cell to display a formated date and or time
35
36 wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(wxString outformat, wxString informat)
37 {
38 m_iformat = informat;
39 m_oformat = outformat;
40 m_tz = wxDateTime::Local;
41 m_dateDef = wxDefaultDateTime;
42 }
43
44 wxGridCellRenderer *wxGridCellDateTimeRenderer::Clone() const
45 {
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;
51
52 return renderer;
53 }
54
55 wxString wxGridCellDateTimeRenderer::GetString(wxGrid& grid, int row, int col)
56 {
57 wxGridTableBase *table = grid.GetTable();
58
59 bool hasDatetime = FALSE;
60 wxDateTime val;
61 wxString text;
62 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_DATETIME) )
63 {
64 void * tempval = table->GetValueAsCustom(row, col,wxGRID_VALUE_DATETIME);
65
66 if (tempval){
67 val = *((wxDateTime *)tempval);
68 hasDatetime = TRUE;
69 delete (wxDateTime *)tempval;
70 }
71
72 }
73
74 if (!hasDatetime )
75 {
76 text = table->GetValue(row, col);
77 hasDatetime = (val.ParseFormat( text, m_iformat, m_dateDef ) != (wxChar *)NULL) ;
78 }
79
80 if ( hasDatetime )
81 text = val.Format(m_oformat, m_tz );
82
83 //If we faild to parse string just show what we where given?
84 return text;
85 }
86
87 void wxGridCellDateTimeRenderer::Draw(wxGrid& grid,
88 wxGridCellAttr& attr,
89 wxDC& dc,
90 const wxRect& rectCell,
91 int row, int col,
92 bool isSelected)
93 {
94 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
95
96 SetTextColoursAndFont(grid, attr, dc, isSelected);
97
98 // draw the text right aligned by default
99 int hAlign, vAlign;
100 attr.GetAlignment(&hAlign, &vAlign);
101 hAlign = wxRIGHT;
102
103 wxRect rect = rectCell;
104 rect.Inflate(-1);
105
106 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
107 }
108
109 wxSize wxGridCellDateTimeRenderer::GetBestSize(wxGrid& grid,
110 wxGridCellAttr& attr,
111 wxDC& dc,
112 int row, int col)
113 {
114 return DoGetBestSize(attr, dc, GetString(grid, row, col));
115 }
116
117 void wxGridCellDateTimeRenderer::SetParameters(const wxString& params){
118 if(params)
119 m_oformat=params;
120 }
121
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"
127
128
129 wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString& choices)
130 {
131 if(choices)
132 SetParameters(choices);
133 }
134
135 wxGridCellRenderer *wxGridCellEnumRenderer::Clone() const
136 {
137 wxGridCellEnumRenderer *renderer = new wxGridCellEnumRenderer;
138 renderer->m_choices = m_choices;
139 return renderer;
140 }
141
142 wxString wxGridCellEnumRenderer::GetString(wxGrid& grid, int row, int col)
143 {
144 wxGridTableBase *table = grid.GetTable();
145 wxString text;
146 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
147 {
148 int choiceno = table->GetValueAsLong(row, col);
149 text.Printf(_T("%s"), m_choices[ choiceno ].c_str() );
150 }
151 else
152 {
153 text = table->GetValue(row, col);
154 }
155
156
157 //If we faild to parse string just show what we where given?
158 return text;
159 }
160
161 void wxGridCellEnumRenderer::Draw(wxGrid& grid,
162 wxGridCellAttr& attr,
163 wxDC& dc,
164 const wxRect& rectCell,
165 int row, int col,
166 bool isSelected)
167 {
168 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
169
170 SetTextColoursAndFont(grid, attr, dc, isSelected);
171
172 // draw the text right aligned by default
173 int hAlign, vAlign;
174 attr.GetAlignment(&hAlign, &vAlign);
175 hAlign = wxRIGHT;
176
177 wxRect rect = rectCell;
178 rect.Inflate(-1);
179
180 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
181 }
182
183 wxSize wxGridCellEnumRenderer::GetBestSize(wxGrid& grid,
184 wxGridCellAttr& attr,
185 wxDC& dc,
186 int row, int col)
187 {
188 return DoGetBestSize(attr, dc, GetString(grid, row, col));
189 }
190
191 void wxGridCellEnumRenderer::SetParameters(const wxString& params)
192 {
193 if ( !params )
194 {
195 // what can we do?
196 return;
197 }
198
199 m_choices.Empty();
200
201 wxStringTokenizer tk(params, _T(','));
202 while ( tk.HasMoreTokens() )
203 {
204 m_choices.Add(tk.GetNextToken());
205 }
206 }
207
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
214 //
215 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString& choices)
216 : wxGridCellChoiceEditor()
217 {
218 if(choices)
219 SetParameters(choices);
220 }
221
222 wxGridCellEditor *wxGridCellEnumEditor::Clone() const
223 {
224 wxGridCellEnumEditor *editor = new wxGridCellEnumEditor();
225 editor->m_startint = m_startint;
226 return editor;
227 }
228
229 void wxGridCellEnumEditor::BeginEdit(int row, int col, wxGrid* grid)
230 {
231 wxASSERT_MSG(m_control,
232 wxT("The wxGridCellEnumEditor must be Created first!"));
233
234 wxGridTableBase *table = grid->GetTable();
235
236 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
237 {
238 m_startint = table->GetValueAsLong(row, col);
239 }
240 else
241 {
242 wxString startValue = table->GetValue(row, col);
243 if (startValue.IsNumber() && !startValue.IsEmpty())
244 {
245 startValue.ToLong(&m_startint);
246 }
247 else
248 {
249 m_startint=-1;
250 }
251 }
252
253 Combo()->SetSelection(m_startint);
254 Combo()->SetInsertionPointEnd();
255 Combo()->SetFocus();
256
257 }
258
259 bool wxGridCellEnumEditor::EndEdit(int row, int col, wxGrid* grid)
260 {
261 int pos = Combo()->GetSelection();
262 bool changed = (pos != m_startint);
263 if (changed)
264 {
265 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
266 grid->GetTable()->SetValueAsLong(row, col, pos);
267 else
268 grid->GetTable()->SetValue(row, col,wxString::Format(wxT("%i"),pos));
269 }
270
271 return changed;
272 }
273
274
275 void
276 wxGridCellAutoWrapStringEditor::Create(wxWindow* parent,
277 wxWindowID id,
278 wxEvtHandler* evtHandler)
279 {
280 m_control = new wxTextCtrl(parent, id, wxEmptyString,
281 wxDefaultPosition, wxDefaultSize,
282 wxTE_MULTILINE | wxTE_RICH);
283
284
285 wxGridCellEditor::Create(parent, id, evtHandler);
286 }
287
288 void
289 wxGridCellAutoWrapStringRenderer::Draw(wxGrid& grid,
290 wxGridCellAttr& attr,
291 wxDC& dc,
292 const wxRect& rectCell,
293 int row, int col,
294 bool isSelected) {
295
296
297 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
298
299 // now we only have to draw the text
300 SetTextColoursAndFont(grid, attr, dc, isSelected);
301
302 int horizAlign, vertAlign;
303 attr.GetAlignment(&horizAlign, &vertAlign);
304
305 wxRect rect = rectCell;
306 rect.Inflate(-1);
307
308 grid.DrawTextRectangle(dc, GetTextLines(grid,dc,attr,rect,row,col),
309 rect, horizAlign, vertAlign);
310 }
311
312
313 wxArrayString
314 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid& grid,
315 wxDC& dc,
316 wxGridCellAttr& attr,
317 const wxRect& rect,
318 int row, int col)
319 {
320 wxString data = grid.GetCellValue(row, col);
321
322 wxArrayString lines;
323 dc.SetFont(attr.GetFont());
324
325 //Taken from wxGrid again!
326 wxCoord x = 0, y = 0, curr_x = 0;
327 wxCoord max_x = rect.GetWidth();
328
329 dc.SetFont(attr.GetFont());
330 wxStringTokenizer tk(data , _T(" \n\t\r"));
331 wxString thisline("");
332
333 while ( tk.HasMoreTokens() )
334 {
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
340 tok += _T(" ");
341
342 dc.GetTextExtent(tok, &x, &y);
343 if ( curr_x + x > max_x) {
344 lines.Add( wxString(thisline) );
345 thisline = tok;
346 curr_x=x;
347 } else {
348 thisline+= tok;
349 curr_x += x;
350 }
351
352 }
353 //Add last line
354 lines.Add( wxString(thisline) );
355
356 return lines;
357 }
358
359
360 wxSize
361 wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid& grid,
362 wxGridCellAttr& attr,
363 wxDC& dc,
364 int row, int col)
365 {
366 int x,y, height , width = grid.GetColSize(col) -10;
367 int count = 250; //Limit iterations..
368
369 wxRect rect(0,0,width,10);
370
371 // M is a nice large character 'y' gives descender!.
372 dc.GetTextExtent("My", &x, &y);
373
374 do
375 {
376 width+=10;
377 rect.SetWidth(width);
378 height = y *( GetTextLines(grid,dc,attr,rect,row,col).GetCount());
379 count--;
380 // Search for a shape no taller than the golden ratio.
381 } while (count && (width < (height*1.68)) );
382
383
384 return wxSize(width,height);
385 }
386