]>
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) | |
65571936 | 9 | // Licence: wxWindows licence |
d10f4bf9 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
d10f4bf9 VZ |
12 | #include "wx/wxprec.h" |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
f7556ff0 | 18 | #if wxUSE_GRID |
b53dc7d6 | 19 | |
df9fac6d | 20 | #include "wx/generic/gridctrl.h" |
29efc6e4 | 21 | #include "wx/generic/grideditors.h" |
df9fac6d | 22 | |
7ac05a91 VZ |
23 | #ifndef WX_PRECOMP |
24 | #include "wx/textctrl.h" | |
25 | #include "wx/dc.h" | |
df9fac6d | 26 | #include "wx/combobox.h" |
03647350 | 27 | #include "wx/settings.h" |
85fe9f6f FM |
28 | #include "wx/log.h" |
29 | #include "wx/checkbox.h" | |
7ac05a91 VZ |
30 | #endif // WX_PRECOMP |
31 | ||
d10f4bf9 | 32 | #include "wx/tokenzr.h" |
29efc6e4 | 33 | #include "wx/renderer.h" |
d10f4bf9 | 34 | |
f8d0234d FM |
35 | |
36 | // ---------------------------------------------------------------------------- | |
37 | // wxGridCellRenderer | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | void wxGridCellRenderer::Draw(wxGrid& grid, | |
41 | wxGridCellAttr& attr, | |
42 | wxDC& dc, | |
43 | const wxRect& rect, | |
44 | int WXUNUSED(row), int WXUNUSED(col), | |
45 | bool isSelected) | |
46 | { | |
47 | dc.SetBackgroundMode( wxBRUSHSTYLE_SOLID ); | |
48 | ||
49 | wxColour clr; | |
01a65e7c | 50 | if ( grid.IsThisEnabled() ) |
f8d0234d FM |
51 | { |
52 | if ( isSelected ) | |
53 | { | |
54 | if ( grid.HasFocus() ) | |
55 | clr = grid.GetSelectionBackground(); | |
56 | else | |
57 | clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW); | |
58 | } | |
59 | else | |
60 | { | |
61 | clr = attr.GetBackgroundColour(); | |
62 | } | |
63 | } | |
64 | else // grey out fields if the grid is disabled | |
65 | { | |
66 | clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
67 | } | |
68 | ||
69 | dc.SetBrush(clr); | |
70 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
71 | dc.DrawRectangle(rect); | |
72 | } | |
73 | ||
74 | ||
d10f4bf9 VZ |
75 | // ---------------------------------------------------------------------------- |
76 | // wxGridCellDateTimeRenderer | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
e2b87f38 VZ |
79 | #if wxUSE_DATETIME |
80 | ||
90e572f1 | 81 | // Enables a grid cell to display a formatted date and or time |
d10f4bf9 | 82 | |
fbfb8bcc | 83 | wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(const wxString& outformat, const wxString& informat) |
d10f4bf9 VZ |
84 | { |
85 | m_iformat = informat; | |
86 | m_oformat = outformat; | |
87 | m_tz = wxDateTime::Local; | |
88 | m_dateDef = wxDefaultDateTime; | |
89 | } | |
90 | ||
91 | wxGridCellRenderer *wxGridCellDateTimeRenderer::Clone() const | |
92 | { | |
93 | wxGridCellDateTimeRenderer *renderer = new wxGridCellDateTimeRenderer; | |
94 | renderer->m_iformat = m_iformat; | |
95 | renderer->m_oformat = m_oformat; | |
96 | renderer->m_dateDef = m_dateDef; | |
97 | renderer->m_tz = m_tz; | |
98 | ||
99 | return renderer; | |
100 | } | |
101 | ||
fbfb8bcc | 102 | wxString wxGridCellDateTimeRenderer::GetString(const wxGrid& grid, int row, int col) |
d10f4bf9 VZ |
103 | { |
104 | wxGridTableBase *table = grid.GetTable(); | |
105 | ||
ca65c044 | 106 | bool hasDatetime = false; |
d10f4bf9 VZ |
107 | wxDateTime val; |
108 | wxString text; | |
109 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_DATETIME) ) | |
110 | { | |
111 | void * tempval = table->GetValueAsCustom(row, col,wxGRID_VALUE_DATETIME); | |
112 | ||
6a147dfe VZ |
113 | if (tempval) |
114 | { | |
d10f4bf9 | 115 | val = *((wxDateTime *)tempval); |
ca65c044 | 116 | hasDatetime = true; |
d10f4bf9 VZ |
117 | delete (wxDateTime *)tempval; |
118 | } | |
119 | ||
120 | } | |
121 | ||
122 | if (!hasDatetime ) | |
123 | { | |
124 | text = table->GetValue(row, col); | |
6a147dfe VZ |
125 | const char * const end = val.ParseFormat(text, m_iformat, m_dateDef); |
126 | hasDatetime = end && !*end; | |
d10f4bf9 VZ |
127 | } |
128 | ||
129 | if ( hasDatetime ) | |
130 | text = val.Format(m_oformat, m_tz ); | |
131 | ||
6a147dfe | 132 | // If we failed to parse string just show what we where given? |
d10f4bf9 VZ |
133 | return text; |
134 | } | |
135 | ||
136 | void wxGridCellDateTimeRenderer::Draw(wxGrid& grid, | |
137 | wxGridCellAttr& attr, | |
138 | wxDC& dc, | |
139 | const wxRect& rectCell, | |
140 | int row, int col, | |
141 | bool isSelected) | |
142 | { | |
143 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
144 | ||
145 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
146 | ||
147 | // draw the text right aligned by default | |
cfbc15ee VZ |
148 | int hAlign = wxALIGN_RIGHT, |
149 | vAlign = wxALIGN_INVALID; | |
150 | attr.GetNonDefaultAlignment(&hAlign, &vAlign); | |
d10f4bf9 VZ |
151 | |
152 | wxRect rect = rectCell; | |
153 | rect.Inflate(-1); | |
154 | ||
155 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
156 | } | |
157 | ||
158 | wxSize wxGridCellDateTimeRenderer::GetBestSize(wxGrid& grid, | |
159 | wxGridCellAttr& attr, | |
160 | wxDC& dc, | |
161 | int row, int col) | |
162 | { | |
163 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
164 | } | |
165 | ||
7448de8d WS |
166 | void wxGridCellDateTimeRenderer::SetParameters(const wxString& params) |
167 | { | |
168 | if (!params.empty()) | |
d10f4bf9 VZ |
169 | m_oformat=params; |
170 | } | |
171 | ||
e2b87f38 VZ |
172 | #endif // wxUSE_DATETIME |
173 | ||
d10f4bf9 | 174 | // ---------------------------------------------------------------------------- |
db890987 | 175 | // wxGridCellEnumRenderer |
d10f4bf9 VZ |
176 | // ---------------------------------------------------------------------------- |
177 | // Renders a number as a textual equivalent. | |
178 | // eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob" | |
179 | ||
180 | ||
181 | wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString& choices) | |
182 | { | |
7448de8d | 183 | if (!choices.empty()) |
d10f4bf9 VZ |
184 | SetParameters(choices); |
185 | } | |
186 | ||
187 | wxGridCellRenderer *wxGridCellEnumRenderer::Clone() const | |
188 | { | |
189 | wxGridCellEnumRenderer *renderer = new wxGridCellEnumRenderer; | |
190 | renderer->m_choices = m_choices; | |
191 | return renderer; | |
192 | } | |
193 | ||
fbfb8bcc | 194 | wxString wxGridCellEnumRenderer::GetString(const wxGrid& grid, int row, int col) |
d10f4bf9 VZ |
195 | { |
196 | wxGridTableBase *table = grid.GetTable(); | |
197 | wxString text; | |
198 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
199 | { | |
200 | int choiceno = table->GetValueAsLong(row, col); | |
9a83f860 | 201 | text.Printf(wxT("%s"), m_choices[ choiceno ].c_str() ); |
d10f4bf9 VZ |
202 | } |
203 | else | |
204 | { | |
205 | text = table->GetValue(row, col); | |
206 | } | |
207 | ||
208 | ||
209 | //If we faild to parse string just show what we where given? | |
210 | return text; | |
211 | } | |
212 | ||
213 | void wxGridCellEnumRenderer::Draw(wxGrid& grid, | |
214 | wxGridCellAttr& attr, | |
215 | wxDC& dc, | |
216 | const wxRect& rectCell, | |
217 | int row, int col, | |
218 | bool isSelected) | |
219 | { | |
220 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
221 | ||
222 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
223 | ||
224 | // draw the text right aligned by default | |
cfbc15ee VZ |
225 | int hAlign = wxALIGN_RIGHT, |
226 | vAlign = wxALIGN_INVALID; | |
227 | attr.GetNonDefaultAlignment(&hAlign, &vAlign); | |
d10f4bf9 VZ |
228 | |
229 | wxRect rect = rectCell; | |
230 | rect.Inflate(-1); | |
231 | ||
232 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
233 | } | |
234 | ||
235 | wxSize wxGridCellEnumRenderer::GetBestSize(wxGrid& grid, | |
236 | wxGridCellAttr& attr, | |
237 | wxDC& dc, | |
238 | int row, int col) | |
239 | { | |
240 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
241 | } | |
242 | ||
243 | void wxGridCellEnumRenderer::SetParameters(const wxString& params) | |
244 | { | |
245 | if ( !params ) | |
246 | { | |
247 | // what can we do? | |
248 | return; | |
249 | } | |
250 | ||
251 | m_choices.Empty(); | |
252 | ||
9a83f860 | 253 | wxStringTokenizer tk(params, wxT(',')); |
d10f4bf9 VZ |
254 | while ( tk.HasMoreTokens() ) |
255 | { | |
256 | m_choices.Add(tk.GetNextToken()); | |
257 | } | |
258 | } | |
259 | ||
3a8c693a | 260 | |
d10f4bf9 | 261 | // ---------------------------------------------------------------------------- |
29efc6e4 | 262 | // wxGridCellAutoWrapStringRenderer |
d10f4bf9 | 263 | // ---------------------------------------------------------------------------- |
3a8c693a | 264 | |
d10f4bf9 VZ |
265 | |
266 | void | |
267 | wxGridCellAutoWrapStringRenderer::Draw(wxGrid& grid, | |
268 | wxGridCellAttr& attr, | |
269 | wxDC& dc, | |
270 | const wxRect& rectCell, | |
271 | int row, int col, | |
272 | bool isSelected) { | |
273 | ||
274 | ||
275 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
276 | ||
277 | // now we only have to draw the text | |
278 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
279 | ||
280 | int horizAlign, vertAlign; | |
281 | attr.GetAlignment(&horizAlign, &vertAlign); | |
282 | ||
283 | wxRect rect = rectCell; | |
284 | rect.Inflate(-1); | |
285 | ||
286 | grid.DrawTextRectangle(dc, GetTextLines(grid,dc,attr,rect,row,col), | |
287 | rect, horizAlign, vertAlign); | |
288 | } | |
289 | ||
290 | ||
291 | wxArrayString | |
292 | wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid& grid, | |
293 | wxDC& dc, | |
fbfb8bcc | 294 | const wxGridCellAttr& attr, |
d10f4bf9 VZ |
295 | const wxRect& rect, |
296 | int row, int col) | |
297 | { | |
298 | wxString data = grid.GetCellValue(row, col); | |
299 | ||
300 | wxArrayString lines; | |
301 | dc.SetFont(attr.GetFont()); | |
302 | ||
303 | //Taken from wxGrid again! | |
304 | wxCoord x = 0, y = 0, curr_x = 0; | |
305 | wxCoord max_x = rect.GetWidth(); | |
306 | ||
307 | dc.SetFont(attr.GetFont()); | |
9a83f860 | 308 | wxStringTokenizer tk(data , wxT(" \n\t\r")); |
ca65c044 | 309 | wxString thisline = wxEmptyString; |
d10f4bf9 VZ |
310 | |
311 | while ( tk.HasMoreTokens() ) | |
312 | { | |
313 | wxString tok = tk.GetNextToken(); | |
7448de8d WS |
314 | //FIXME: this causes us to print an extra unnecesary |
315 | // space at the end of the line. But it | |
316 | // is invisible , simplifies the size calculation | |
317 | // and ensures tokens are separated in the display | |
9a83f860 | 318 | tok += wxT(" "); |
d10f4bf9 VZ |
319 | |
320 | dc.GetTextExtent(tok, &x, &y); | |
7448de8d WS |
321 | if ( curr_x + x > max_x) |
322 | { | |
5c3a7f71 VZ |
323 | if ( curr_x == 0 ) |
324 | { | |
325 | // this means that a single token is wider than the maximal | |
326 | // width -- still use it as is as we need to show at least the | |
327 | // part of it which fits | |
328 | lines.Add(tok); | |
329 | } | |
330 | else | |
331 | { | |
332 | lines.Add(thisline); | |
333 | thisline = tok; | |
334 | curr_x = x; | |
335 | } | |
7448de8d WS |
336 | } |
337 | else | |
338 | { | |
d10f4bf9 | 339 | thisline+= tok; |
7448de8d WS |
340 | curr_x += x; |
341 | } | |
d10f4bf9 VZ |
342 | } |
343 | //Add last line | |
344 | lines.Add( wxString(thisline) ); | |
345 | ||
346 | return lines; | |
347 | } | |
348 | ||
349 | ||
350 | wxSize | |
351 | wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid& grid, | |
352 | wxGridCellAttr& attr, | |
353 | wxDC& dc, | |
354 | int row, int col) | |
355 | { | |
86647b53 SN |
356 | wxCoord x,y, height , width = grid.GetColSize(col) -20; |
357 | // for width, subtract 20 because ColSize includes a magin of 10 pixels | |
358 | // that we do not want here and because we always start with an increment | |
359 | // by 10 in the loop below. | |
d10f4bf9 VZ |
360 | int count = 250; //Limit iterations.. |
361 | ||
362 | wxRect rect(0,0,width,10); | |
363 | ||
364 | // M is a nice large character 'y' gives descender!. | |
8dd8f875 | 365 | dc.GetTextExtent(wxT("My"), &x, &y); |
d10f4bf9 VZ |
366 | |
367 | do | |
368 | { | |
369 | width+=10; | |
370 | rect.SetWidth(width); | |
4a10ea8b | 371 | height = y * (wx_truncate_cast(wxCoord, GetTextLines(grid,dc,attr,rect,row,col).GetCount())); |
d10f4bf9 VZ |
372 | count--; |
373 | // Search for a shape no taller than the golden ratio. | |
374 | } while (count && (width < (height*1.68)) ); | |
375 | ||
376 | ||
377 | return wxSize(width,height); | |
378 | } | |
379 | ||
29efc6e4 | 380 | |
29efc6e4 FM |
381 | // ---------------------------------------------------------------------------- |
382 | // wxGridCellStringRenderer | |
383 | // ---------------------------------------------------------------------------- | |
384 | ||
385 | void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid, | |
386 | const wxGridCellAttr& attr, | |
387 | wxDC& dc, | |
388 | bool isSelected) | |
389 | { | |
390 | dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT ); | |
391 | ||
392 | // TODO some special colours for attr.IsReadOnly() case? | |
393 | ||
394 | // different coloured text when the grid is disabled | |
01a65e7c | 395 | if ( grid.IsThisEnabled() ) |
29efc6e4 FM |
396 | { |
397 | if ( isSelected ) | |
398 | { | |
399 | wxColour clr; | |
400 | if ( grid.HasFocus() ) | |
401 | clr = grid.GetSelectionBackground(); | |
402 | else | |
403 | clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW); | |
404 | dc.SetTextBackground( clr ); | |
405 | dc.SetTextForeground( grid.GetSelectionForeground() ); | |
406 | } | |
407 | else | |
408 | { | |
409 | dc.SetTextBackground( attr.GetBackgroundColour() ); | |
410 | dc.SetTextForeground( attr.GetTextColour() ); | |
411 | } | |
412 | } | |
413 | else | |
414 | { | |
415 | dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); | |
416 | dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT)); | |
417 | } | |
418 | ||
419 | dc.SetFont( attr.GetFont() ); | |
420 | } | |
421 | ||
422 | wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr, | |
423 | wxDC& dc, | |
424 | const wxString& text) | |
425 | { | |
426 | wxCoord x = 0, y = 0, max_x = 0; | |
427 | dc.SetFont(attr.GetFont()); | |
9a83f860 | 428 | wxStringTokenizer tk(text, wxT('\n')); |
29efc6e4 FM |
429 | while ( tk.HasMoreTokens() ) |
430 | { | |
431 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); | |
432 | max_x = wxMax(max_x, x); | |
433 | } | |
434 | ||
435 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. | |
436 | ||
437 | return wxSize(max_x, y); | |
438 | } | |
439 | ||
440 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, | |
441 | wxGridCellAttr& attr, | |
442 | wxDC& dc, | |
443 | int row, int col) | |
444 | { | |
445 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); | |
446 | } | |
447 | ||
448 | void wxGridCellStringRenderer::Draw(wxGrid& grid, | |
449 | wxGridCellAttr& attr, | |
450 | wxDC& dc, | |
451 | const wxRect& rectCell, | |
452 | int row, int col, | |
453 | bool isSelected) | |
454 | { | |
455 | wxRect rect = rectCell; | |
456 | rect.Inflate(-1); | |
457 | ||
458 | // erase only this cells background, overflow cells should have been erased | |
459 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
460 | ||
461 | int hAlign, vAlign; | |
462 | attr.GetAlignment(&hAlign, &vAlign); | |
463 | ||
464 | int overflowCols = 0; | |
465 | ||
466 | if (attr.GetOverflow()) | |
467 | { | |
468 | int cols = grid.GetNumberCols(); | |
469 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); | |
470 | int cell_rows, cell_cols; | |
471 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <= 0 | |
472 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) | |
473 | { | |
474 | int i, c_cols, c_rows; | |
475 | for (i = col+cell_cols; i < cols; i++) | |
476 | { | |
477 | bool is_empty = true; | |
478 | for (int j=row; j < row + cell_rows; j++) | |
479 | { | |
480 | // check w/ anchor cell for multicell block | |
481 | grid.GetCellSize(j, i, &c_rows, &c_cols); | |
482 | if (c_rows > 0) | |
483 | c_rows = 0; | |
484 | if (!grid.GetTable()->IsEmptyCell(j + c_rows, i)) | |
485 | { | |
486 | is_empty = false; | |
487 | break; | |
488 | } | |
489 | } | |
490 | ||
491 | if (is_empty) | |
492 | { | |
493 | rect.width += grid.GetColSize(i); | |
494 | } | |
495 | else | |
496 | { | |
497 | i--; | |
498 | break; | |
499 | } | |
500 | ||
501 | if (rect.width >= best_width) | |
502 | break; | |
503 | } | |
504 | ||
505 | overflowCols = i - col - cell_cols + 1; | |
506 | if (overflowCols >= cols) | |
507 | overflowCols = cols - 1; | |
508 | } | |
509 | ||
510 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight | |
511 | { | |
512 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned | |
513 | wxRect clip = rect; | |
514 | clip.x += rectCell.width; | |
515 | // draw each overflow cell individually | |
516 | int col_end = col + cell_cols + overflowCols; | |
517 | if (col_end >= grid.GetNumberCols()) | |
518 | col_end = grid.GetNumberCols() - 1; | |
519 | for (int i = col + cell_cols; i <= col_end; i++) | |
520 | { | |
521 | clip.width = grid.GetColSize(i) - 1; | |
522 | dc.DestroyClippingRegion(); | |
523 | dc.SetClippingRegion(clip); | |
524 | ||
525 | SetTextColoursAndFont(grid, attr, dc, | |
526 | grid.IsInSelection(row,i)); | |
527 | ||
528 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), | |
529 | rect, hAlign, vAlign); | |
530 | clip.x += grid.GetColSize(i) - 1; | |
531 | } | |
532 | ||
533 | rect = rectCell; | |
534 | rect.Inflate(-1); | |
535 | rect.width++; | |
536 | dc.DestroyClippingRegion(); | |
537 | } | |
538 | } | |
539 | ||
540 | // now we only have to draw the text | |
541 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
542 | ||
543 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), | |
544 | rect, hAlign, vAlign); | |
545 | } | |
546 | ||
547 | // ---------------------------------------------------------------------------- | |
548 | // wxGridCellNumberRenderer | |
549 | // ---------------------------------------------------------------------------- | |
550 | ||
551 | wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col) | |
552 | { | |
553 | wxGridTableBase *table = grid.GetTable(); | |
554 | wxString text; | |
555 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) | |
556 | { | |
9a83f860 | 557 | text.Printf(wxT("%ld"), table->GetValueAsLong(row, col)); |
29efc6e4 FM |
558 | } |
559 | else | |
560 | { | |
561 | text = table->GetValue(row, col); | |
562 | } | |
563 | ||
564 | return text; | |
565 | } | |
566 | ||
567 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, | |
568 | wxGridCellAttr& attr, | |
569 | wxDC& dc, | |
570 | const wxRect& rectCell, | |
571 | int row, int col, | |
572 | bool isSelected) | |
573 | { | |
574 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
575 | ||
576 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
577 | ||
578 | // draw the text right aligned by default | |
cfbc15ee VZ |
579 | int hAlign = wxALIGN_RIGHT, |
580 | vAlign = wxALIGN_INVALID; | |
581 | attr.GetNonDefaultAlignment(&hAlign, &vAlign); | |
29efc6e4 FM |
582 | |
583 | wxRect rect = rectCell; | |
584 | rect.Inflate(-1); | |
585 | ||
586 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
587 | } | |
588 | ||
589 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, | |
590 | wxGridCellAttr& attr, | |
591 | wxDC& dc, | |
592 | int row, int col) | |
593 | { | |
594 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
595 | } | |
596 | ||
597 | // ---------------------------------------------------------------------------- | |
598 | // wxGridCellFloatRenderer | |
599 | // ---------------------------------------------------------------------------- | |
600 | ||
601 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) | |
602 | { | |
603 | SetWidth(width); | |
604 | SetPrecision(precision); | |
605 | } | |
606 | ||
607 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const | |
608 | { | |
609 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; | |
610 | renderer->m_width = m_width; | |
611 | renderer->m_precision = m_precision; | |
612 | renderer->m_format = m_format; | |
613 | ||
614 | return renderer; | |
615 | } | |
616 | ||
617 | wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col) | |
618 | { | |
619 | wxGridTableBase *table = grid.GetTable(); | |
620 | ||
621 | bool hasDouble; | |
622 | double val; | |
623 | wxString text; | |
624 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) | |
625 | { | |
626 | val = table->GetValueAsDouble(row, col); | |
627 | hasDouble = true; | |
628 | } | |
629 | else | |
630 | { | |
631 | text = table->GetValue(row, col); | |
632 | hasDouble = text.ToDouble(&val); | |
633 | } | |
634 | ||
635 | if ( hasDouble ) | |
636 | { | |
637 | if ( !m_format ) | |
638 | { | |
639 | if ( m_width == -1 ) | |
640 | { | |
641 | if ( m_precision == -1 ) | |
642 | { | |
643 | // default width/precision | |
9a83f860 | 644 | m_format = wxT("%f"); |
29efc6e4 FM |
645 | } |
646 | else | |
647 | { | |
9a83f860 | 648 | m_format.Printf(wxT("%%.%df"), m_precision); |
29efc6e4 FM |
649 | } |
650 | } | |
651 | else if ( m_precision == -1 ) | |
652 | { | |
653 | // default precision | |
9a83f860 | 654 | m_format.Printf(wxT("%%%d.f"), m_width); |
29efc6e4 FM |
655 | } |
656 | else | |
657 | { | |
9a83f860 | 658 | m_format.Printf(wxT("%%%d.%df"), m_width, m_precision); |
29efc6e4 FM |
659 | } |
660 | } | |
661 | ||
662 | text.Printf(m_format, val); | |
663 | ||
664 | } | |
665 | //else: text already contains the string | |
666 | ||
667 | return text; | |
668 | } | |
669 | ||
670 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, | |
671 | wxGridCellAttr& attr, | |
672 | wxDC& dc, | |
673 | const wxRect& rectCell, | |
674 | int row, int col, | |
675 | bool isSelected) | |
676 | { | |
677 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); | |
678 | ||
679 | SetTextColoursAndFont(grid, attr, dc, isSelected); | |
680 | ||
681 | // draw the text right aligned by default | |
cfbc15ee VZ |
682 | int hAlign = wxALIGN_RIGHT, |
683 | vAlign = wxALIGN_INVALID; | |
684 | attr.GetNonDefaultAlignment(&hAlign, &vAlign); | |
29efc6e4 FM |
685 | |
686 | wxRect rect = rectCell; | |
687 | rect.Inflate(-1); | |
688 | ||
689 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); | |
690 | } | |
691 | ||
692 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, | |
693 | wxGridCellAttr& attr, | |
694 | wxDC& dc, | |
695 | int row, int col) | |
696 | { | |
697 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); | |
698 | } | |
699 | ||
700 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) | |
701 | { | |
702 | if ( !params ) | |
703 | { | |
704 | // reset to defaults | |
705 | SetWidth(-1); | |
706 | SetPrecision(-1); | |
707 | } | |
708 | else | |
709 | { | |
9a83f860 | 710 | wxString tmp = params.BeforeFirst(wxT(',')); |
29efc6e4 FM |
711 | if ( !tmp.empty() ) |
712 | { | |
713 | long width; | |
714 | if ( tmp.ToLong(&width) ) | |
715 | { | |
716 | SetWidth((int)width); | |
717 | } | |
718 | else | |
719 | { | |
9a83f860 | 720 | wxLogDebug(wxT("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); |
29efc6e4 FM |
721 | } |
722 | } | |
723 | ||
9a83f860 | 724 | tmp = params.AfterFirst(wxT(',')); |
29efc6e4 FM |
725 | if ( !tmp.empty() ) |
726 | { | |
727 | long precision; | |
728 | if ( tmp.ToLong(&precision) ) | |
729 | { | |
730 | SetPrecision((int)precision); | |
731 | } | |
732 | else | |
733 | { | |
9a83f860 | 734 | wxLogDebug(wxT("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); |
29efc6e4 FM |
735 | } |
736 | } | |
737 | } | |
738 | } | |
739 | ||
740 | // ---------------------------------------------------------------------------- | |
741 | // wxGridCellBoolRenderer | |
742 | // ---------------------------------------------------------------------------- | |
743 | ||
744 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; | |
745 | ||
191e43fd | 746 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, |
29efc6e4 FM |
747 | wxGridCellAttr& WXUNUSED(attr), |
748 | wxDC& WXUNUSED(dc), | |
749 | int WXUNUSED(row), | |
750 | int WXUNUSED(col)) | |
751 | { | |
752 | // compute it only once (no locks for MT safeness in GUI thread...) | |
753 | if ( !ms_sizeCheckMark.x ) | |
754 | { | |
191e43fd | 755 | ms_sizeCheckMark = wxRendererNative::Get().GetCheckBoxSize(&grid); |
29efc6e4 FM |
756 | } |
757 | ||
758 | return ms_sizeCheckMark; | |
759 | } | |
760 | ||
761 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, | |
762 | wxGridCellAttr& attr, | |
763 | wxDC& dc, | |
764 | const wxRect& rect, | |
765 | int row, int col, | |
766 | bool isSelected) | |
767 | { | |
768 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); | |
769 | ||
770 | // draw a check mark in the centre (ignoring alignment - TODO) | |
771 | wxSize size = GetBestSize(grid, attr, dc, row, col); | |
772 | ||
773 | // don't draw outside the cell | |
774 | wxCoord minSize = wxMin(rect.width, rect.height); | |
775 | if ( size.x >= minSize || size.y >= minSize ) | |
776 | { | |
777 | // and even leave (at least) 1 pixel margin | |
778 | size.x = size.y = minSize; | |
779 | } | |
780 | ||
781 | // draw a border around checkmark | |
782 | int vAlign, hAlign; | |
783 | attr.GetAlignment(&hAlign, &vAlign); | |
784 | ||
785 | wxRect rectBorder; | |
786 | if (hAlign == wxALIGN_CENTRE) | |
787 | { | |
788 | rectBorder.x = rect.x + rect.width / 2 - size.x / 2; | |
789 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
790 | rectBorder.width = size.x; | |
791 | rectBorder.height = size.y; | |
792 | } | |
793 | else if (hAlign == wxALIGN_LEFT) | |
794 | { | |
795 | rectBorder.x = rect.x + 2; | |
796 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
797 | rectBorder.width = size.x; | |
798 | rectBorder.height = size.y; | |
799 | } | |
800 | else if (hAlign == wxALIGN_RIGHT) | |
801 | { | |
802 | rectBorder.x = rect.x + rect.width - size.x - 2; | |
803 | rectBorder.y = rect.y + rect.height / 2 - size.y / 2; | |
804 | rectBorder.width = size.x; | |
805 | rectBorder.height = size.y; | |
806 | } | |
807 | ||
808 | bool value; | |
809 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) | |
810 | { | |
811 | value = grid.GetTable()->GetValueAsBool(row, col); | |
812 | } | |
813 | else | |
814 | { | |
815 | wxString cellval( grid.GetTable()->GetValue(row, col) ); | |
816 | value = wxGridCellBoolEditor::IsTrueValue(cellval); | |
817 | } | |
818 | ||
819 | int flags = 0; | |
820 | if (value) | |
821 | flags |= wxCONTROL_CHECKED; | |
822 | ||
823 | wxRendererNative::Get().DrawCheckBox( &grid, dc, rectBorder, flags ); | |
824 | } | |
825 | ||
3a8c693a VZ |
826 | #endif // wxUSE_GRID |
827 |