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