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