]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
Extract default grid header renderers from wxGridCellAttrProvider.
[wxWidgets.git] / src / generic / grid.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
6 // Created: 1/08/1999
7 // RCS-ID: $Id$
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 TODO:
14
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
19 */
20
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #if wxUSE_GRID
29
30 #include "wx/grid.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/utils.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
36 #include "wx/log.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
41 #include "wx/intl.h"
42 #include "wx/math.h"
43 #include "wx/listbox.h"
44 #endif
45
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
51 #include "wx/hashset.h"
52
53 #include "wx/generic/gridsel.h"
54 #include "wx/generic/gridctrl.h"
55 #include "wx/generic/grideditors.h"
56 #include "wx/generic/private/grid.h"
57
58 const char wxGridNameStr[] = "grid";
59
60 #if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
62 #else
63 #define WXUNUSED_MOTIF(identifier) identifier
64 #endif
65
66 #if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
68 #else
69 #define WXUNUSED_GTK(identifier) identifier
70 #endif
71
72 // Required for wxIs... functions
73 #include <ctype.h>
74
75 WX_DECLARE_HASH_SET_WITH_DECL(int, wxIntegerHash, wxIntegerEqual,
76 wxGridFixedIndicesSet, class WXDLLIMPEXP_ADV);
77
78
79 // ----------------------------------------------------------------------------
80 // globals
81 // ----------------------------------------------------------------------------
82
83 namespace
84 {
85
86 //#define DEBUG_ATTR_CACHE
87 #ifdef DEBUG_ATTR_CACHE
88 static size_t gs_nAttrCacheHits = 0;
89 static size_t gs_nAttrCacheMisses = 0;
90 #endif
91
92 // this struct simply combines together the default header renderers
93 //
94 // as the renderers ctors are trivial, there is no problem with making them
95 // globals
96 struct DefaultHeaderRenderers
97 {
98 wxGridColumnHeaderRendererDefault colRenderer;
99 wxGridRowHeaderRendererDefault rowRenderer;
100 wxGridCornerHeaderRendererDefault cornerRenderer;
101 } gs_defaultHeaderRenderers;
102
103 } // anonymous namespace
104
105 // ----------------------------------------------------------------------------
106 // constants
107 // ----------------------------------------------------------------------------
108
109 wxGridCellCoords wxGridNoCellCoords( -1, -1 );
110 wxRect wxGridNoCellRect( -1, -1, -1, -1 );
111
112 namespace
113 {
114
115 // scroll line size
116 const size_t GRID_SCROLL_LINE_X = 15;
117 const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
118
119 // the size of hash tables used a bit everywhere (the max number of elements
120 // in these hash tables is the number of rows/columns)
121 const int GRID_HASH_SIZE = 100;
122
123 // the minimal distance in pixels the mouse needs to move to start a drag
124 // operation
125 const int DRAG_SENSITIVITY = 3;
126
127 } // anonymous namespace
128
129 #include "wx/arrimpl.cpp"
130
131 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
132 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
133
134 // ----------------------------------------------------------------------------
135 // events
136 // ----------------------------------------------------------------------------
137
138 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK, wxGridEvent );
139 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEvent );
140 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEvent );
141 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK, wxGridEvent );
142 wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG, wxGridEvent );
143 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK, wxGridEvent );
144 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK, wxGridEvent );
145 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEvent );
146 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent );
147 wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE, wxGridSizeEvent );
148 wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE, wxGridSizeEvent );
149 wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE, wxGridEvent );
150 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT, wxGridEvent );
151 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEvent );
152 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING, wxGridEvent );
153 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED, wxGridEvent );
154 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL, wxGridEvent );
155 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN, wxGridEvent );
156 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN, wxGridEvent );
157 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED, wxGridEditorCreatedEvent );
158
159 // ============================================================================
160 // implementation
161 // ============================================================================
162
163 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler)
164
165 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
166 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus )
167 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
168 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
169 END_EVENT_TABLE()
170
171 BEGIN_EVENT_TABLE(wxGridHeaderCtrl, wxHeaderCtrl)
172 EVT_HEADER_CLICK(wxID_ANY, wxGridHeaderCtrl::OnClick)
173
174 EVT_HEADER_BEGIN_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnBeginResize)
175 EVT_HEADER_RESIZING(wxID_ANY, wxGridHeaderCtrl::OnResizing)
176 EVT_HEADER_END_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnEndResize)
177
178 EVT_HEADER_BEGIN_REORDER(wxID_ANY, wxGridHeaderCtrl::OnBeginReorder)
179 EVT_HEADER_END_REORDER(wxID_ANY, wxGridHeaderCtrl::OnEndReorder)
180 END_EVENT_TABLE()
181
182 wxGridOperations& wxGridRowOperations::Dual() const
183 {
184 static wxGridColumnOperations s_colOper;
185
186 return s_colOper;
187 }
188
189 wxGridOperations& wxGridColumnOperations::Dual() const
190 {
191 static wxGridRowOperations s_rowOper;
192
193 return s_rowOper;
194 }
195
196 // ----------------------------------------------------------------------------
197 // wxGridCellWorker is an (almost) empty common base class for
198 // wxGridCellRenderer and wxGridCellEditor managing ref counting
199 // ----------------------------------------------------------------------------
200
201 void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
202 {
203 // nothing to do
204 }
205
206 wxGridCellWorker::~wxGridCellWorker()
207 {
208 }
209
210 // ----------------------------------------------------------------------------
211 // wxGridHeaderLabelsRenderer and related classes
212 // ----------------------------------------------------------------------------
213
214 void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid& grid,
215 wxDC& dc,
216 const wxString& value,
217 const wxRect& rect,
218 int horizAlign,
219 int vertAlign,
220 int textOrientation) const
221 {
222 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
223 dc.SetTextForeground(grid.GetLabelTextColour());
224 dc.SetFont(grid.GetLabelFont());
225 grid.DrawTextRectangle(dc, value, rect, horizAlign, vertAlign, textOrientation);
226 }
227
228
229 void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
230 wxDC& dc,
231 wxRect& rect) const
232 {
233 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
234 dc.DrawLine(rect.GetRight(), rect.GetTop(),
235 rect.GetRight(), rect.GetBottom());
236 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
237 rect.GetLeft(), rect.GetBottom());
238 dc.DrawLine(rect.GetLeft(), rect.GetBottom(),
239 rect.GetRight() + 1, rect.GetBottom());
240
241 dc.SetPen(*wxWHITE_PEN);
242 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(),
243 rect.GetLeft() + 1, rect.GetBottom());
244 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(),
245 rect.GetRight(), rect.GetTop());
246
247 rect.Deflate(2);
248 }
249
250 void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
251 wxDC& dc,
252 wxRect& rect) const
253 {
254 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
255 dc.DrawLine(rect.GetRight(), rect.GetTop(),
256 rect.GetRight(), rect.GetBottom());
257 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
258 rect.GetRight(), rect.GetTop());
259 dc.DrawLine(rect.GetLeft(), rect.GetBottom(),
260 rect.GetRight() + 1, rect.GetBottom());
261
262 dc.SetPen(*wxWHITE_PEN);
263 dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1,
264 rect.GetLeft(), rect.GetBottom());
265 dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1,
266 rect.GetRight(), rect.GetTop() + 1);
267
268 rect.Deflate(2);
269 }
270
271 void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
272 wxDC& dc,
273 wxRect& rect) const
274 {
275 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
276 dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1,
277 rect.GetRight() - 1, rect.GetTop());
278 dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1,
279 rect.GetLeft(), rect.GetBottom() - 1);
280 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
281 rect.GetRight(), rect.GetTop());
282 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
283 rect.GetLeft(), rect.GetBottom());
284
285 dc.SetPen(*wxWHITE_PEN);
286 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1,
287 rect.GetRight() - 1, rect.GetTop() + 1);
288 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1,
289 rect.GetLeft() + 1, rect.GetBottom() - 1);
290
291 rect.Deflate(2);
292 }
293
294 // ----------------------------------------------------------------------------
295 // wxGridCellAttr
296 // ----------------------------------------------------------------------------
297
298 void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
299 {
300 m_isReadOnly = Unset;
301
302 m_renderer = NULL;
303 m_editor = NULL;
304
305 m_attrkind = wxGridCellAttr::Cell;
306
307 m_sizeRows = m_sizeCols = 1;
308 m_overflow = UnsetOverflow;
309
310 SetDefAttr(attrDefault);
311 }
312
313 wxGridCellAttr *wxGridCellAttr::Clone() const
314 {
315 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
316
317 if ( HasTextColour() )
318 attr->SetTextColour(GetTextColour());
319 if ( HasBackgroundColour() )
320 attr->SetBackgroundColour(GetBackgroundColour());
321 if ( HasFont() )
322 attr->SetFont(GetFont());
323 if ( HasAlignment() )
324 attr->SetAlignment(m_hAlign, m_vAlign);
325
326 attr->SetSize( m_sizeRows, m_sizeCols );
327
328 if ( m_renderer )
329 {
330 attr->SetRenderer(m_renderer);
331 m_renderer->IncRef();
332 }
333 if ( m_editor )
334 {
335 attr->SetEditor(m_editor);
336 m_editor->IncRef();
337 }
338
339 if ( IsReadOnly() )
340 attr->SetReadOnly();
341
342 attr->SetOverflow( m_overflow == Overflow );
343 attr->SetKind( m_attrkind );
344
345 return attr;
346 }
347
348 void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
349 {
350 if ( !HasTextColour() && mergefrom->HasTextColour() )
351 SetTextColour(mergefrom->GetTextColour());
352 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
353 SetBackgroundColour(mergefrom->GetBackgroundColour());
354 if ( !HasFont() && mergefrom->HasFont() )
355 SetFont(mergefrom->GetFont());
356 if ( !HasAlignment() && mergefrom->HasAlignment() )
357 {
358 int hAlign, vAlign;
359 mergefrom->GetAlignment( &hAlign, &vAlign);
360 SetAlignment(hAlign, vAlign);
361 }
362 if ( !HasSize() && mergefrom->HasSize() )
363 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
364
365 // Directly access member functions as GetRender/Editor don't just return
366 // m_renderer/m_editor
367 //
368 // Maybe add support for merge of Render and Editor?
369 if (!HasRenderer() && mergefrom->HasRenderer() )
370 {
371 m_renderer = mergefrom->m_renderer;
372 m_renderer->IncRef();
373 }
374 if ( !HasEditor() && mergefrom->HasEditor() )
375 {
376 m_editor = mergefrom->m_editor;
377 m_editor->IncRef();
378 }
379 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
380 SetReadOnly(mergefrom->IsReadOnly());
381
382 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
383 SetOverflow(mergefrom->GetOverflow());
384
385 SetDefAttr(mergefrom->m_defGridAttr);
386 }
387
388 void wxGridCellAttr::SetSize(int num_rows, int num_cols)
389 {
390 // The size of a cell is normally 1,1
391
392 // If this cell is larger (2,2) then this is the top left cell
393 // the other cells that will be covered (lower right cells) must be
394 // set to negative or zero values such that
395 // row + num_rows of the covered cell points to the larger cell (this cell)
396 // same goes for the col + num_cols.
397
398 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
399
400 wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) ||
401 !((num_rows <= 0) && (num_cols > 0)) ||
402 !((num_rows == 0) && (num_cols == 0))),
403 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
404
405 m_sizeRows = num_rows;
406 m_sizeCols = num_cols;
407 }
408
409 const wxColour& wxGridCellAttr::GetTextColour() const
410 {
411 if (HasTextColour())
412 {
413 return m_colText;
414 }
415 else if (m_defGridAttr && m_defGridAttr != this)
416 {
417 return m_defGridAttr->GetTextColour();
418 }
419 else
420 {
421 wxFAIL_MSG(wxT("Missing default cell attribute"));
422 return wxNullColour;
423 }
424 }
425
426 const wxColour& wxGridCellAttr::GetBackgroundColour() const
427 {
428 if (HasBackgroundColour())
429 {
430 return m_colBack;
431 }
432 else if (m_defGridAttr && m_defGridAttr != this)
433 {
434 return m_defGridAttr->GetBackgroundColour();
435 }
436 else
437 {
438 wxFAIL_MSG(wxT("Missing default cell attribute"));
439 return wxNullColour;
440 }
441 }
442
443 const wxFont& wxGridCellAttr::GetFont() const
444 {
445 if (HasFont())
446 {
447 return m_font;
448 }
449 else if (m_defGridAttr && m_defGridAttr != this)
450 {
451 return m_defGridAttr->GetFont();
452 }
453 else
454 {
455 wxFAIL_MSG(wxT("Missing default cell attribute"));
456 return wxNullFont;
457 }
458 }
459
460 void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
461 {
462 if (HasAlignment())
463 {
464 if ( hAlign )
465 *hAlign = m_hAlign;
466 if ( vAlign )
467 *vAlign = m_vAlign;
468 }
469 else if (m_defGridAttr && m_defGridAttr != this)
470 {
471 m_defGridAttr->GetAlignment(hAlign, vAlign);
472 }
473 else
474 {
475 wxFAIL_MSG(wxT("Missing default cell attribute"));
476 }
477 }
478
479 void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
480 {
481 if ( num_rows )
482 *num_rows = m_sizeRows;
483 if ( num_cols )
484 *num_cols = m_sizeCols;
485 }
486
487 // GetRenderer and GetEditor use a slightly different decision path about
488 // which attribute to use. If a non-default attr object has one then it is
489 // used, otherwise the default editor or renderer is fetched from the grid and
490 // used. It should be the default for the data type of the cell. If it is
491 // NULL (because the table has a type that the grid does not have in its
492 // registry), then the grid's default editor or renderer is used.
493
494 wxGridCellRenderer* wxGridCellAttr::GetRenderer(const wxGrid* grid, int row, int col) const
495 {
496 wxGridCellRenderer *renderer = NULL;
497
498 if ( m_renderer && this != m_defGridAttr )
499 {
500 // use the cells renderer if it has one
501 renderer = m_renderer;
502 renderer->IncRef();
503 }
504 else // no non-default cell renderer
505 {
506 // get default renderer for the data type
507 if ( grid )
508 {
509 // GetDefaultRendererForCell() will do IncRef() for us
510 renderer = grid->GetDefaultRendererForCell(row, col);
511 }
512
513 if ( renderer == NULL )
514 {
515 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
516 {
517 // if we still don't have one then use the grid default
518 // (no need for IncRef() here neither)
519 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
520 }
521 else // default grid attr
522 {
523 // use m_renderer which we had decided not to use initially
524 renderer = m_renderer;
525 if ( renderer )
526 renderer->IncRef();
527 }
528 }
529 }
530
531 // we're supposed to always find something
532 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
533
534 return renderer;
535 }
536
537 // same as above, except for s/renderer/editor/g
538 wxGridCellEditor* wxGridCellAttr::GetEditor(const wxGrid* grid, int row, int col) const
539 {
540 wxGridCellEditor *editor = NULL;
541
542 if ( m_editor && this != m_defGridAttr )
543 {
544 // use the cells editor if it has one
545 editor = m_editor;
546 editor->IncRef();
547 }
548 else // no non default cell editor
549 {
550 // get default editor for the data type
551 if ( grid )
552 {
553 // GetDefaultEditorForCell() will do IncRef() for us
554 editor = grid->GetDefaultEditorForCell(row, col);
555 }
556
557 if ( editor == NULL )
558 {
559 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
560 {
561 // if we still don't have one then use the grid default
562 // (no need for IncRef() here neither)
563 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
564 }
565 else // default grid attr
566 {
567 // use m_editor which we had decided not to use initially
568 editor = m_editor;
569 if ( editor )
570 editor->IncRef();
571 }
572 }
573 }
574
575 // we're supposed to always find something
576 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
577
578 return editor;
579 }
580
581 // ----------------------------------------------------------------------------
582 // wxGridCellAttrData
583 // ----------------------------------------------------------------------------
584
585 void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
586 {
587 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
588 // touch attribute's reference counting explicitly, since this
589 // is managed by class wxGridCellWithAttr
590 int n = FindIndex(row, col);
591 if ( n == wxNOT_FOUND )
592 {
593 if ( attr )
594 {
595 // add the attribute
596 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
597 }
598 //else: nothing to do
599 }
600 else // we already have an attribute for this cell
601 {
602 if ( attr )
603 {
604 // change the attribute
605 m_attrs[(size_t)n].ChangeAttr(attr);
606 }
607 else
608 {
609 // remove this attribute
610 m_attrs.RemoveAt((size_t)n);
611 }
612 }
613 }
614
615 wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
616 {
617 wxGridCellAttr *attr = NULL;
618
619 int n = FindIndex(row, col);
620 if ( n != wxNOT_FOUND )
621 {
622 attr = m_attrs[(size_t)n].attr;
623 attr->IncRef();
624 }
625
626 return attr;
627 }
628
629 void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
630 {
631 size_t count = m_attrs.GetCount();
632 for ( size_t n = 0; n < count; n++ )
633 {
634 wxGridCellCoords& coords = m_attrs[n].coords;
635 wxCoord row = coords.GetRow();
636 if ((size_t)row >= pos)
637 {
638 if (numRows > 0)
639 {
640 // If rows inserted, include row counter where necessary
641 coords.SetRow(row + numRows);
642 }
643 else if (numRows < 0)
644 {
645 // If rows deleted ...
646 if ((size_t)row >= pos - numRows)
647 {
648 // ...either decrement row counter (if row still exists)...
649 coords.SetRow(row + numRows);
650 }
651 else
652 {
653 // ...or remove the attribute
654 m_attrs.RemoveAt(n);
655 n--;
656 count--;
657 }
658 }
659 }
660 }
661 }
662
663 void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
664 {
665 size_t count = m_attrs.GetCount();
666 for ( size_t n = 0; n < count; n++ )
667 {
668 wxGridCellCoords& coords = m_attrs[n].coords;
669 wxCoord col = coords.GetCol();
670 if ( (size_t)col >= pos )
671 {
672 if ( numCols > 0 )
673 {
674 // If rows inserted, include row counter where necessary
675 coords.SetCol(col + numCols);
676 }
677 else if (numCols < 0)
678 {
679 // If rows deleted ...
680 if ((size_t)col >= pos - numCols)
681 {
682 // ...either decrement row counter (if row still exists)...
683 coords.SetCol(col + numCols);
684 }
685 else
686 {
687 // ...or remove the attribute
688 m_attrs.RemoveAt(n);
689 n--;
690 count--;
691 }
692 }
693 }
694 }
695 }
696
697 int wxGridCellAttrData::FindIndex(int row, int col) const
698 {
699 size_t count = m_attrs.GetCount();
700 for ( size_t n = 0; n < count; n++ )
701 {
702 const wxGridCellCoords& coords = m_attrs[n].coords;
703 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
704 {
705 return n;
706 }
707 }
708
709 return wxNOT_FOUND;
710 }
711
712 // ----------------------------------------------------------------------------
713 // wxGridRowOrColAttrData
714 // ----------------------------------------------------------------------------
715
716 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
717 {
718 size_t count = m_attrs.GetCount();
719 for ( size_t n = 0; n < count; n++ )
720 {
721 m_attrs[n]->DecRef();
722 }
723 }
724
725 wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
726 {
727 wxGridCellAttr *attr = NULL;
728
729 int n = m_rowsOrCols.Index(rowOrCol);
730 if ( n != wxNOT_FOUND )
731 {
732 attr = m_attrs[(size_t)n];
733 attr->IncRef();
734 }
735
736 return attr;
737 }
738
739 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
740 {
741 int i = m_rowsOrCols.Index(rowOrCol);
742 if ( i == wxNOT_FOUND )
743 {
744 if ( attr )
745 {
746 // store the new attribute, taking its ownership
747 m_rowsOrCols.Add(rowOrCol);
748 m_attrs.Add(attr);
749 }
750 // nothing to remove
751 }
752 else // we have an attribute for this row or column
753 {
754 size_t n = (size_t)i;
755
756 // notice that this code works correctly even when the old attribute is
757 // the same as the new one: as we own of it, we must call DecRef() on
758 // it in any case and this won't result in destruction of the new
759 // attribute if it's the same as old one because it must have ref count
760 // of at least 2 to be passed to us while we keep a reference to it too
761 m_attrs[n]->DecRef();
762
763 if ( attr )
764 {
765 // replace the attribute with the new one
766 m_attrs[n] = attr;
767 }
768 else // remove the attribute
769 {
770 m_rowsOrCols.RemoveAt(n);
771 m_attrs.RemoveAt(n);
772 }
773 }
774 }
775
776 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
777 {
778 size_t count = m_attrs.GetCount();
779 for ( size_t n = 0; n < count; n++ )
780 {
781 int & rowOrCol = m_rowsOrCols[n];
782 if ( (size_t)rowOrCol >= pos )
783 {
784 if ( numRowsOrCols > 0 )
785 {
786 // If rows inserted, include row counter where necessary
787 rowOrCol += numRowsOrCols;
788 }
789 else if ( numRowsOrCols < 0)
790 {
791 // If rows deleted, either decrement row counter (if row still exists)
792 if ((size_t)rowOrCol >= pos - numRowsOrCols)
793 rowOrCol += numRowsOrCols;
794 else
795 {
796 m_rowsOrCols.RemoveAt(n);
797 m_attrs[n]->DecRef();
798 m_attrs.RemoveAt(n);
799 n--;
800 count--;
801 }
802 }
803 }
804 }
805 }
806
807 // ----------------------------------------------------------------------------
808 // wxGridCellAttrProvider
809 // ----------------------------------------------------------------------------
810
811 wxGridCellAttrProvider::wxGridCellAttrProvider()
812 {
813 m_data = NULL;
814 }
815
816 wxGridCellAttrProvider::~wxGridCellAttrProvider()
817 {
818 delete m_data;
819 }
820
821 void wxGridCellAttrProvider::InitData()
822 {
823 m_data = new wxGridCellAttrProviderData;
824 }
825
826 wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
827 wxGridCellAttr::wxAttrKind kind ) const
828 {
829 wxGridCellAttr *attr = NULL;
830 if ( m_data )
831 {
832 switch (kind)
833 {
834 case (wxGridCellAttr::Any):
835 // Get cached merge attributes.
836 // Currently not used as no cache implemented as not mutable
837 // attr = m_data->m_mergeAttr.GetAttr(row, col);
838 if (!attr)
839 {
840 // Basically implement old version.
841 // Also check merge cache, so we don't have to re-merge every time..
842 wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col);
843 wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row);
844 wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col);
845
846 if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol))
847 {
848 // Two or more are non NULL
849 attr = new wxGridCellAttr;
850 attr->SetKind(wxGridCellAttr::Merged);
851
852 // Order is important..
853 if (attrcell)
854 {
855 attr->MergeWith(attrcell);
856 attrcell->DecRef();
857 }
858 if (attrcol)
859 {
860 attr->MergeWith(attrcol);
861 attrcol->DecRef();
862 }
863 if (attrrow)
864 {
865 attr->MergeWith(attrrow);
866 attrrow->DecRef();
867 }
868
869 // store merge attr if cache implemented
870 //attr->IncRef();
871 //m_data->m_mergeAttr.SetAttr(attr, row, col);
872 }
873 else
874 {
875 // one or none is non null return it or null.
876 if (attrrow)
877 attr = attrrow;
878 if (attrcol)
879 {
880 if (attr)
881 attr->DecRef();
882 attr = attrcol;
883 }
884 if (attrcell)
885 {
886 if (attr)
887 attr->DecRef();
888 attr = attrcell;
889 }
890 }
891 }
892 break;
893
894 case (wxGridCellAttr::Cell):
895 attr = m_data->m_cellAttrs.GetAttr(row, col);
896 break;
897
898 case (wxGridCellAttr::Col):
899 attr = m_data->m_colAttrs.GetAttr(col);
900 break;
901
902 case (wxGridCellAttr::Row):
903 attr = m_data->m_rowAttrs.GetAttr(row);
904 break;
905
906 default:
907 // unused as yet...
908 // (wxGridCellAttr::Default):
909 // (wxGridCellAttr::Merged):
910 break;
911 }
912 }
913
914 return attr;
915 }
916
917 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
918 int row, int col)
919 {
920 if ( !m_data )
921 InitData();
922
923 m_data->m_cellAttrs.SetAttr(attr, row, col);
924 }
925
926 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
927 {
928 if ( !m_data )
929 InitData();
930
931 m_data->m_rowAttrs.SetAttr(attr, row);
932 }
933
934 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
935 {
936 if ( !m_data )
937 InitData();
938
939 m_data->m_colAttrs.SetAttr(attr, col);
940 }
941
942 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
943 {
944 if ( m_data )
945 {
946 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
947
948 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
949 }
950 }
951
952 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
953 {
954 if ( m_data )
955 {
956 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
957
958 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
959 }
960 }
961
962 const wxGridColumnHeaderRenderer&
963 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col))
964 {
965 return gs_defaultHeaderRenderers.colRenderer;
966 }
967
968 const wxGridRowHeaderRenderer&
969 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row))
970 {
971 return gs_defaultHeaderRenderers.rowRenderer;
972 }
973
974 const wxGridCornerHeaderRenderer& wxGridCellAttrProvider::GetCornerRenderer()
975 {
976 return gs_defaultHeaderRenderers.cornerRenderer;
977 }
978
979 // ----------------------------------------------------------------------------
980 // wxGridTableBase
981 // ----------------------------------------------------------------------------
982
983 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
984
985 wxGridTableBase::wxGridTableBase()
986 {
987 m_view = NULL;
988 m_attrProvider = NULL;
989 }
990
991 wxGridTableBase::~wxGridTableBase()
992 {
993 delete m_attrProvider;
994 }
995
996 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
997 {
998 delete m_attrProvider;
999 m_attrProvider = attrProvider;
1000 }
1001
1002 bool wxGridTableBase::CanHaveAttributes()
1003 {
1004 if ( ! GetAttrProvider() )
1005 {
1006 // use the default attr provider by default
1007 SetAttrProvider(new wxGridCellAttrProvider);
1008 }
1009
1010 return true;
1011 }
1012
1013 wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
1014 {
1015 if ( m_attrProvider )
1016 return m_attrProvider->GetAttr(row, col, kind);
1017 else
1018 return NULL;
1019 }
1020
1021 void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
1022 {
1023 if ( m_attrProvider )
1024 {
1025 if ( attr )
1026 attr->SetKind(wxGridCellAttr::Cell);
1027 m_attrProvider->SetAttr(attr, row, col);
1028 }
1029 else
1030 {
1031 // as we take ownership of the pointer and don't store it, we must
1032 // free it now
1033 wxSafeDecRef(attr);
1034 }
1035 }
1036
1037 void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
1038 {
1039 if ( m_attrProvider )
1040 {
1041 attr->SetKind(wxGridCellAttr::Row);
1042 m_attrProvider->SetRowAttr(attr, row);
1043 }
1044 else
1045 {
1046 // as we take ownership of the pointer and don't store it, we must
1047 // free it now
1048 wxSafeDecRef(attr);
1049 }
1050 }
1051
1052 void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
1053 {
1054 if ( m_attrProvider )
1055 {
1056 attr->SetKind(wxGridCellAttr::Col);
1057 m_attrProvider->SetColAttr(attr, col);
1058 }
1059 else
1060 {
1061 // as we take ownership of the pointer and don't store it, we must
1062 // free it now
1063 wxSafeDecRef(attr);
1064 }
1065 }
1066
1067 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
1068 size_t WXUNUSED(numRows) )
1069 {
1070 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1071
1072 return false;
1073 }
1074
1075 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
1076 {
1077 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1078
1079 return false;
1080 }
1081
1082 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
1083 size_t WXUNUSED(numRows) )
1084 {
1085 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1086
1087 return false;
1088 }
1089
1090 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
1091 size_t WXUNUSED(numCols) )
1092 {
1093 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1094
1095 return false;
1096 }
1097
1098 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
1099 {
1100 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1101
1102 return false;
1103 }
1104
1105 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
1106 size_t WXUNUSED(numCols) )
1107 {
1108 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1109
1110 return false;
1111 }
1112
1113 wxString wxGridTableBase::GetRowLabelValue( int row )
1114 {
1115 wxString s;
1116
1117 // RD: Starting the rows at zero confuses users,
1118 // no matter how much it makes sense to us geeks.
1119 s << row + 1;
1120
1121 return s;
1122 }
1123
1124 wxString wxGridTableBase::GetColLabelValue( int col )
1125 {
1126 // default col labels are:
1127 // cols 0 to 25 : A-Z
1128 // cols 26 to 675 : AA-ZZ
1129 // etc.
1130
1131 wxString s;
1132 unsigned int i, n;
1133 for ( n = 1; ; n++ )
1134 {
1135 s += (wxChar) (wxT('A') + (wxChar)(col % 26));
1136 col = col / 26 - 1;
1137 if ( col < 0 )
1138 break;
1139 }
1140
1141 // reverse the string...
1142 wxString s2;
1143 for ( i = 0; i < n; i++ )
1144 {
1145 s2 += s[n - i - 1];
1146 }
1147
1148 return s2;
1149 }
1150
1151 wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
1152 {
1153 return wxGRID_VALUE_STRING;
1154 }
1155
1156 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
1157 const wxString& typeName )
1158 {
1159 return typeName == wxGRID_VALUE_STRING;
1160 }
1161
1162 bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
1163 {
1164 return CanGetValueAs(row, col, typeName);
1165 }
1166
1167 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
1168 {
1169 return 0;
1170 }
1171
1172 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
1173 {
1174 return 0.0;
1175 }
1176
1177 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
1178 {
1179 return false;
1180 }
1181
1182 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
1183 long WXUNUSED(value) )
1184 {
1185 }
1186
1187 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
1188 double WXUNUSED(value) )
1189 {
1190 }
1191
1192 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
1193 bool WXUNUSED(value) )
1194 {
1195 }
1196
1197 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
1198 const wxString& WXUNUSED(typeName) )
1199 {
1200 return NULL;
1201 }
1202
1203 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
1204 const wxString& WXUNUSED(typeName),
1205 void* WXUNUSED(value) )
1206 {
1207 }
1208
1209 //////////////////////////////////////////////////////////////////////
1210 //
1211 // Message class for the grid table to send requests and notifications
1212 // to the grid view
1213 //
1214
1215 wxGridTableMessage::wxGridTableMessage()
1216 {
1217 m_table = NULL;
1218 m_id = -1;
1219 m_comInt1 = -1;
1220 m_comInt2 = -1;
1221 }
1222
1223 wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
1224 int commandInt1, int commandInt2 )
1225 {
1226 m_table = table;
1227 m_id = id;
1228 m_comInt1 = commandInt1;
1229 m_comInt2 = commandInt2;
1230 }
1231
1232 //////////////////////////////////////////////////////////////////////
1233 //
1234 // A basic grid table for string data. An object of this class will
1235 // created by wxGrid if you don't specify an alternative table class.
1236 //
1237
1238 WX_DEFINE_OBJARRAY(wxGridStringArray)
1239
1240 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
1241
1242 wxGridStringTable::wxGridStringTable()
1243 : wxGridTableBase()
1244 {
1245 m_numCols = 0;
1246 }
1247
1248 wxGridStringTable::wxGridStringTable( int numRows, int numCols )
1249 : wxGridTableBase()
1250 {
1251 m_numCols = numCols;
1252
1253 m_data.Alloc( numRows );
1254
1255 wxArrayString sa;
1256 sa.Alloc( numCols );
1257 sa.Add( wxEmptyString, numCols );
1258
1259 m_data.Add( sa, numRows );
1260 }
1261
1262 wxString wxGridStringTable::GetValue( int row, int col )
1263 {
1264 wxCHECK_MSG( (row >= 0 && row < GetNumberRows()) &&
1265 (col >= 0 && col < GetNumberCols()),
1266 wxEmptyString,
1267 wxT("invalid row or column index in wxGridStringTable") );
1268
1269 return m_data[row][col];
1270 }
1271
1272 void wxGridStringTable::SetValue( int row, int col, const wxString& value )
1273 {
1274 wxCHECK_RET( (row >= 0 && row < GetNumberRows()) &&
1275 (col >= 0 && col < GetNumberCols()),
1276 wxT("invalid row or column index in wxGridStringTable") );
1277
1278 m_data[row][col] = value;
1279 }
1280
1281 void wxGridStringTable::Clear()
1282 {
1283 int row, col;
1284 int numRows, numCols;
1285
1286 numRows = m_data.GetCount();
1287 if ( numRows > 0 )
1288 {
1289 numCols = m_data[0].GetCount();
1290
1291 for ( row = 0; row < numRows; row++ )
1292 {
1293 for ( col = 0; col < numCols; col++ )
1294 {
1295 m_data[row][col] = wxEmptyString;
1296 }
1297 }
1298 }
1299 }
1300
1301 bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
1302 {
1303 size_t curNumRows = m_data.GetCount();
1304 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
1305 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1306
1307 if ( pos >= curNumRows )
1308 {
1309 return AppendRows( numRows );
1310 }
1311
1312 wxArrayString sa;
1313 sa.Alloc( curNumCols );
1314 sa.Add( wxEmptyString, curNumCols );
1315 m_data.Insert( sa, pos, numRows );
1316
1317 if ( GetView() )
1318 {
1319 wxGridTableMessage msg( this,
1320 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
1321 pos,
1322 numRows );
1323
1324 GetView()->ProcessTableMessage( msg );
1325 }
1326
1327 return true;
1328 }
1329
1330 bool wxGridStringTable::AppendRows( size_t numRows )
1331 {
1332 size_t curNumRows = m_data.GetCount();
1333 size_t curNumCols = ( curNumRows > 0
1334 ? m_data[0].GetCount()
1335 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1336
1337 wxArrayString sa;
1338 if ( curNumCols > 0 )
1339 {
1340 sa.Alloc( curNumCols );
1341 sa.Add( wxEmptyString, curNumCols );
1342 }
1343
1344 m_data.Add( sa, numRows );
1345
1346 if ( GetView() )
1347 {
1348 wxGridTableMessage msg( this,
1349 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
1350 numRows );
1351
1352 GetView()->ProcessTableMessage( msg );
1353 }
1354
1355 return true;
1356 }
1357
1358 bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
1359 {
1360 size_t curNumRows = m_data.GetCount();
1361
1362 if ( pos >= curNumRows )
1363 {
1364 wxFAIL_MSG( wxString::Format
1365 (
1366 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1367 (unsigned long)pos,
1368 (unsigned long)numRows,
1369 (unsigned long)curNumRows
1370 ) );
1371
1372 return false;
1373 }
1374
1375 if ( numRows > curNumRows - pos )
1376 {
1377 numRows = curNumRows - pos;
1378 }
1379
1380 if ( numRows >= curNumRows )
1381 {
1382 m_data.Clear();
1383 }
1384 else
1385 {
1386 m_data.RemoveAt( pos, numRows );
1387 }
1388
1389 if ( GetView() )
1390 {
1391 wxGridTableMessage msg( this,
1392 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
1393 pos,
1394 numRows );
1395
1396 GetView()->ProcessTableMessage( msg );
1397 }
1398
1399 return true;
1400 }
1401
1402 bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
1403 {
1404 size_t row, col;
1405
1406 size_t curNumRows = m_data.GetCount();
1407 size_t curNumCols = ( curNumRows > 0
1408 ? m_data[0].GetCount()
1409 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1410
1411 if ( pos >= curNumCols )
1412 {
1413 return AppendCols( numCols );
1414 }
1415
1416 if ( !m_colLabels.IsEmpty() )
1417 {
1418 m_colLabels.Insert( wxEmptyString, pos, numCols );
1419
1420 size_t i;
1421 for ( i = pos; i < pos + numCols; i++ )
1422 m_colLabels[i] = wxGridTableBase::GetColLabelValue( i );
1423 }
1424
1425 for ( row = 0; row < curNumRows; row++ )
1426 {
1427 for ( col = pos; col < pos + numCols; col++ )
1428 {
1429 m_data[row].Insert( wxEmptyString, col );
1430 }
1431 }
1432
1433 m_numCols += numCols;
1434
1435 if ( GetView() )
1436 {
1437 wxGridTableMessage msg( this,
1438 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
1439 pos,
1440 numCols );
1441
1442 GetView()->ProcessTableMessage( msg );
1443 }
1444
1445 return true;
1446 }
1447
1448 bool wxGridStringTable::AppendCols( size_t numCols )
1449 {
1450 size_t row;
1451
1452 size_t curNumRows = m_data.GetCount();
1453
1454 for ( row = 0; row < curNumRows; row++ )
1455 {
1456 m_data[row].Add( wxEmptyString, numCols );
1457 }
1458
1459 m_numCols += numCols;
1460
1461 if ( GetView() )
1462 {
1463 wxGridTableMessage msg( this,
1464 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
1465 numCols );
1466
1467 GetView()->ProcessTableMessage( msg );
1468 }
1469
1470 return true;
1471 }
1472
1473 bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
1474 {
1475 size_t row;
1476
1477 size_t curNumRows = m_data.GetCount();
1478 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
1479 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1480
1481 if ( pos >= curNumCols )
1482 {
1483 wxFAIL_MSG( wxString::Format
1484 (
1485 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1486 (unsigned long)pos,
1487 (unsigned long)numCols,
1488 (unsigned long)curNumCols
1489 ) );
1490 return false;
1491 }
1492
1493 int colID;
1494 if ( GetView() )
1495 colID = GetView()->GetColAt( pos );
1496 else
1497 colID = pos;
1498
1499 if ( numCols > curNumCols - colID )
1500 {
1501 numCols = curNumCols - colID;
1502 }
1503
1504 if ( !m_colLabels.IsEmpty() )
1505 {
1506 // m_colLabels stores just as many elements as it needs, e.g. if only
1507 // the label of the first column had been set it would have only one
1508 // element and not numCols, so account for it
1509 int nToRm = m_colLabels.size() - colID;
1510 if ( nToRm > 0 )
1511 m_colLabels.RemoveAt( colID, nToRm );
1512 }
1513
1514 if ( numCols >= curNumCols )
1515 {
1516 for ( row = 0; row < curNumRows; row++ )
1517 {
1518 m_data[row].Clear();
1519 }
1520
1521 m_numCols = 0;
1522 }
1523 else // something will be left
1524 {
1525 for ( row = 0; row < curNumRows; row++ )
1526 {
1527 m_data[row].RemoveAt( colID, numCols );
1528 }
1529
1530 m_numCols -= numCols;
1531 }
1532
1533 if ( GetView() )
1534 {
1535 wxGridTableMessage msg( this,
1536 wxGRIDTABLE_NOTIFY_COLS_DELETED,
1537 pos,
1538 numCols );
1539
1540 GetView()->ProcessTableMessage( msg );
1541 }
1542
1543 return true;
1544 }
1545
1546 wxString wxGridStringTable::GetRowLabelValue( int row )
1547 {
1548 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
1549 {
1550 // using default label
1551 //
1552 return wxGridTableBase::GetRowLabelValue( row );
1553 }
1554 else
1555 {
1556 return m_rowLabels[row];
1557 }
1558 }
1559
1560 wxString wxGridStringTable::GetColLabelValue( int col )
1561 {
1562 if ( col > (int)(m_colLabels.GetCount()) - 1 )
1563 {
1564 // using default label
1565 //
1566 return wxGridTableBase::GetColLabelValue( col );
1567 }
1568 else
1569 {
1570 return m_colLabels[col];
1571 }
1572 }
1573
1574 void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
1575 {
1576 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
1577 {
1578 int n = m_rowLabels.GetCount();
1579 int i;
1580
1581 for ( i = n; i <= row; i++ )
1582 {
1583 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
1584 }
1585 }
1586
1587 m_rowLabels[row] = value;
1588 }
1589
1590 void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
1591 {
1592 if ( col > (int)(m_colLabels.GetCount()) - 1 )
1593 {
1594 int n = m_colLabels.GetCount();
1595 int i;
1596
1597 for ( i = n; i <= col; i++ )
1598 {
1599 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
1600 }
1601 }
1602
1603 m_colLabels[col] = value;
1604 }
1605
1606
1607 //////////////////////////////////////////////////////////////////////
1608 //////////////////////////////////////////////////////////////////////
1609
1610 BEGIN_EVENT_TABLE(wxGridSubwindow, wxWindow)
1611 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost)
1612 END_EVENT_TABLE()
1613
1614 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
1615 {
1616 m_owner->CancelMouseCapture();
1617 }
1618
1619 BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxGridSubwindow )
1620 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
1621 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel )
1622 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
1623 END_EVENT_TABLE()
1624
1625 void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
1626 {
1627 wxPaintDC dc(this);
1628
1629 // NO - don't do this because it will set both the x and y origin
1630 // coords to match the parent scrolled window and we just want to
1631 // set the y coord - MB
1632 //
1633 // m_owner->PrepareDC( dc );
1634
1635 int x, y;
1636 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
1637 wxPoint pt = dc.GetDeviceOrigin();
1638 dc.SetDeviceOrigin( pt.x, pt.y-y );
1639
1640 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
1641 m_owner->DrawRowLabels( dc, rows );
1642 }
1643
1644 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
1645 {
1646 m_owner->ProcessRowLabelMouseEvent( event );
1647 }
1648
1649 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
1650 {
1651 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
1652 event.Skip();
1653 }
1654
1655 //////////////////////////////////////////////////////////////////////
1656
1657 BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxGridSubwindow )
1658 EVT_PAINT( wxGridColLabelWindow::OnPaint )
1659 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel )
1660 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
1661 END_EVENT_TABLE()
1662
1663 void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
1664 {
1665 wxPaintDC dc(this);
1666
1667 // NO - don't do this because it will set both the x and y origin
1668 // coords to match the parent scrolled window and we just want to
1669 // set the x coord - MB
1670 //
1671 // m_owner->PrepareDC( dc );
1672
1673 int x, y;
1674 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
1675 wxPoint pt = dc.GetDeviceOrigin();
1676 if (GetLayoutDirection() == wxLayout_RightToLeft)
1677 dc.SetDeviceOrigin( pt.x+x, pt.y );
1678 else
1679 dc.SetDeviceOrigin( pt.x-x, pt.y );
1680
1681 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
1682 m_owner->DrawColLabels( dc, cols );
1683 }
1684
1685 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
1686 {
1687 m_owner->ProcessColLabelMouseEvent( event );
1688 }
1689
1690 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
1691 {
1692 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
1693 event.Skip();
1694 }
1695
1696 //////////////////////////////////////////////////////////////////////
1697
1698 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxGridSubwindow )
1699 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel )
1700 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
1701 EVT_PAINT( wxGridCornerLabelWindow::OnPaint )
1702 END_EVENT_TABLE()
1703
1704 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
1705 {
1706 wxPaintDC dc(this);
1707
1708 m_owner->DrawCornerLabel(dc);
1709 }
1710
1711 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
1712 {
1713 m_owner->ProcessCornerLabelMouseEvent( event );
1714 }
1715
1716 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
1717 {
1718 if (!m_owner->GetEventHandler()->ProcessEvent(event))
1719 event.Skip();
1720 }
1721
1722 //////////////////////////////////////////////////////////////////////
1723
1724 BEGIN_EVENT_TABLE( wxGridWindow, wxGridSubwindow )
1725 EVT_PAINT( wxGridWindow::OnPaint )
1726 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel )
1727 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
1728 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
1729 EVT_KEY_UP( wxGridWindow::OnKeyUp )
1730 EVT_CHAR( wxGridWindow::OnChar )
1731 EVT_SET_FOCUS( wxGridWindow::OnFocus )
1732 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
1733 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
1734 END_EVENT_TABLE()
1735
1736 void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1737 {
1738 wxPaintDC dc( this );
1739 m_owner->PrepareDC( dc );
1740 wxRegion reg = GetUpdateRegion();
1741 wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg );
1742 m_owner->DrawGridCellArea( dc, dirtyCells );
1743
1744 m_owner->DrawGridSpace( dc );
1745
1746 m_owner->DrawAllGridLines( dc, reg );
1747
1748 m_owner->DrawHighlight( dc, dirtyCells );
1749 }
1750
1751 void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
1752 {
1753 wxWindow::ScrollWindow( dx, dy, rect );
1754 m_owner->GetGridRowLabelWindow()->ScrollWindow( 0, dy, rect );
1755 m_owner->GetGridColLabelWindow()->ScrollWindow( dx, 0, rect );
1756 }
1757
1758 void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
1759 {
1760 if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
1761 SetFocus();
1762
1763 m_owner->ProcessGridCellMouseEvent( event );
1764 }
1765
1766 void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
1767 {
1768 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
1769 event.Skip();
1770 }
1771
1772 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1773 // cursor must be in the cell edit control to get key events
1774 //
1775 void wxGridWindow::OnKeyDown( wxKeyEvent& event )
1776 {
1777 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
1778 event.Skip();
1779 }
1780
1781 void wxGridWindow::OnKeyUp( wxKeyEvent& event )
1782 {
1783 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
1784 event.Skip();
1785 }
1786
1787 void wxGridWindow::OnChar( wxKeyEvent& event )
1788 {
1789 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
1790 event.Skip();
1791 }
1792
1793 void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
1794 {
1795 }
1796
1797 void wxGridWindow::OnFocus(wxFocusEvent& event)
1798 {
1799 // and if we have any selection, it has to be repainted, because it
1800 // uses different colour when the grid is not focused:
1801 if ( m_owner->IsSelection() )
1802 {
1803 Refresh();
1804 }
1805 else
1806 {
1807 // NB: Note that this code is in "else" branch only because the other
1808 // branch refreshes everything and so there's no point in calling
1809 // Refresh() again, *not* because it should only be done if
1810 // !IsSelection(). If the above code is ever optimized to refresh
1811 // only selected area, this needs to be moved out of the "else"
1812 // branch so that it's always executed.
1813
1814 // current cell cursor {dis,re}appears on focus change:
1815 const wxGridCellCoords cursorCoords(m_owner->GetGridCursorRow(),
1816 m_owner->GetGridCursorCol());
1817 const wxRect cursor =
1818 m_owner->BlockToDeviceRect(cursorCoords, cursorCoords);
1819 Refresh(true, &cursor);
1820 }
1821
1822 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
1823 event.Skip();
1824 }
1825
1826 #define internalXToCol(x) XToCol(x, true)
1827 #define internalYToRow(y) YToRow(y, true)
1828
1829 /////////////////////////////////////////////////////////////////////
1830
1831 #if wxUSE_EXTENDED_RTTI
1832 WX_DEFINE_FLAGS( wxGridStyle )
1833
1834 wxBEGIN_FLAGS( wxGridStyle )
1835 // new style border flags, we put them first to
1836 // use them for streaming out
1837 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
1838 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
1839 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
1840 wxFLAGS_MEMBER(wxBORDER_RAISED)
1841 wxFLAGS_MEMBER(wxBORDER_STATIC)
1842 wxFLAGS_MEMBER(wxBORDER_NONE)
1843
1844 // old style border flags
1845 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
1846 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
1847 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
1848 wxFLAGS_MEMBER(wxRAISED_BORDER)
1849 wxFLAGS_MEMBER(wxSTATIC_BORDER)
1850 wxFLAGS_MEMBER(wxBORDER)
1851
1852 // standard window styles
1853 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
1854 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
1855 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
1856 wxFLAGS_MEMBER(wxWANTS_CHARS)
1857 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
1858 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB)
1859 wxFLAGS_MEMBER(wxVSCROLL)
1860 wxFLAGS_MEMBER(wxHSCROLL)
1861
1862 wxEND_FLAGS( wxGridStyle )
1863
1864 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
1865
1866 wxBEGIN_PROPERTIES_TABLE(wxGrid)
1867 wxHIDE_PROPERTY( Children )
1868 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
1869 wxEND_PROPERTIES_TABLE()
1870
1871 wxBEGIN_HANDLERS_TABLE(wxGrid)
1872 wxEND_HANDLERS_TABLE()
1873
1874 wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
1875
1876 /*
1877 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
1878 */
1879 #else
1880 IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
1881 #endif
1882
1883 BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
1884 EVT_PAINT( wxGrid::OnPaint )
1885 EVT_SIZE( wxGrid::OnSize )
1886 EVT_KEY_DOWN( wxGrid::OnKeyDown )
1887 EVT_KEY_UP( wxGrid::OnKeyUp )
1888 EVT_CHAR ( wxGrid::OnChar )
1889 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
1890 END_EVENT_TABLE()
1891
1892 bool wxGrid::Create(wxWindow *parent, wxWindowID id,
1893 const wxPoint& pos, const wxSize& size,
1894 long style, const wxString& name)
1895 {
1896 if (!wxScrolledWindow::Create(parent, id, pos, size,
1897 style | wxWANTS_CHARS, name))
1898 return false;
1899
1900 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
1901 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE);
1902
1903 Create();
1904 SetInitialSize(size);
1905 SetScrollRate(m_scrollLineX, m_scrollLineY);
1906 CalcDimensions();
1907
1908 return true;
1909 }
1910
1911 wxGrid::~wxGrid()
1912 {
1913 if ( m_winCapture )
1914 m_winCapture->ReleaseMouse();
1915
1916 // Ensure that the editor control is destroyed before the grid is,
1917 // otherwise we crash later when the editor tries to do something with the
1918 // half destroyed grid
1919 HideCellEditControl();
1920
1921 // Must do this or ~wxScrollHelper will pop the wrong event handler
1922 SetTargetWindow(this);
1923 ClearAttrCache();
1924 wxSafeDecRef(m_defaultCellAttr);
1925
1926 #ifdef DEBUG_ATTR_CACHE
1927 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
1928 wxPrintf(wxT("wxGrid attribute cache statistics: "
1929 "total: %u, hits: %u (%u%%)\n"),
1930 total, gs_nAttrCacheHits,
1931 total ? (gs_nAttrCacheHits*100) / total : 0);
1932 #endif
1933
1934 // if we own the table, just delete it, otherwise at least don't leave it
1935 // with dangling view pointer
1936 if ( m_ownTable )
1937 delete m_table;
1938 else if ( m_table && m_table->GetView() == this )
1939 m_table->SetView(NULL);
1940
1941 delete m_typeRegistry;
1942 delete m_selection;
1943
1944 delete m_setFixedRows;
1945 delete m_setFixedCols;
1946 }
1947
1948 //
1949 // ----- internal init and update functions
1950 //
1951
1952 // NOTE: If using the default visual attributes works everywhere then this can
1953 // be removed as well as the #else cases below.
1954 #define _USE_VISATTR 0
1955
1956 void wxGrid::Create()
1957 {
1958 // create the type registry
1959 m_typeRegistry = new wxGridTypeRegistry;
1960
1961 m_cellEditCtrlEnabled = false;
1962
1963 m_defaultCellAttr = new wxGridCellAttr();
1964
1965 // Set default cell attributes
1966 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
1967 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
1968 m_defaultCellAttr->SetFont(GetFont());
1969 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
1970 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
1971 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
1972
1973 #if _USE_VISATTR
1974 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
1975 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
1976
1977 m_defaultCellAttr->SetTextColour(gva.colFg);
1978 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
1979
1980 #else
1981 m_defaultCellAttr->SetTextColour(
1982 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
1983 m_defaultCellAttr->SetBackgroundColour(
1984 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
1985 #endif
1986
1987 m_numRows = 0;
1988 m_numCols = 0;
1989 m_currentCellCoords = wxGridNoCellCoords;
1990
1991 // subwindow components that make up the wxGrid
1992 m_rowLabelWin = new wxGridRowLabelWindow(this);
1993 CreateColumnWindow();
1994 m_cornerLabelWin = new wxGridCornerLabelWindow(this);
1995 m_gridWin = new wxGridWindow( this );
1996
1997 SetTargetWindow( m_gridWin );
1998
1999 #if _USE_VISATTR
2000 wxColour gfg = gva.colFg;
2001 wxColour gbg = gva.colBg;
2002 wxColour lfg = lva.colFg;
2003 wxColour lbg = lva.colBg;
2004 #else
2005 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
2006 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
2007 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
2008 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
2009 #endif
2010
2011 m_cornerLabelWin->SetOwnForegroundColour(lfg);
2012 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
2013 m_rowLabelWin->SetOwnForegroundColour(lfg);
2014 m_rowLabelWin->SetOwnBackgroundColour(lbg);
2015 m_colWindow->SetOwnForegroundColour(lfg);
2016 m_colWindow->SetOwnBackgroundColour(lbg);
2017
2018 m_gridWin->SetOwnForegroundColour(gfg);
2019 m_gridWin->SetOwnBackgroundColour(gbg);
2020
2021 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
2022 m_labelTextColour = m_rowLabelWin->GetForegroundColour();
2023
2024 // now that we have the grid window, use its font to compute the default
2025 // row height
2026 m_defaultRowHeight = m_gridWin->GetCharHeight();
2027 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2028 m_defaultRowHeight += 8;
2029 #else
2030 m_defaultRowHeight += 4;
2031 #endif
2032
2033 }
2034
2035 void wxGrid::CreateColumnWindow()
2036 {
2037 if ( m_useNativeHeader )
2038 {
2039 m_colWindow = new wxGridHeaderCtrl(this);
2040 m_colLabelHeight = m_colWindow->GetBestSize().y;
2041 }
2042 else // draw labels ourselves
2043 {
2044 m_colWindow = new wxGridColLabelWindow(this);
2045 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2046 }
2047 }
2048
2049 bool wxGrid::CreateGrid( int numRows, int numCols,
2050 wxGridSelectionModes selmode )
2051 {
2052 wxCHECK_MSG( !m_created,
2053 false,
2054 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2055
2056 return SetTable(new wxGridStringTable(numRows, numCols), true, selmode);
2057 }
2058
2059 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode)
2060 {
2061 wxCHECK_RET( m_created,
2062 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2063
2064 m_selection->SetSelectionMode( selmode );
2065 }
2066
2067 wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
2068 {
2069 wxCHECK_MSG( m_created, wxGridSelectCells,
2070 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2071
2072 return m_selection->GetSelectionMode();
2073 }
2074
2075 bool
2076 wxGrid::SetTable(wxGridTableBase *table,
2077 bool takeOwnership,
2078 wxGrid::wxGridSelectionModes selmode )
2079 {
2080 bool checkSelection = false;
2081 if ( m_created )
2082 {
2083 // stop all processing
2084 m_created = false;
2085
2086 if (m_table)
2087 {
2088 m_table->SetView(0);
2089 if( m_ownTable )
2090 delete m_table;
2091 m_table = NULL;
2092 }
2093
2094 delete m_selection;
2095 m_selection = NULL;
2096
2097 m_ownTable = false;
2098 m_numRows = 0;
2099 m_numCols = 0;
2100 checkSelection = true;
2101
2102 // kill row and column size arrays
2103 m_colWidths.Empty();
2104 m_colRights.Empty();
2105 m_rowHeights.Empty();
2106 m_rowBottoms.Empty();
2107 }
2108
2109 if (table)
2110 {
2111 m_numRows = table->GetNumberRows();
2112 m_numCols = table->GetNumberCols();
2113
2114 if ( m_useNativeHeader )
2115 GetGridColHeader()->SetColumnCount(m_numCols);
2116
2117 m_table = table;
2118 m_table->SetView( this );
2119 m_ownTable = takeOwnership;
2120 m_selection = new wxGridSelection( this, selmode );
2121 if (checkSelection)
2122 {
2123 // If the newly set table is smaller than the
2124 // original one current cell and selection regions
2125 // might be invalid,
2126 m_selectedBlockCorner = wxGridNoCellCoords;
2127 m_currentCellCoords =
2128 wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()),
2129 wxMin(m_numCols, m_currentCellCoords.GetCol()));
2130 if (m_selectedBlockTopLeft.GetRow() >= m_numRows ||
2131 m_selectedBlockTopLeft.GetCol() >= m_numCols)
2132 {
2133 m_selectedBlockTopLeft = wxGridNoCellCoords;
2134 m_selectedBlockBottomRight = wxGridNoCellCoords;
2135 }
2136 else
2137 m_selectedBlockBottomRight =
2138 wxGridCellCoords(wxMin(m_numRows,
2139 m_selectedBlockBottomRight.GetRow()),
2140 wxMin(m_numCols,
2141 m_selectedBlockBottomRight.GetCol()));
2142 }
2143 CalcDimensions();
2144
2145 m_created = true;
2146 }
2147
2148 return m_created;
2149 }
2150
2151 void wxGrid::Init()
2152 {
2153 m_created = false;
2154
2155 m_cornerLabelWin = NULL;
2156 m_rowLabelWin = NULL;
2157 m_colWindow = NULL;
2158 m_gridWin = NULL;
2159
2160 m_table = NULL;
2161 m_ownTable = false;
2162
2163 m_selection = NULL;
2164 m_defaultCellAttr = NULL;
2165 m_typeRegistry = NULL;
2166 m_winCapture = NULL;
2167
2168 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
2169 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2170
2171 m_setFixedRows =
2172 m_setFixedCols = NULL;
2173
2174 // init attr cache
2175 m_attrCache.row = -1;
2176 m_attrCache.col = -1;
2177 m_attrCache.attr = NULL;
2178
2179 m_labelFont = GetFont();
2180 m_labelFont.SetWeight( wxBOLD );
2181
2182 m_rowLabelHorizAlign = wxALIGN_CENTRE;
2183 m_rowLabelVertAlign = wxALIGN_CENTRE;
2184
2185 m_colLabelHorizAlign = wxALIGN_CENTRE;
2186 m_colLabelVertAlign = wxALIGN_CENTRE;
2187 m_colLabelTextOrientation = wxHORIZONTAL;
2188
2189 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
2190 m_defaultRowHeight = 0; // this will be initialized after creation
2191
2192 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
2193 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
2194
2195 m_gridLineColour = wxColour( 192,192,192 );
2196 m_gridLinesEnabled = true;
2197 m_gridLinesClipHorz =
2198 m_gridLinesClipVert = true;
2199 m_cellHighlightColour = *wxBLACK;
2200 m_cellHighlightPenWidth = 2;
2201 m_cellHighlightROPenWidth = 1;
2202
2203 m_canDragColMove = false;
2204
2205 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
2206 m_winCapture = NULL;
2207 m_canDragRowSize = true;
2208 m_canDragColSize = true;
2209 m_canDragGridSize = true;
2210 m_canDragCell = false;
2211 m_dragLastPos = -1;
2212 m_dragRowOrCol = -1;
2213 m_isDragging = false;
2214 m_startDragPos = wxDefaultPosition;
2215
2216 m_sortCol = wxNOT_FOUND;
2217 m_sortIsAscending = true;
2218
2219 m_useNativeHeader =
2220 m_nativeColumnLabels = false;
2221
2222 m_waitForSlowClick = false;
2223
2224 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
2225 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
2226
2227 m_currentCellCoords = wxGridNoCellCoords;
2228
2229 m_selectedBlockTopLeft =
2230 m_selectedBlockBottomRight =
2231 m_selectedBlockCorner = wxGridNoCellCoords;
2232
2233 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
2234 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2235
2236 m_editable = true; // default for whole grid
2237
2238 m_inOnKeyDown = false;
2239 m_batchCount = 0;
2240
2241 m_extraWidth =
2242 m_extraHeight = 0;
2243
2244 m_scrollLineX = GRID_SCROLL_LINE_X;
2245 m_scrollLineY = GRID_SCROLL_LINE_Y;
2246 }
2247
2248 // ----------------------------------------------------------------------------
2249 // the idea is to call these functions only when necessary because they create
2250 // quite big arrays which eat memory mostly unnecessary - in particular, if
2251 // default widths/heights are used for all rows/columns, we may not use these
2252 // arrays at all
2253 //
2254 // with some extra code, it should be possible to only store the widths/heights
2255 // different from default ones (resulting in space savings for huge grids) but
2256 // this is not done currently
2257 // ----------------------------------------------------------------------------
2258
2259 void wxGrid::InitRowHeights()
2260 {
2261 m_rowHeights.Empty();
2262 m_rowBottoms.Empty();
2263
2264 m_rowHeights.Alloc( m_numRows );
2265 m_rowBottoms.Alloc( m_numRows );
2266
2267 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
2268
2269 int rowBottom = 0;
2270 for ( int i = 0; i < m_numRows; i++ )
2271 {
2272 rowBottom += m_defaultRowHeight;
2273 m_rowBottoms.Add( rowBottom );
2274 }
2275 }
2276
2277 void wxGrid::InitColWidths()
2278 {
2279 m_colWidths.Empty();
2280 m_colRights.Empty();
2281
2282 m_colWidths.Alloc( m_numCols );
2283 m_colRights.Alloc( m_numCols );
2284
2285 m_colWidths.Add( m_defaultColWidth, m_numCols );
2286
2287 for ( int i = 0; i < m_numCols; i++ )
2288 {
2289 int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
2290 m_colRights.Add( colRight );
2291 }
2292 }
2293
2294 int wxGrid::GetColWidth(int col) const
2295 {
2296 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
2297 }
2298
2299 int wxGrid::GetColLeft(int col) const
2300 {
2301 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
2302 : m_colRights[col] - m_colWidths[col];
2303 }
2304
2305 int wxGrid::GetColRight(int col) const
2306 {
2307 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
2308 : m_colRights[col];
2309 }
2310
2311 int wxGrid::GetRowHeight(int row) const
2312 {
2313 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
2314 }
2315
2316 int wxGrid::GetRowTop(int row) const
2317 {
2318 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
2319 : m_rowBottoms[row] - m_rowHeights[row];
2320 }
2321
2322 int wxGrid::GetRowBottom(int row) const
2323 {
2324 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
2325 : m_rowBottoms[row];
2326 }
2327
2328 void wxGrid::CalcDimensions()
2329 {
2330 // compute the size of the scrollable area
2331 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
2332 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
2333
2334 w += m_extraWidth;
2335 h += m_extraHeight;
2336
2337 // take into account editor if shown
2338 if ( IsCellEditControlShown() )
2339 {
2340 int w2, h2;
2341 int r = m_currentCellCoords.GetRow();
2342 int c = m_currentCellCoords.GetCol();
2343 int x = GetColLeft(c);
2344 int y = GetRowTop(r);
2345
2346 // how big is the editor
2347 wxGridCellAttr* attr = GetCellAttr(r, c);
2348 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
2349 editor->GetControl()->GetSize(&w2, &h2);
2350 w2 += x;
2351 h2 += y;
2352 if ( w2 > w )
2353 w = w2;
2354 if ( h2 > h )
2355 h = h2;
2356 editor->DecRef();
2357 attr->DecRef();
2358 }
2359
2360 // preserve (more or less) the previous position
2361 int x, y;
2362 GetViewStart( &x, &y );
2363
2364 // ensure the position is valid for the new scroll ranges
2365 if ( x >= w )
2366 x = wxMax( w - 1, 0 );
2367 if ( y >= h )
2368 y = wxMax( h - 1, 0 );
2369
2370 // update the virtual size and refresh the scrollbars to reflect it
2371 m_gridWin->SetVirtualSize(w, h);
2372 Scroll(x, y);
2373 AdjustScrollbars();
2374
2375 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2376 // still must reposition the children
2377 CalcWindowSizes();
2378 }
2379
2380 wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size)
2381 {
2382 wxSize sizeGridWin(size);
2383 sizeGridWin.x -= m_rowLabelWidth;
2384 sizeGridWin.y -= m_colLabelHeight;
2385
2386 return sizeGridWin;
2387 }
2388
2389 void wxGrid::CalcWindowSizes()
2390 {
2391 // escape if the window is has not been fully created yet
2392
2393 if ( m_cornerLabelWin == NULL )
2394 return;
2395
2396 int cw, ch;
2397 GetClientSize( &cw, &ch );
2398
2399 // the grid may be too small to have enough space for the labels yet, don't
2400 // size the windows to negative sizes in this case
2401 int gw = cw - m_rowLabelWidth;
2402 int gh = ch - m_colLabelHeight;
2403 if (gw < 0)
2404 gw = 0;
2405 if (gh < 0)
2406 gh = 0;
2407
2408 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
2409 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
2410
2411 if ( m_colWindow && m_colWindow->IsShown() )
2412 m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight );
2413
2414 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
2415 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh );
2416
2417 if ( m_gridWin && m_gridWin->IsShown() )
2418 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh );
2419 }
2420
2421 // this is called when the grid table sends a message
2422 // to indicate that it has been redimensioned
2423 //
2424 bool wxGrid::Redimension( wxGridTableMessage& msg )
2425 {
2426 int i;
2427 bool result = false;
2428
2429 // Clear the attribute cache as the attribute might refer to a different
2430 // cell than stored in the cache after adding/removing rows/columns.
2431 ClearAttrCache();
2432
2433 // By the same reasoning, the editor should be dismissed if columns are
2434 // added or removed. And for consistency, it should IMHO always be
2435 // removed, not only if the cell "underneath" it actually changes.
2436 // For now, I intentionally do not save the editor's content as the
2437 // cell it might want to save that stuff to might no longer exist.
2438 HideCellEditControl();
2439
2440 switch ( msg.GetId() )
2441 {
2442 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
2443 {
2444 size_t pos = msg.GetCommandInt();
2445 int numRows = msg.GetCommandInt2();
2446
2447 m_numRows += numRows;
2448
2449 if ( !m_rowHeights.IsEmpty() )
2450 {
2451 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
2452 m_rowBottoms.Insert( 0, pos, numRows );
2453
2454 int bottom = 0;
2455 if ( pos > 0 )
2456 bottom = m_rowBottoms[pos - 1];
2457
2458 for ( i = pos; i < m_numRows; i++ )
2459 {
2460 bottom += m_rowHeights[i];
2461 m_rowBottoms[i] = bottom;
2462 }
2463 }
2464
2465 if ( m_currentCellCoords == wxGridNoCellCoords )
2466 {
2467 // if we have just inserted cols into an empty grid the current
2468 // cell will be undefined...
2469 //
2470 SetCurrentCell( 0, 0 );
2471 }
2472
2473 if ( m_selection )
2474 m_selection->UpdateRows( pos, numRows );
2475 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2476 if (attrProvider)
2477 attrProvider->UpdateAttrRows( pos, numRows );
2478
2479 if ( !GetBatchCount() )
2480 {
2481 CalcDimensions();
2482 m_rowLabelWin->Refresh();
2483 }
2484 }
2485 result = true;
2486 break;
2487
2488 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
2489 {
2490 int numRows = msg.GetCommandInt();
2491 int oldNumRows = m_numRows;
2492 m_numRows += numRows;
2493
2494 if ( !m_rowHeights.IsEmpty() )
2495 {
2496 m_rowHeights.Add( m_defaultRowHeight, numRows );
2497 m_rowBottoms.Add( 0, numRows );
2498
2499 int bottom = 0;
2500 if ( oldNumRows > 0 )
2501 bottom = m_rowBottoms[oldNumRows - 1];
2502
2503 for ( i = oldNumRows; i < m_numRows; i++ )
2504 {
2505 bottom += m_rowHeights[i];
2506 m_rowBottoms[i] = bottom;
2507 }
2508 }
2509
2510 if ( m_currentCellCoords == wxGridNoCellCoords )
2511 {
2512 // if we have just inserted cols into an empty grid the current
2513 // cell will be undefined...
2514 //
2515 SetCurrentCell( 0, 0 );
2516 }
2517
2518 if ( !GetBatchCount() )
2519 {
2520 CalcDimensions();
2521 m_rowLabelWin->Refresh();
2522 }
2523 }
2524 result = true;
2525 break;
2526
2527 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
2528 {
2529 size_t pos = msg.GetCommandInt();
2530 int numRows = msg.GetCommandInt2();
2531 m_numRows -= numRows;
2532
2533 if ( !m_rowHeights.IsEmpty() )
2534 {
2535 m_rowHeights.RemoveAt( pos, numRows );
2536 m_rowBottoms.RemoveAt( pos, numRows );
2537
2538 int h = 0;
2539 for ( i = 0; i < m_numRows; i++ )
2540 {
2541 h += m_rowHeights[i];
2542 m_rowBottoms[i] = h;
2543 }
2544 }
2545
2546 if ( !m_numRows )
2547 {
2548 m_currentCellCoords = wxGridNoCellCoords;
2549 }
2550 else
2551 {
2552 if ( m_currentCellCoords.GetRow() >= m_numRows )
2553 m_currentCellCoords.Set( 0, 0 );
2554 }
2555
2556 if ( m_selection )
2557 m_selection->UpdateRows( pos, -((int)numRows) );
2558 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2559 if (attrProvider)
2560 {
2561 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
2562
2563 // ifdef'd out following patch from Paul Gammans
2564 #if 0
2565 // No need to touch column attributes, unless we
2566 // removed _all_ rows, in this case, we remove
2567 // all column attributes.
2568 // I hate to do this here, but the
2569 // needed data is not available inside UpdateAttrRows.
2570 if ( !GetNumberRows() )
2571 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
2572 #endif
2573 }
2574
2575 if ( !GetBatchCount() )
2576 {
2577 CalcDimensions();
2578 m_rowLabelWin->Refresh();
2579 }
2580 }
2581 result = true;
2582 break;
2583
2584 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
2585 {
2586 size_t pos = msg.GetCommandInt();
2587 int numCols = msg.GetCommandInt2();
2588 m_numCols += numCols;
2589
2590 if ( m_useNativeHeader )
2591 GetGridColHeader()->SetColumnCount(m_numCols);
2592
2593 if ( !m_colAt.IsEmpty() )
2594 {
2595 //Shift the column IDs
2596 int i;
2597 for ( i = 0; i < m_numCols - numCols; i++ )
2598 {
2599 if ( m_colAt[i] >= (int)pos )
2600 m_colAt[i] += numCols;
2601 }
2602
2603 m_colAt.Insert( pos, pos, numCols );
2604
2605 //Set the new columns' positions
2606 for ( i = pos + 1; i < (int)pos + numCols; i++ )
2607 {
2608 m_colAt[i] = i;
2609 }
2610 }
2611
2612 if ( !m_colWidths.IsEmpty() )
2613 {
2614 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
2615 m_colRights.Insert( 0, pos, numCols );
2616
2617 int right = 0;
2618 if ( pos > 0 )
2619 right = m_colRights[GetColAt( pos - 1 )];
2620
2621 int colPos;
2622 for ( colPos = pos; colPos < m_numCols; colPos++ )
2623 {
2624 i = GetColAt( colPos );
2625
2626 right += m_colWidths[i];
2627 m_colRights[i] = right;
2628 }
2629 }
2630
2631 if ( m_currentCellCoords == wxGridNoCellCoords )
2632 {
2633 // if we have just inserted cols into an empty grid the current
2634 // cell will be undefined...
2635 //
2636 SetCurrentCell( 0, 0 );
2637 }
2638
2639 if ( m_selection )
2640 m_selection->UpdateCols( pos, numCols );
2641 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2642 if (attrProvider)
2643 attrProvider->UpdateAttrCols( pos, numCols );
2644 if ( !GetBatchCount() )
2645 {
2646 CalcDimensions();
2647 m_colWindow->Refresh();
2648 }
2649 }
2650 result = true;
2651 break;
2652
2653 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
2654 {
2655 int numCols = msg.GetCommandInt();
2656 int oldNumCols = m_numCols;
2657 m_numCols += numCols;
2658 if ( m_useNativeHeader )
2659 GetGridColHeader()->SetColumnCount(m_numCols);
2660
2661 if ( !m_colAt.IsEmpty() )
2662 {
2663 m_colAt.Add( 0, numCols );
2664
2665 //Set the new columns' positions
2666 int i;
2667 for ( i = oldNumCols; i < m_numCols; i++ )
2668 {
2669 m_colAt[i] = i;
2670 }
2671 }
2672
2673 if ( !m_colWidths.IsEmpty() )
2674 {
2675 m_colWidths.Add( m_defaultColWidth, numCols );
2676 m_colRights.Add( 0, numCols );
2677
2678 int right = 0;
2679 if ( oldNumCols > 0 )
2680 right = m_colRights[GetColAt( oldNumCols - 1 )];
2681
2682 int colPos;
2683 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
2684 {
2685 i = GetColAt( colPos );
2686
2687 right += m_colWidths[i];
2688 m_colRights[i] = right;
2689 }
2690 }
2691
2692 if ( m_currentCellCoords == wxGridNoCellCoords )
2693 {
2694 // if we have just inserted cols into an empty grid the current
2695 // cell will be undefined...
2696 //
2697 SetCurrentCell( 0, 0 );
2698 }
2699 if ( !GetBatchCount() )
2700 {
2701 CalcDimensions();
2702 m_colWindow->Refresh();
2703 }
2704 }
2705 result = true;
2706 break;
2707
2708 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
2709 {
2710 size_t pos = msg.GetCommandInt();
2711 int numCols = msg.GetCommandInt2();
2712 m_numCols -= numCols;
2713 if ( m_useNativeHeader )
2714 GetGridColHeader()->SetColumnCount(m_numCols);
2715
2716 if ( !m_colAt.IsEmpty() )
2717 {
2718 int colID = GetColAt( pos );
2719
2720 m_colAt.RemoveAt( pos, numCols );
2721
2722 //Shift the column IDs
2723 int colPos;
2724 for ( colPos = 0; colPos < m_numCols; colPos++ )
2725 {
2726 if ( m_colAt[colPos] > colID )
2727 m_colAt[colPos] -= numCols;
2728 }
2729 }
2730
2731 if ( !m_colWidths.IsEmpty() )
2732 {
2733 m_colWidths.RemoveAt( pos, numCols );
2734 m_colRights.RemoveAt( pos, numCols );
2735
2736 int w = 0;
2737 int colPos;
2738 for ( colPos = 0; colPos < m_numCols; colPos++ )
2739 {
2740 i = GetColAt( colPos );
2741
2742 w += m_colWidths[i];
2743 m_colRights[i] = w;
2744 }
2745 }
2746
2747 if ( !m_numCols )
2748 {
2749 m_currentCellCoords = wxGridNoCellCoords;
2750 }
2751 else
2752 {
2753 if ( m_currentCellCoords.GetCol() >= m_numCols )
2754 m_currentCellCoords.Set( 0, 0 );
2755 }
2756
2757 if ( m_selection )
2758 m_selection->UpdateCols( pos, -((int)numCols) );
2759 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2760 if (attrProvider)
2761 {
2762 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
2763
2764 // ifdef'd out following patch from Paul Gammans
2765 #if 0
2766 // No need to touch row attributes, unless we
2767 // removed _all_ columns, in this case, we remove
2768 // all row attributes.
2769 // I hate to do this here, but the
2770 // needed data is not available inside UpdateAttrCols.
2771 if ( !GetNumberCols() )
2772 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
2773 #endif
2774 }
2775
2776 if ( !GetBatchCount() )
2777 {
2778 CalcDimensions();
2779 m_colWindow->Refresh();
2780 }
2781 }
2782 result = true;
2783 break;
2784 }
2785
2786 if (result && !GetBatchCount() )
2787 m_gridWin->Refresh();
2788
2789 return result;
2790 }
2791
2792 wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
2793 {
2794 wxRegionIterator iter( reg );
2795 wxRect r;
2796
2797 wxArrayInt rowlabels;
2798
2799 int top, bottom;
2800 while ( iter )
2801 {
2802 r = iter.GetRect();
2803
2804 // TODO: remove this when we can...
2805 // There is a bug in wxMotif that gives garbage update
2806 // rectangles if you jump-scroll a long way by clicking the
2807 // scrollbar with middle button. This is a work-around
2808 //
2809 #if defined(__WXMOTIF__)
2810 int cw, ch;
2811 m_gridWin->GetClientSize( &cw, &ch );
2812 if ( r.GetTop() > ch )
2813 r.SetTop( 0 );
2814 r.SetBottom( wxMin( r.GetBottom(), ch ) );
2815 #endif
2816
2817 // logical bounds of update region
2818 //
2819 int dummy;
2820 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
2821 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
2822
2823 // find the row labels within these bounds
2824 //
2825 int row;
2826 for ( row = internalYToRow(top); row < m_numRows; row++ )
2827 {
2828 if ( GetRowBottom(row) < top )
2829 continue;
2830
2831 if ( GetRowTop(row) > bottom )
2832 break;
2833
2834 rowlabels.Add( row );
2835 }
2836
2837 ++iter;
2838 }
2839
2840 return rowlabels;
2841 }
2842
2843 wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
2844 {
2845 wxRegionIterator iter( reg );
2846 wxRect r;
2847
2848 wxArrayInt colLabels;
2849
2850 int left, right;
2851 while ( iter )
2852 {
2853 r = iter.GetRect();
2854
2855 // TODO: remove this when we can...
2856 // There is a bug in wxMotif that gives garbage update
2857 // rectangles if you jump-scroll a long way by clicking the
2858 // scrollbar with middle button. This is a work-around
2859 //
2860 #if defined(__WXMOTIF__)
2861 int cw, ch;
2862 m_gridWin->GetClientSize( &cw, &ch );
2863 if ( r.GetLeft() > cw )
2864 r.SetLeft( 0 );
2865 r.SetRight( wxMin( r.GetRight(), cw ) );
2866 #endif
2867
2868 // logical bounds of update region
2869 //
2870 int dummy;
2871 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
2872 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
2873
2874 // find the cells within these bounds
2875 //
2876 int col;
2877 int colPos;
2878 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2879 {
2880 col = GetColAt( colPos );
2881
2882 if ( GetColRight(col) < left )
2883 continue;
2884
2885 if ( GetColLeft(col) > right )
2886 break;
2887
2888 colLabels.Add( col );
2889 }
2890
2891 ++iter;
2892 }
2893
2894 return colLabels;
2895 }
2896
2897 wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
2898 {
2899 wxRegionIterator iter( reg );
2900 wxRect r;
2901
2902 wxGridCellCoordsArray cellsExposed;
2903
2904 int left, top, right, bottom;
2905 while ( iter )
2906 {
2907 r = iter.GetRect();
2908
2909 // TODO: remove this when we can...
2910 // There is a bug in wxMotif that gives garbage update
2911 // rectangles if you jump-scroll a long way by clicking the
2912 // scrollbar with middle button. This is a work-around
2913 //
2914 #if defined(__WXMOTIF__)
2915 int cw, ch;
2916 m_gridWin->GetClientSize( &cw, &ch );
2917 if ( r.GetTop() > ch ) r.SetTop( 0 );
2918 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
2919 r.SetRight( wxMin( r.GetRight(), cw ) );
2920 r.SetBottom( wxMin( r.GetBottom(), ch ) );
2921 #endif
2922
2923 // logical bounds of update region
2924 //
2925 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
2926 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
2927
2928 // find the cells within these bounds
2929 wxArrayInt cols;
2930 for ( int row = internalYToRow(top); row < m_numRows; row++ )
2931 {
2932 if ( GetRowBottom(row) <= top )
2933 continue;
2934
2935 if ( GetRowTop(row) > bottom )
2936 break;
2937
2938 // add all dirty cells in this row: notice that the columns which
2939 // are dirty don't depend on the row so we compute them only once
2940 // for the first dirty row and then reuse for all the next ones
2941 if ( cols.empty() )
2942 {
2943 // do determine the dirty columns
2944 for ( int pos = XToPos(left); pos <= XToPos(right); pos++ )
2945 cols.push_back(GetColAt(pos));
2946
2947 // if there are no dirty columns at all, nothing to do
2948 if ( cols.empty() )
2949 break;
2950 }
2951
2952 const size_t count = cols.size();
2953 for ( size_t n = 0; n < count; n++ )
2954 cellsExposed.Add(wxGridCellCoords(row, cols[n]));
2955 }
2956
2957 ++iter;
2958 }
2959
2960 return cellsExposed;
2961 }
2962
2963
2964 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
2965 {
2966 int x, y, row;
2967 wxPoint pos( event.GetPosition() );
2968 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
2969
2970 if ( event.Dragging() )
2971 {
2972 if (!m_isDragging)
2973 {
2974 m_isDragging = true;
2975 m_rowLabelWin->CaptureMouse();
2976 }
2977
2978 if ( event.LeftIsDown() )
2979 {
2980 switch ( m_cursorMode )
2981 {
2982 case WXGRID_CURSOR_RESIZE_ROW:
2983 {
2984 int cw, ch, left, dummy;
2985 m_gridWin->GetClientSize( &cw, &ch );
2986 CalcUnscrolledPosition( 0, 0, &left, &dummy );
2987
2988 wxClientDC dc( m_gridWin );
2989 PrepareDC( dc );
2990 y = wxMax( y,
2991 GetRowTop(m_dragRowOrCol) +
2992 GetRowMinimalHeight(m_dragRowOrCol) );
2993 dc.SetLogicalFunction(wxINVERT);
2994 if ( m_dragLastPos >= 0 )
2995 {
2996 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
2997 }
2998 dc.DrawLine( left, y, left+cw, y );
2999 m_dragLastPos = y;
3000 }
3001 break;
3002
3003 case WXGRID_CURSOR_SELECT_ROW:
3004 {
3005 if ( (row = YToRow( y )) >= 0 )
3006 {
3007 if ( m_selection )
3008 m_selection->SelectRow(row, event);
3009 }
3010 }
3011 break;
3012
3013 // default label to suppress warnings about "enumeration value
3014 // 'xxx' not handled in switch
3015 default:
3016 break;
3017 }
3018 }
3019 return;
3020 }
3021
3022 if ( m_isDragging && (event.Entering() || event.Leaving()) )
3023 return;
3024
3025 if (m_isDragging)
3026 {
3027 if (m_rowLabelWin->HasCapture())
3028 m_rowLabelWin->ReleaseMouse();
3029 m_isDragging = false;
3030 }
3031
3032 // ------------ Entering or leaving the window
3033 //
3034 if ( event.Entering() || event.Leaving() )
3035 {
3036 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3037 }
3038
3039 // ------------ Left button pressed
3040 //
3041 else if ( event.LeftDown() )
3042 {
3043 row = YToEdgeOfRow(y);
3044 if ( row != wxNOT_FOUND && CanDragRowSize(row) )
3045 {
3046 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
3047 }
3048 else // not a request to start resizing
3049 {
3050 row = YToRow(y);
3051 if ( row >= 0 &&
3052 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
3053 {
3054 if ( !event.ShiftDown() && !event.CmdDown() )
3055 ClearSelection();
3056 if ( m_selection )
3057 {
3058 if ( event.ShiftDown() )
3059 {
3060 m_selection->SelectBlock
3061 (
3062 m_currentCellCoords.GetRow(), 0,
3063 row, GetNumberCols() - 1,
3064 event
3065 );
3066 }
3067 else
3068 {
3069 m_selection->SelectRow(row, event);
3070 }
3071 }
3072
3073 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
3074 }
3075 }
3076 }
3077
3078 // ------------ Left double click
3079 //
3080 else if (event.LeftDClick() )
3081 {
3082 row = YToEdgeOfRow(y);
3083 if ( row != wxNOT_FOUND && CanDragRowSize(row) )
3084 {
3085 // adjust row height depending on label text
3086 //
3087 // TODO: generate RESIZING event, see #10754
3088 AutoSizeRowLabelSize( row );
3089
3090 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, row, -1, event);
3091
3092 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
3093 m_dragLastPos = -1;
3094 }
3095 else // not on row separator or it's not resizeable
3096 {
3097 row = YToRow(y);
3098 if ( row >=0 &&
3099 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
3100 {
3101 // no default action at the moment
3102 }
3103 }
3104 }
3105
3106 // ------------ Left button released
3107 //
3108 else if ( event.LeftUp() )
3109 {
3110 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3111 DoEndDragResizeRow(event);
3112
3113 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3114 m_dragLastPos = -1;
3115 }
3116
3117 // ------------ Right button down
3118 //
3119 else if ( event.RightDown() )
3120 {
3121 row = YToRow(y);
3122 if ( row >=0 &&
3123 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
3124 {
3125 // no default action at the moment
3126 }
3127 }
3128
3129 // ------------ Right double click
3130 //
3131 else if ( event.RightDClick() )
3132 {
3133 row = YToRow(y);
3134 if ( row >= 0 &&
3135 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
3136 {
3137 // no default action at the moment
3138 }
3139 }
3140
3141 // ------------ No buttons down and mouse moving
3142 //
3143 else if ( event.Moving() )
3144 {
3145 m_dragRowOrCol = YToEdgeOfRow( y );
3146 if ( m_dragRowOrCol != wxNOT_FOUND )
3147 {
3148 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3149 {
3150 if ( CanDragRowSize(m_dragRowOrCol) )
3151 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
3152 }
3153 }
3154 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3155 {
3156 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
3157 }
3158 }
3159 }
3160
3161 void wxGrid::UpdateColumnSortingIndicator(int col)
3162 {
3163 wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" );
3164
3165 if ( m_useNativeHeader )
3166 GetGridColHeader()->UpdateColumn(col);
3167 else if ( m_nativeColumnLabels )
3168 m_colWindow->Refresh();
3169 //else: sorting indicator display not yet implemented in grid version
3170 }
3171
3172 void wxGrid::SetSortingColumn(int col, bool ascending)
3173 {
3174 if ( col == m_sortCol )
3175 {
3176 // we are already using this column for sorting (or not sorting at all)
3177 // but we might still change the sorting order, check for it
3178 if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending )
3179 {
3180 m_sortIsAscending = ascending;
3181
3182 UpdateColumnSortingIndicator(m_sortCol);
3183 }
3184 }
3185 else // we're changing the column used for sorting
3186 {
3187 const int sortColOld = m_sortCol;
3188
3189 // change it before updating the column as we want GetSortingColumn()
3190 // to return the correct new value
3191 m_sortCol = col;
3192
3193 if ( sortColOld != wxNOT_FOUND )
3194 UpdateColumnSortingIndicator(sortColOld);
3195
3196 if ( m_sortCol != wxNOT_FOUND )
3197 {
3198 m_sortIsAscending = ascending;
3199 UpdateColumnSortingIndicator(m_sortCol);
3200 }
3201 }
3202 }
3203
3204 void wxGrid::DoColHeaderClick(int col)
3205 {
3206 // we consider that the grid was resorted if this event is processed and
3207 // not vetoed
3208 if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 )
3209 {
3210 SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true);
3211 Refresh();
3212 }
3213 }
3214
3215 void wxGrid::DoStartResizeCol(int col)
3216 {
3217 m_dragRowOrCol = col;
3218 m_dragLastPos = -1;
3219 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol));
3220 }
3221
3222 void wxGrid::DoUpdateResizeCol(int x)
3223 {
3224 int cw, ch, dummy, top;
3225 m_gridWin->GetClientSize( &cw, &ch );
3226 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3227
3228 wxClientDC dc( m_gridWin );
3229 PrepareDC( dc );
3230
3231 x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol));
3232 dc.SetLogicalFunction(wxINVERT);
3233 if ( m_dragLastPos >= 0 )
3234 {
3235 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
3236 }
3237 dc.DrawLine( x, top, x, top + ch );
3238 m_dragLastPos = x;
3239 }
3240
3241 void wxGrid::DoUpdateResizeColWidth(int w)
3242 {
3243 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w);
3244 }
3245
3246 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3247 {
3248 int x, y;
3249 wxPoint pos( event.GetPosition() );
3250 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3251
3252 int col = XToCol(x);
3253 if ( event.Dragging() )
3254 {
3255 if (!m_isDragging)
3256 {
3257 m_isDragging = true;
3258 GetColLabelWindow()->CaptureMouse();
3259
3260 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 )
3261 DoStartMoveCol(col);
3262 }
3263
3264 if ( event.LeftIsDown() )
3265 {
3266 switch ( m_cursorMode )
3267 {
3268 case WXGRID_CURSOR_RESIZE_COL:
3269 DoUpdateResizeCol(x);
3270 break;
3271
3272 case WXGRID_CURSOR_SELECT_COL:
3273 {
3274 if ( col != -1 )
3275 {
3276 if ( m_selection )
3277 m_selection->SelectCol(col, event);
3278 }
3279 }
3280 break;
3281
3282 case WXGRID_CURSOR_MOVE_COL:
3283 {
3284 int posNew = XToPos(x);
3285 int colNew = GetColAt(posNew);
3286
3287 // determine the position of the drop marker
3288 int markerX;
3289 if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) )
3290 markerX = GetColRight(colNew);
3291 else
3292 markerX = GetColLeft(colNew);
3293
3294 if ( markerX != m_dragLastPos )
3295 {
3296 wxClientDC dc( GetColLabelWindow() );
3297 DoPrepareDC(dc);
3298
3299 int cw, ch;
3300 GetColLabelWindow()->GetClientSize( &cw, &ch );
3301
3302 markerX++;
3303
3304 //Clean up the last indicator
3305 if ( m_dragLastPos >= 0 )
3306 {
3307 wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3308 dc.SetPen(pen);
3309 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
3310 dc.SetPen(wxNullPen);
3311
3312 if ( XToCol( m_dragLastPos ) != -1 )
3313 DrawColLabel( dc, XToCol( m_dragLastPos ) );
3314 }
3315
3316 const wxColour *color;
3317 //Moving to the same place? Don't draw a marker
3318 if ( colNew == m_dragRowOrCol )
3319 color = wxLIGHT_GREY;
3320 else
3321 color = wxBLUE;
3322
3323 //Draw the marker
3324 wxPen pen( *color, 2 );
3325 dc.SetPen(pen);
3326
3327 dc.DrawLine( markerX, 0, markerX, ch );
3328
3329 dc.SetPen(wxNullPen);
3330
3331 m_dragLastPos = markerX - 1;
3332 }
3333 }
3334 break;
3335
3336 // default label to suppress warnings about "enumeration value
3337 // 'xxx' not handled in switch
3338 default:
3339 break;
3340 }
3341 }
3342 return;
3343 }
3344
3345 if ( m_isDragging && (event.Entering() || event.Leaving()) )
3346 return;
3347
3348 if (m_isDragging)
3349 {
3350 if (GetColLabelWindow()->HasCapture())
3351 GetColLabelWindow()->ReleaseMouse();
3352 m_isDragging = false;
3353 }
3354
3355 // ------------ Entering or leaving the window
3356 //
3357 if ( event.Entering() || event.Leaving() )
3358 {
3359 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
3360 }
3361
3362 // ------------ Left button pressed
3363 //
3364 else if ( event.LeftDown() )
3365 {
3366 int col = XToEdgeOfCol(x);
3367 if ( col != wxNOT_FOUND && CanDragColSize(col) )
3368 {
3369 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow());
3370 }
3371 else // not a request to start resizing
3372 {
3373 col = XToCol(x);
3374 if ( col >= 0 &&
3375 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
3376 {
3377 if ( m_canDragColMove )
3378 {
3379 //Show button as pressed
3380 wxClientDC dc( GetColLabelWindow() );
3381 int colLeft = GetColLeft( col );
3382 int colRight = GetColRight( col ) - 1;
3383 dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3384 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
3385 dc.DrawLine( colLeft, 1, colRight, 1 );
3386
3387 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow());
3388 }
3389 else
3390 {
3391 if ( !event.ShiftDown() && !event.CmdDown() )
3392 ClearSelection();
3393 if ( m_selection )
3394 {
3395 if ( event.ShiftDown() )
3396 {
3397 m_selection->SelectBlock
3398 (
3399 0, m_currentCellCoords.GetCol(),
3400 GetNumberRows() - 1, col,
3401 event
3402 );
3403 }
3404 else
3405 {
3406 m_selection->SelectCol(col, event);
3407 }
3408 }
3409
3410 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow());
3411 }
3412 }
3413 }
3414 }
3415
3416 // ------------ Left double click
3417 //
3418 if ( event.LeftDClick() )
3419 {
3420 const int colEdge = XToEdgeOfCol(x);
3421 if ( colEdge == -1 )
3422 {
3423 if ( col >= 0 &&
3424 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
3425 {
3426 // no default action at the moment
3427 }
3428 }
3429 else
3430 {
3431 // adjust column width depending on label text
3432 //
3433 // TODO: generate RESIZING event, see #10754
3434 AutoSizeColLabelSize( colEdge );
3435
3436 SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event);
3437
3438 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
3439 m_dragLastPos = -1;
3440 }
3441 }
3442
3443 // ------------ Left button released
3444 //
3445 else if ( event.LeftUp() )
3446 {
3447 switch ( m_cursorMode )
3448 {
3449 case WXGRID_CURSOR_RESIZE_COL:
3450 DoEndDragResizeCol(event);
3451 break;
3452
3453 case WXGRID_CURSOR_MOVE_COL:
3454 if ( m_dragLastPos == -1 || col == m_dragRowOrCol )
3455 {
3456 // the column didn't actually move anywhere
3457 if ( col != -1 )
3458 DoColHeaderClick(col);
3459 m_colWindow->Refresh(); // "unpress" the column
3460 }
3461 else
3462 {
3463 DoEndMoveCol(XToPos(x));
3464 }
3465 break;
3466
3467 case WXGRID_CURSOR_SELECT_COL:
3468 case WXGRID_CURSOR_SELECT_CELL:
3469 case WXGRID_CURSOR_RESIZE_ROW:
3470 case WXGRID_CURSOR_SELECT_ROW:
3471 if ( col != -1 )
3472 DoColHeaderClick(col);
3473 break;
3474 }
3475
3476 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
3477 m_dragLastPos = -1;
3478 }
3479
3480 // ------------ Right button down
3481 //
3482 else if ( event.RightDown() )
3483 {
3484 if ( col >= 0 &&
3485 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
3486 {
3487 // no default action at the moment
3488 }
3489 }
3490
3491 // ------------ Right double click
3492 //
3493 else if ( event.RightDClick() )
3494 {
3495 if ( col >= 0 &&
3496 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
3497 {
3498 // no default action at the moment
3499 }
3500 }
3501
3502 // ------------ No buttons down and mouse moving
3503 //
3504 else if ( event.Moving() )
3505 {
3506 m_dragRowOrCol = XToEdgeOfCol( x );
3507 if ( m_dragRowOrCol >= 0 )
3508 {
3509 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3510 {
3511 if ( CanDragColSize(m_dragRowOrCol) )
3512 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false);
3513 }
3514 }
3515 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3516 {
3517 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false);
3518 }
3519 }
3520 }
3521
3522 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
3523 {
3524 if ( event.LeftDown() )
3525 {
3526 // indicate corner label by having both row and
3527 // col args == -1
3528 //
3529 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
3530 {
3531 SelectAll();
3532 }
3533 }
3534 else if ( event.LeftDClick() )
3535 {
3536 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
3537 }
3538 else if ( event.RightDown() )
3539 {
3540 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
3541 {
3542 // no default action at the moment
3543 }
3544 }
3545 else if ( event.RightDClick() )
3546 {
3547 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
3548 {
3549 // no default action at the moment
3550 }
3551 }
3552 }
3553
3554 void wxGrid::CancelMouseCapture()
3555 {
3556 // cancel operation currently in progress, whatever it is
3557 if ( m_winCapture )
3558 {
3559 m_isDragging = false;
3560 m_startDragPos = wxDefaultPosition;
3561
3562 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
3563 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
3564 m_winCapture = NULL;
3565
3566 // remove traces of whatever we drew on screen
3567 Refresh();
3568 }
3569 }
3570
3571 void wxGrid::ChangeCursorMode(CursorMode mode,
3572 wxWindow *win,
3573 bool captureMouse)
3574 {
3575 #if wxUSE_LOG_TRACE
3576 static const wxChar *cursorModes[] =
3577 {
3578 wxT("SELECT_CELL"),
3579 wxT("RESIZE_ROW"),
3580 wxT("RESIZE_COL"),
3581 wxT("SELECT_ROW"),
3582 wxT("SELECT_COL"),
3583 wxT("MOVE_COL"),
3584 };
3585
3586 wxLogTrace(wxT("grid"),
3587 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3588 win == m_colWindow ? wxT("colLabelWin")
3589 : win ? wxT("rowLabelWin")
3590 : wxT("gridWin"),
3591 cursorModes[m_cursorMode], cursorModes[mode]);
3592 #endif // wxUSE_LOG_TRACE
3593
3594 if ( mode == m_cursorMode &&
3595 win == m_winCapture &&
3596 captureMouse == (m_winCapture != NULL))
3597 return;
3598
3599 if ( !win )
3600 {
3601 // by default use the grid itself
3602 win = m_gridWin;
3603 }
3604
3605 if ( m_winCapture )
3606 {
3607 m_winCapture->ReleaseMouse();
3608 m_winCapture = NULL;
3609 }
3610
3611 m_cursorMode = mode;
3612
3613 switch ( m_cursorMode )
3614 {
3615 case WXGRID_CURSOR_RESIZE_ROW:
3616 win->SetCursor( m_rowResizeCursor );
3617 break;
3618
3619 case WXGRID_CURSOR_RESIZE_COL:
3620 win->SetCursor( m_colResizeCursor );
3621 break;
3622
3623 case WXGRID_CURSOR_MOVE_COL:
3624 win->SetCursor( wxCursor(wxCURSOR_HAND) );
3625 break;
3626
3627 default:
3628 win->SetCursor( *wxSTANDARD_CURSOR );
3629 break;
3630 }
3631
3632 // we need to capture mouse when resizing
3633 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
3634 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
3635
3636 if ( captureMouse && resize )
3637 {
3638 win->CaptureMouse();
3639 m_winCapture = win;
3640 }
3641 }
3642
3643 // ----------------------------------------------------------------------------
3644 // grid mouse event processing
3645 // ----------------------------------------------------------------------------
3646
3647 void
3648 wxGrid::DoGridCellDrag(wxMouseEvent& event,
3649 const wxGridCellCoords& coords,
3650 bool isFirstDrag)
3651 {
3652 if ( coords == wxGridNoCellCoords )
3653 return; // we're outside any valid cell
3654
3655 // Hide the edit control, so it won't interfere with drag-shrinking.
3656 if ( IsCellEditControlShown() )
3657 {
3658 HideCellEditControl();
3659 SaveEditControlValue();
3660 }
3661
3662 switch ( event.GetModifiers() )
3663 {
3664 case wxMOD_CMD:
3665 if ( m_selectedBlockCorner == wxGridNoCellCoords)
3666 m_selectedBlockCorner = coords;
3667 UpdateBlockBeingSelected(m_selectedBlockCorner, coords);
3668 break;
3669
3670 case wxMOD_NONE:
3671 if ( CanDragCell() )
3672 {
3673 if ( isFirstDrag )
3674 {
3675 if ( m_selectedBlockCorner == wxGridNoCellCoords)
3676 m_selectedBlockCorner = coords;
3677
3678 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event);
3679 return;
3680 }
3681 }
3682
3683 UpdateBlockBeingSelected(m_currentCellCoords, coords);
3684 break;
3685
3686 default:
3687 // we don't handle the other key modifiers
3688 event.Skip();
3689 }
3690 }
3691
3692 void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper)
3693 {
3694 wxClientDC dc(m_gridWin);
3695 PrepareDC(dc);
3696 dc.SetLogicalFunction(wxINVERT);
3697
3698 const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3699 m_gridWin->GetClientSize());
3700
3701 // erase the previously drawn line, if any
3702 if ( m_dragLastPos >= 0 )
3703 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
3704
3705 // we need the vertical position for rows and horizontal for columns here
3706 m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition()));
3707
3708 // don't allow resizing beneath the minimal size
3709 const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) +
3710 oper.GetMinimalLineSize(this, m_dragRowOrCol);
3711 if ( m_dragLastPos < posMin )
3712 m_dragLastPos = posMin;
3713
3714 // and draw it at the new position
3715 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
3716 }
3717
3718 void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords)
3719 {
3720 if ( !m_isDragging )
3721 {
3722 // Don't start doing anything until the mouse has been dragged far
3723 // enough
3724 const wxPoint& pt = event.GetPosition();
3725 if ( m_startDragPos == wxDefaultPosition )
3726 {
3727 m_startDragPos = pt;
3728 return;
3729 }
3730
3731 if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY &&
3732 abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY )
3733 return;
3734 }
3735
3736 const bool isFirstDrag = !m_isDragging;
3737 m_isDragging = true;
3738
3739 switch ( m_cursorMode )
3740 {
3741 case WXGRID_CURSOR_SELECT_CELL:
3742 DoGridCellDrag(event, coords, isFirstDrag);
3743 break;
3744
3745 case WXGRID_CURSOR_RESIZE_ROW:
3746 DoGridLineDrag(event, wxGridRowOperations());
3747 break;
3748
3749 case WXGRID_CURSOR_RESIZE_COL:
3750 DoGridLineDrag(event, wxGridColumnOperations());
3751 break;
3752
3753 default:
3754 event.Skip();
3755 }
3756
3757 if ( isFirstDrag )
3758 {
3759 m_winCapture = m_gridWin;
3760 m_winCapture->CaptureMouse();
3761 }
3762 }
3763
3764 void
3765 wxGrid::DoGridCellLeftDown(wxMouseEvent& event,
3766 const wxGridCellCoords& coords,
3767 const wxPoint& pos)
3768 {
3769 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) )
3770 {
3771 // event handled by user code, no need to do anything here
3772 return;
3773 }
3774
3775 if ( !event.CmdDown() )
3776 ClearSelection();
3777
3778 if ( event.ShiftDown() )
3779 {
3780 if ( m_selection )
3781 {
3782 m_selection->SelectBlock(m_currentCellCoords, coords, event);
3783 m_selectedBlockCorner = coords;
3784 }
3785 }
3786 else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
3787 {
3788 DisableCellEditControl();
3789 MakeCellVisible( coords );
3790
3791 if ( event.CmdDown() )
3792 {
3793 if ( m_selection )
3794 {
3795 m_selection->ToggleCellSelection(coords, event);
3796 }
3797
3798 m_selectedBlockTopLeft = wxGridNoCellCoords;
3799 m_selectedBlockBottomRight = wxGridNoCellCoords;
3800 m_selectedBlockCorner = coords;
3801 }
3802 else
3803 {
3804 m_waitForSlowClick = m_currentCellCoords == coords &&
3805 coords != wxGridNoCellCoords;
3806 SetCurrentCell( coords );
3807 }
3808 }
3809 }
3810
3811 void
3812 wxGrid::DoGridCellLeftDClick(wxMouseEvent& event,
3813 const wxGridCellCoords& coords,
3814 const wxPoint& pos)
3815 {
3816 if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
3817 {
3818 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) )
3819 {
3820 // we want double click to select a cell and start editing
3821 // (i.e. to behave in same way as sequence of two slow clicks):
3822 m_waitForSlowClick = true;
3823 }
3824 }
3825 }
3826
3827 void
3828 wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords)
3829 {
3830 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3831 {
3832 if (m_winCapture)
3833 {
3834 m_winCapture->ReleaseMouse();
3835 m_winCapture = NULL;
3836 }
3837
3838 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
3839 {
3840 ClearSelection();
3841 EnableCellEditControl();
3842
3843 wxGridCellAttr *attr = GetCellAttr(coords);
3844 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
3845 editor->StartingClick();
3846 editor->DecRef();
3847 attr->DecRef();
3848
3849 m_waitForSlowClick = false;
3850 }
3851 else if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
3852 m_selectedBlockBottomRight != wxGridNoCellCoords )
3853 {
3854 if ( m_selection )
3855 {
3856 m_selection->SelectBlock( m_selectedBlockTopLeft,
3857 m_selectedBlockBottomRight,
3858 event );
3859 }
3860
3861 m_selectedBlockTopLeft = wxGridNoCellCoords;
3862 m_selectedBlockBottomRight = wxGridNoCellCoords;
3863
3864 // Show the edit control, if it has been hidden for
3865 // drag-shrinking.
3866 ShowCellEditControl();
3867 }
3868 }
3869 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3870 {
3871 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3872 DoEndDragResizeRow(event);
3873 }
3874 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3875 {
3876 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3877 DoEndDragResizeCol(event);
3878 }
3879
3880 m_dragLastPos = -1;
3881 }
3882
3883 void
3884 wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event),
3885 const wxGridCellCoords& coords,
3886 const wxPoint& pos)
3887 {
3888 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
3889 {
3890 // out of grid cell area
3891 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3892 return;
3893 }
3894
3895 int dragRow = YToEdgeOfRow( pos.y );
3896 int dragCol = XToEdgeOfCol( pos.x );
3897
3898 // Dragging on the corner of a cell to resize in both
3899 // directions is not implemented yet...
3900 //
3901 if ( dragRow >= 0 && dragCol >= 0 )
3902 {
3903 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3904 return;
3905 }
3906
3907 if ( dragRow >= 0 && CanDragGridSize() && CanDragRowSize(dragRow) )
3908 {
3909 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3910 {
3911 m_dragRowOrCol = dragRow;
3912 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
3913 }
3914 }
3915 // When using the native header window we can only resize the columns by
3916 // dragging the dividers in it because we can't make it enter into the
3917 // column resizing mode programmatically
3918 else if ( dragCol >= 0 && !m_useNativeHeader &&
3919 CanDragGridSize() && CanDragColSize(dragCol) )
3920 {
3921 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3922 {
3923 m_dragRowOrCol = dragCol;
3924 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
3925 }
3926 }
3927 else // Neither on a row or col edge
3928 {
3929 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3930 {
3931 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3932 }
3933 }
3934 }
3935
3936 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
3937 {
3938 const wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
3939
3940 // coordinates of the cell under mouse
3941 wxGridCellCoords coords = XYToCell(pos);
3942
3943 int cell_rows, cell_cols;
3944 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
3945 if ( (cell_rows < 0) || (cell_cols < 0) )
3946 {
3947 coords.SetRow(coords.GetRow() + cell_rows);
3948 coords.SetCol(coords.GetCol() + cell_cols);
3949 }
3950
3951 if ( event.Dragging() )
3952 {
3953 if ( event.LeftIsDown() )
3954 DoGridDragEvent(event, coords);
3955 else
3956 event.Skip();
3957 return;
3958 }
3959
3960 m_isDragging = false;
3961 m_startDragPos = wxDefaultPosition;
3962
3963 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3964 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3965 // wxGTK
3966 #if 0
3967 if ( event.Entering() || event.Leaving() )
3968 {
3969 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3970 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
3971 }
3972 #endif // 0
3973
3974 // deal with various button presses
3975 if ( event.IsButton() )
3976 {
3977 if ( coords != wxGridNoCellCoords )
3978 {
3979 DisableCellEditControl();
3980
3981 if ( event.LeftDown() )
3982 DoGridCellLeftDown(event, coords, pos);
3983 else if ( event.LeftDClick() )
3984 DoGridCellLeftDClick(event, coords, pos);
3985 else if ( event.RightDown() )
3986 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event);
3987 else if ( event.RightDClick() )
3988 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event);
3989 }
3990
3991 // this one should be called even if we're not over any cell
3992 if ( event.LeftUp() )
3993 {
3994 DoGridCellLeftUp(event, coords);
3995 }
3996 }
3997 else if ( event.Moving() )
3998 {
3999 DoGridMouseMoveEvent(event, coords, pos);
4000 }
4001 else // unknown mouse event?
4002 {
4003 event.Skip();
4004 }
4005 }
4006
4007 // this function returns true only if the size really changed
4008 bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper)
4009 {
4010 if ( m_dragLastPos == -1 )
4011 return false;
4012
4013 const wxGridOperations& doper = oper.Dual();
4014
4015 const wxSize size = m_gridWin->GetClientSize();
4016
4017 const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0));
4018
4019 // erase the last line we drew
4020 wxClientDC dc(m_gridWin);
4021 PrepareDC(dc);
4022 dc.SetLogicalFunction(wxINVERT);
4023
4024 const int posLineStart = oper.Select(ptOrigin);
4025 const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size);
4026
4027 oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos);
4028
4029 // temporarily hide the edit control before resizing
4030 HideCellEditControl();
4031 SaveEditControlValue();
4032
4033 // do resize the line
4034 const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol);
4035 const int lineSizeOld = oper.GetLineSize(this, m_dragRowOrCol);
4036 oper.SetLineSize(this, m_dragRowOrCol,
4037 wxMax(m_dragLastPos - lineStart,
4038 oper.GetMinimalLineSize(this, m_dragRowOrCol)));
4039 const bool
4040 sizeChanged = oper.GetLineSize(this, m_dragRowOrCol) != lineSizeOld;
4041
4042 m_dragLastPos = -1;
4043
4044 // refresh now if we're not frozen
4045 if ( !GetBatchCount() )
4046 {
4047 // we need to refresh everything beyond the resized line in the header
4048 // window
4049
4050 // get the position from which to refresh in the other direction
4051 wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0)));
4052 rect.SetPosition(CalcScrolledPosition(rect.GetPosition()));
4053
4054 // we only need the ordinate (for rows) or abscissa (for columns) here,
4055 // and need to cover the entire window in the other direction
4056 oper.Select(rect) = 0;
4057
4058 wxRect rectHeader(rect.GetPosition(),
4059 oper.MakeSize
4060 (
4061 oper.GetHeaderWindowSize(this),
4062 doper.Select(size) - doper.Select(rect)
4063 ));
4064
4065 oper.GetHeaderWindow(this)->Refresh(true, &rectHeader);
4066
4067
4068 // also refresh the grid window: extend the rectangle
4069 if ( m_table )
4070 {
4071 oper.SelectSize(rect) = oper.Select(size);
4072
4073 int subtractLines = 0;
4074 const int lineStart = oper.PosToLine(this, posLineStart);
4075 if ( lineStart >= 0 )
4076 {
4077 // ensure that if we have a multi-cell block we redraw all of
4078 // it by increasing the refresh area to cover it entirely if a
4079 // part of it is affected
4080 const int lineEnd = oper.PosToLine(this, posLineEnd, true);
4081 for ( int line = lineStart; line < lineEnd; line++ )
4082 {
4083 int cellLines = oper.Select(
4084 GetCellSize(oper.MakeCoords(m_dragRowOrCol, line)));
4085 if ( cellLines < subtractLines )
4086 subtractLines = cellLines;
4087 }
4088 }
4089
4090 int startPos =
4091 oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines);
4092 startPos = doper.CalcScrolledPosition(this, startPos);
4093
4094 doper.Select(rect) = startPos;
4095 doper.SelectSize(rect) = doper.Select(size) - startPos;
4096
4097 m_gridWin->Refresh(false, &rect);
4098 }
4099 }
4100
4101 // show the edit control back again
4102 ShowCellEditControl();
4103
4104 return sizeChanged;
4105 }
4106
4107 void wxGrid::DoEndDragResizeRow(const wxMouseEvent& event)
4108 {
4109 // TODO: generate RESIZING event, see #10754
4110
4111 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4112 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event);
4113 }
4114
4115 void wxGrid::DoEndDragResizeCol(const wxMouseEvent& event)
4116 {
4117 // TODO: generate RESIZING event, see #10754
4118
4119 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4120 SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event);
4121 }
4122
4123 void wxGrid::DoStartMoveCol(int col)
4124 {
4125 m_dragRowOrCol = col;
4126 }
4127
4128 void wxGrid::DoEndMoveCol(int pos)
4129 {
4130 wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" );
4131
4132 if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 )
4133 SetColPos(m_dragRowOrCol, pos);
4134 //else: vetoed by user
4135
4136 m_dragRowOrCol = -1;
4137 }
4138
4139 void wxGrid::RefreshAfterColPosChange()
4140 {
4141 // recalculate the column rights as the column positions have changed,
4142 // unless we calculate them dynamically because all columns widths are the
4143 // same and it's easy to do
4144 if ( !m_colWidths.empty() )
4145 {
4146 int colRight = 0;
4147 for ( int colPos = 0; colPos < m_numCols; colPos++ )
4148 {
4149 int colID = GetColAt( colPos );
4150
4151 colRight += m_colWidths[colID];
4152 m_colRights[colID] = colRight;
4153 }
4154 }
4155
4156 // and make the changes visible
4157 if ( m_useNativeHeader )
4158 {
4159 if ( m_colAt.empty() )
4160 GetGridColHeader()->ResetColumnsOrder();
4161 else
4162 GetGridColHeader()->SetColumnsOrder(m_colAt);
4163 }
4164 else
4165 {
4166 m_colWindow->Refresh();
4167 }
4168 m_gridWin->Refresh();
4169 }
4170
4171 void wxGrid::SetColumnsOrder(const wxArrayInt& order)
4172 {
4173 m_colAt = order;
4174
4175 RefreshAfterColPosChange();
4176 }
4177
4178 void wxGrid::SetColPos(int idx, int pos)
4179 {
4180 // we're going to need m_colAt now, initialize it if needed
4181 if ( m_colAt.empty() )
4182 {
4183 m_colAt.reserve(m_numCols);
4184 for ( int i = 0; i < m_numCols; i++ )
4185 m_colAt.push_back(i);
4186 }
4187
4188 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
4189
4190 RefreshAfterColPosChange();
4191 }
4192
4193 void wxGrid::ResetColPos()
4194 {
4195 m_colAt.clear();
4196
4197 RefreshAfterColPosChange();
4198 }
4199
4200 void wxGrid::EnableDragColMove( bool enable )
4201 {
4202 if ( m_canDragColMove == enable )
4203 return;
4204
4205 if ( m_useNativeHeader )
4206 {
4207 // update all columns to make them [not] reorderable
4208 GetGridColHeader()->SetColumnCount(m_numCols);
4209 }
4210
4211 m_canDragColMove = enable;
4212
4213 // we use to call ResetColPos() from here if !enable but this doesn't seem
4214 // right as it would mean there would be no way to "freeze" the current
4215 // columns order by disabling moving them after putting them in the desired
4216 // order, whereas now you can always call ResetColPos() manually if needed
4217 }
4218
4219
4220 //
4221 // ------ interaction with data model
4222 //
4223 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
4224 {
4225 switch ( msg.GetId() )
4226 {
4227 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4228 return GetModelValues();
4229
4230 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4231 return SetModelValues();
4232
4233 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4234 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4235 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4236 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4237 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4238 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4239 return Redimension( msg );
4240
4241 default:
4242 return false;
4243 }
4244 }
4245
4246 // The behaviour of this function depends on the grid table class
4247 // Clear() function. For the default wxGridStringTable class the
4248 // behaviour is to replace all cell contents with wxEmptyString but
4249 // not to change the number of rows or cols.
4250 //
4251 void wxGrid::ClearGrid()
4252 {
4253 if ( m_table )
4254 {
4255 if (IsCellEditControlEnabled())
4256 DisableCellEditControl();
4257
4258 m_table->Clear();
4259 if (!GetBatchCount())
4260 m_gridWin->Refresh();
4261 }
4262 }
4263
4264 bool
4265 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t),
4266 int pos, int num, bool WXUNUSED(updateLabels) )
4267 {
4268 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
4269
4270 if ( !m_table )
4271 return false;
4272
4273 if ( IsCellEditControlEnabled() )
4274 DisableCellEditControl();
4275
4276 return (m_table->*funcModify)(pos, num);
4277
4278 // the table will have sent the results of the insert row
4279 // operation to this view object as a grid table message
4280 }
4281
4282 bool
4283 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t),
4284 int num, bool WXUNUSED(updateLabels))
4285 {
4286 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
4287
4288 if ( !m_table )
4289 return false;
4290
4291 return (m_table->*funcAppend)(num);
4292 }
4293
4294 // ----------------------------------------------------------------------------
4295 // event generation helpers
4296 // ----------------------------------------------------------------------------
4297
4298 void
4299 wxGrid::SendGridSizeEvent(wxEventType type,
4300 int row, int col,
4301 const wxMouseEvent& mouseEv)
4302 {
4303 int rowOrCol = row == -1 ? col : row;
4304
4305 wxGridSizeEvent gridEvt( GetId(),
4306 type,
4307 this,
4308 rowOrCol,
4309 mouseEv.GetX() + GetRowLabelSize(),
4310 mouseEv.GetY() + GetColLabelSize(),
4311 mouseEv);
4312
4313 GetEventHandler()->ProcessEvent(gridEvt);
4314 }
4315
4316 // Generate a grid event based on a mouse event and return:
4317 // -1 if the event was vetoed
4318 // +1 if the event was processed (but not vetoed)
4319 // 0 if the event wasn't handled
4320 int
4321 wxGrid::SendEvent(const wxEventType type,
4322 int row, int col,
4323 const wxMouseEvent& mouseEv)
4324 {
4325 bool claimed, vetoed;
4326
4327 if ( type == wxEVT_GRID_RANGE_SELECT )
4328 {
4329 // Right now, it should _never_ end up here!
4330 wxGridRangeSelectEvent gridEvt( GetId(),
4331 type,
4332 this,
4333 m_selectedBlockTopLeft,
4334 m_selectedBlockBottomRight,
4335 true,
4336 mouseEv);
4337
4338 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4339 vetoed = !gridEvt.IsAllowed();
4340 }
4341 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
4342 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
4343 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
4344 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
4345 {
4346 wxPoint pos = mouseEv.GetPosition();
4347
4348 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
4349 pos.y += GetColLabelSize();
4350 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
4351 pos.x += GetRowLabelSize();
4352
4353 wxGridEvent gridEvt( GetId(),
4354 type,
4355 this,
4356 row, col,
4357 pos.x,
4358 pos.y,
4359 false,
4360 mouseEv);
4361 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4362 vetoed = !gridEvt.IsAllowed();
4363 }
4364 else
4365 {
4366 wxGridEvent gridEvt( GetId(),
4367 type,
4368 this,
4369 row, col,
4370 mouseEv.GetX() + GetRowLabelSize(),
4371 mouseEv.GetY() + GetColLabelSize(),
4372 false,
4373 mouseEv);
4374 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4375 vetoed = !gridEvt.IsAllowed();
4376 }
4377
4378 // A Veto'd event may not be `claimed' so test this first
4379 if (vetoed)
4380 return -1;
4381
4382 return claimed ? 1 : 0;
4383 }
4384
4385 // Generate a grid event of specified type, return value same as above
4386 //
4387 int
4388 wxGrid::SendEvent(const wxEventType type, int row, int col, const wxString& s)
4389 {
4390 wxGridEvent gridEvt( GetId(), type, this, row, col );
4391 gridEvt.SetString(s);
4392
4393 const bool claimed = GetEventHandler()->ProcessEvent(gridEvt);
4394
4395 // A Veto'd event may not be `claimed' so test this first
4396 if ( !gridEvt.IsAllowed() )
4397 return -1;
4398
4399 return claimed ? 1 : 0;
4400 }
4401
4402 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
4403 {
4404 // needed to prevent zillions of paint events on MSW
4405 wxPaintDC dc(this);
4406 }
4407
4408 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
4409 {
4410 // Don't do anything if between Begin/EndBatch...
4411 // EndBatch() will do all this on the last nested one anyway.
4412 if ( m_created && !GetBatchCount() )
4413 {
4414 // Refresh to get correct scrolled position:
4415 wxScrolledWindow::Refresh(eraseb, rect);
4416
4417 if (rect)
4418 {
4419 int rect_x, rect_y, rectWidth, rectHeight;
4420 int width_label, width_cell, height_label, height_cell;
4421 int x, y;
4422
4423 // Copy rectangle can get scroll offsets..
4424 rect_x = rect->GetX();
4425 rect_y = rect->GetY();
4426 rectWidth = rect->GetWidth();
4427 rectHeight = rect->GetHeight();
4428
4429 width_label = m_rowLabelWidth - rect_x;
4430 if (width_label > rectWidth)
4431 width_label = rectWidth;
4432
4433 height_label = m_colLabelHeight - rect_y;
4434 if (height_label > rectHeight)
4435 height_label = rectHeight;
4436
4437 if (rect_x > m_rowLabelWidth)
4438 {
4439 x = rect_x - m_rowLabelWidth;
4440 width_cell = rectWidth;
4441 }
4442 else
4443 {
4444 x = 0;
4445 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
4446 }
4447
4448 if (rect_y > m_colLabelHeight)
4449 {
4450 y = rect_y - m_colLabelHeight;
4451 height_cell = rectHeight;
4452 }
4453 else
4454 {
4455 y = 0;
4456 height_cell = rectHeight - (m_colLabelHeight - rect_y);
4457 }
4458
4459 // Paint corner label part intersecting rect.
4460 if ( width_label > 0 && height_label > 0 )
4461 {
4462 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
4463 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
4464 }
4465
4466 // Paint col labels part intersecting rect.
4467 if ( width_cell > 0 && height_label > 0 )
4468 {
4469 wxRect anotherrect(x, rect_y, width_cell, height_label);
4470 m_colWindow->Refresh(eraseb, &anotherrect);
4471 }
4472
4473 // Paint row labels part intersecting rect.
4474 if ( width_label > 0 && height_cell > 0 )
4475 {
4476 wxRect anotherrect(rect_x, y, width_label, height_cell);
4477 m_rowLabelWin->Refresh(eraseb, &anotherrect);
4478 }
4479
4480 // Paint cell area part intersecting rect.
4481 if ( width_cell > 0 && height_cell > 0 )
4482 {
4483 wxRect anotherrect(x, y, width_cell, height_cell);
4484 m_gridWin->Refresh(eraseb, &anotherrect);
4485 }
4486 }
4487 else
4488 {
4489 m_cornerLabelWin->Refresh(eraseb, NULL);
4490 m_colWindow->Refresh(eraseb, NULL);
4491 m_rowLabelWin->Refresh(eraseb, NULL);
4492 m_gridWin->Refresh(eraseb, NULL);
4493 }
4494 }
4495 }
4496
4497 void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
4498 {
4499 if (m_targetWindow != this) // check whether initialisation has been done
4500 {
4501 // reposition our children windows
4502 CalcWindowSizes();
4503 }
4504 }
4505
4506 void wxGrid::OnKeyDown( wxKeyEvent& event )
4507 {
4508 if ( m_inOnKeyDown )
4509 {
4510 // shouldn't be here - we are going round in circles...
4511 //
4512 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4513 }
4514
4515 m_inOnKeyDown = true;
4516
4517 // propagate the event up and see if it gets processed
4518 wxWindow *parent = GetParent();
4519 wxKeyEvent keyEvt( event );
4520 keyEvt.SetEventObject( parent );
4521
4522 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
4523 {
4524 if (GetLayoutDirection() == wxLayout_RightToLeft)
4525 {
4526 if (event.GetKeyCode() == WXK_RIGHT)
4527 event.m_keyCode = WXK_LEFT;
4528 else if (event.GetKeyCode() == WXK_LEFT)
4529 event.m_keyCode = WXK_RIGHT;
4530 }
4531
4532 // try local handlers
4533 switch ( event.GetKeyCode() )
4534 {
4535 case WXK_UP:
4536 if ( event.ControlDown() )
4537 MoveCursorUpBlock( event.ShiftDown() );
4538 else
4539 MoveCursorUp( event.ShiftDown() );
4540 break;
4541
4542 case WXK_DOWN:
4543 if ( event.ControlDown() )
4544 MoveCursorDownBlock( event.ShiftDown() );
4545 else
4546 MoveCursorDown( event.ShiftDown() );
4547 break;
4548
4549 case WXK_LEFT:
4550 if ( event.ControlDown() )
4551 MoveCursorLeftBlock( event.ShiftDown() );
4552 else
4553 MoveCursorLeft( event.ShiftDown() );
4554 break;
4555
4556 case WXK_RIGHT:
4557 if ( event.ControlDown() )
4558 MoveCursorRightBlock( event.ShiftDown() );
4559 else
4560 MoveCursorRight( event.ShiftDown() );
4561 break;
4562
4563 case WXK_RETURN:
4564 case WXK_NUMPAD_ENTER:
4565 if ( event.ControlDown() )
4566 {
4567 event.Skip(); // to let the edit control have the return
4568 }
4569 else
4570 {
4571 if ( GetGridCursorRow() < GetNumberRows()-1 )
4572 {
4573 MoveCursorDown( event.ShiftDown() );
4574 }
4575 else
4576 {
4577 // at the bottom of a column
4578 DisableCellEditControl();
4579 }
4580 }
4581 break;
4582
4583 case WXK_ESCAPE:
4584 ClearSelection();
4585 break;
4586
4587 case WXK_TAB:
4588 if (event.ShiftDown())
4589 {
4590 if ( GetGridCursorCol() > 0 )
4591 {
4592 MoveCursorLeft( false );
4593 }
4594 else
4595 {
4596 // at left of grid
4597 DisableCellEditControl();
4598 }
4599 }
4600 else
4601 {
4602 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4603 {
4604 MoveCursorRight( false );
4605 }
4606 else
4607 {
4608 // at right of grid
4609 DisableCellEditControl();
4610 }
4611 }
4612 break;
4613
4614 case WXK_HOME:
4615 if ( event.ControlDown() )
4616 {
4617 GoToCell(0, 0);
4618 }
4619 else
4620 {
4621 event.Skip();
4622 }
4623 break;
4624
4625 case WXK_END:
4626 if ( event.ControlDown() )
4627 {
4628 GoToCell(m_numRows - 1, m_numCols - 1);
4629 }
4630 else
4631 {
4632 event.Skip();
4633 }
4634 break;
4635
4636 case WXK_PAGEUP:
4637 MovePageUp();
4638 break;
4639
4640 case WXK_PAGEDOWN:
4641 MovePageDown();
4642 break;
4643
4644 case WXK_SPACE:
4645 // Ctrl-Space selects the current column, Shift-Space -- the
4646 // current row and Ctrl-Shift-Space -- everything
4647 switch ( m_selection ? event.GetModifiers() : wxMOD_NONE )
4648 {
4649 case wxMOD_CONTROL:
4650 m_selection->SelectCol(m_currentCellCoords.GetCol());
4651 break;
4652
4653 case wxMOD_SHIFT:
4654 m_selection->SelectRow(m_currentCellCoords.GetRow());
4655 break;
4656
4657 case wxMOD_CONTROL | wxMOD_SHIFT:
4658 m_selection->SelectBlock(0, 0,
4659 m_numRows - 1, m_numCols - 1);
4660 break;
4661
4662 case wxMOD_NONE:
4663 if ( !IsEditable() )
4664 {
4665 MoveCursorRight(false);
4666 break;
4667 }
4668 //else: fall through
4669
4670 default:
4671 event.Skip();
4672 }
4673 break;
4674
4675 default:
4676 event.Skip();
4677 break;
4678 }
4679 }
4680
4681 m_inOnKeyDown = false;
4682 }
4683
4684 void wxGrid::OnKeyUp( wxKeyEvent& event )
4685 {
4686 // try local handlers
4687 //
4688 if ( event.GetKeyCode() == WXK_SHIFT )
4689 {
4690 if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
4691 m_selectedBlockBottomRight != wxGridNoCellCoords )
4692 {
4693 if ( m_selection )
4694 {
4695 m_selection->SelectBlock(
4696 m_selectedBlockTopLeft,
4697 m_selectedBlockBottomRight,
4698 event);
4699 }
4700 }
4701
4702 m_selectedBlockTopLeft = wxGridNoCellCoords;
4703 m_selectedBlockBottomRight = wxGridNoCellCoords;
4704 m_selectedBlockCorner = wxGridNoCellCoords;
4705 }
4706 }
4707
4708 void wxGrid::OnChar( wxKeyEvent& event )
4709 {
4710 // is it possible to edit the current cell at all?
4711 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4712 {
4713 // yes, now check whether the cells editor accepts the key
4714 int row = m_currentCellCoords.GetRow();
4715 int col = m_currentCellCoords.GetCol();
4716 wxGridCellAttr *attr = GetCellAttr(row, col);
4717 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
4718
4719 // <F2> is special and will always start editing, for
4720 // other keys - ask the editor itself
4721 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
4722 || editor->IsAcceptedKey(event) )
4723 {
4724 // ensure cell is visble
4725 MakeCellVisible(row, col);
4726 EnableCellEditControl();
4727
4728 // a problem can arise if the cell is not completely
4729 // visible (even after calling MakeCellVisible the
4730 // control is not created and calling StartingKey will
4731 // crash the app
4732 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
4733 editor->StartingKey(event);
4734 }
4735 else
4736 {
4737 event.Skip();
4738 }
4739
4740 editor->DecRef();
4741 attr->DecRef();
4742 }
4743 else
4744 {
4745 event.Skip();
4746 }
4747 }
4748
4749 void wxGrid::OnEraseBackground(wxEraseEvent&)
4750 {
4751 }
4752
4753 bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
4754 {
4755 if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 )
4756 {
4757 // the event has been vetoed - do nothing
4758 return false;
4759 }
4760
4761 #if !defined(__WXMAC__)
4762 wxClientDC dc( m_gridWin );
4763 PrepareDC( dc );
4764 #endif
4765
4766 if ( m_currentCellCoords != wxGridNoCellCoords )
4767 {
4768 DisableCellEditControl();
4769
4770 if ( IsVisible( m_currentCellCoords, false ) )
4771 {
4772 wxRect r;
4773 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
4774 if ( !m_gridLinesEnabled )
4775 {
4776 r.x--;
4777 r.y--;
4778 r.width++;
4779 r.height++;
4780 }
4781
4782 wxGridCellCoordsArray cells = CalcCellsExposed( r );
4783
4784 // Otherwise refresh redraws the highlight!
4785 m_currentCellCoords = coords;
4786
4787 #if defined(__WXMAC__)
4788 m_gridWin->Refresh(true /*, & r */);
4789 #else
4790 DrawGridCellArea( dc, cells );
4791 DrawAllGridLines( dc, r );
4792 #endif
4793 }
4794 }
4795
4796 m_currentCellCoords = coords;
4797
4798 wxGridCellAttr *attr = GetCellAttr( coords );
4799 #if !defined(__WXMAC__)
4800 DrawCellHighlight( dc, attr );
4801 #endif
4802 attr->DecRef();
4803
4804 return true;
4805 }
4806
4807 void
4808 wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
4809 int bottomRow, int rightCol)
4810 {
4811 if ( m_selection )
4812 {
4813 switch ( m_selection->GetSelectionMode() )
4814 {
4815 default:
4816 wxFAIL_MSG( "unknown selection mode" );
4817 // fall through
4818
4819 case wxGridSelectCells:
4820 // arbitrary blocks selection allowed so just use the cell
4821 // coordinates as is
4822 break;
4823
4824 case wxGridSelectRows:
4825 // only full rows selection allowd, ensure that we do select
4826 // full rows
4827 leftCol = 0;
4828 rightCol = GetNumberCols() - 1;
4829 break;
4830
4831 case wxGridSelectColumns:
4832 // same as above but for columns
4833 topRow = 0;
4834 bottomRow = GetNumberRows() - 1;
4835 break;
4836
4837 case wxGridSelectRowsOrColumns:
4838 // in this mode we can select only full rows or full columns so
4839 // it doesn't make sense to select blocks at all (and we can't
4840 // extend the block because there is no preferred direction, we
4841 // could only extend it to cover the entire grid but this is
4842 // not useful)
4843 return;
4844 }
4845 }
4846
4847 m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol);
4848 MakeCellVisible(m_selectedBlockCorner);
4849
4850 EnsureFirstLessThanSecond(topRow, bottomRow);
4851 EnsureFirstLessThanSecond(leftCol, rightCol);
4852
4853 wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol),
4854 updateBottomRight = wxGridCellCoords(bottomRow, rightCol);
4855
4856 // First the case that we selected a completely new area
4857 if ( m_selectedBlockTopLeft == wxGridNoCellCoords ||
4858 m_selectedBlockBottomRight == wxGridNoCellCoords )
4859 {
4860 wxRect rect;
4861 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
4862 wxGridCellCoords ( bottomRow, rightCol ) );
4863 m_gridWin->Refresh( false, &rect );
4864 }
4865
4866 // Now handle changing an existing selection area.
4867 else if ( m_selectedBlockTopLeft != updateTopLeft ||
4868 m_selectedBlockBottomRight != updateBottomRight )
4869 {
4870 // Compute two optimal update rectangles:
4871 // Either one rectangle is a real subset of the
4872 // other, or they are (almost) disjoint!
4873 wxRect rect[4];
4874 bool need_refresh[4];
4875 need_refresh[0] =
4876 need_refresh[1] =
4877 need_refresh[2] =
4878 need_refresh[3] = false;
4879 int i;
4880
4881 // Store intermediate values
4882 wxCoord oldLeft = m_selectedBlockTopLeft.GetCol();
4883 wxCoord oldTop = m_selectedBlockTopLeft.GetRow();
4884 wxCoord oldRight = m_selectedBlockBottomRight.GetCol();
4885 wxCoord oldBottom = m_selectedBlockBottomRight.GetRow();
4886
4887 // Determine the outer/inner coordinates.
4888 EnsureFirstLessThanSecond(oldLeft, leftCol);
4889 EnsureFirstLessThanSecond(oldTop, topRow);
4890 EnsureFirstLessThanSecond(rightCol, oldRight);
4891 EnsureFirstLessThanSecond(bottomRow, oldBottom);
4892
4893 // Now, either the stuff marked old is the outer
4894 // rectangle or we don't have a situation where one
4895 // is contained in the other.
4896
4897 if ( oldLeft < leftCol )
4898 {
4899 // Refresh the newly selected or deselected
4900 // area to the left of the old or new selection.
4901 need_refresh[0] = true;
4902 rect[0] = BlockToDeviceRect(
4903 wxGridCellCoords( oldTop, oldLeft ),
4904 wxGridCellCoords( oldBottom, leftCol - 1 ) );
4905 }
4906
4907 if ( oldTop < topRow )
4908 {
4909 // Refresh the newly selected or deselected
4910 // area above the old or new selection.
4911 need_refresh[1] = true;
4912 rect[1] = BlockToDeviceRect(
4913 wxGridCellCoords( oldTop, leftCol ),
4914 wxGridCellCoords( topRow - 1, rightCol ) );
4915 }
4916
4917 if ( oldRight > rightCol )
4918 {
4919 // Refresh the newly selected or deselected
4920 // area to the right of the old or new selection.
4921 need_refresh[2] = true;
4922 rect[2] = BlockToDeviceRect(
4923 wxGridCellCoords( oldTop, rightCol + 1 ),
4924 wxGridCellCoords( oldBottom, oldRight ) );
4925 }
4926
4927 if ( oldBottom > bottomRow )
4928 {
4929 // Refresh the newly selected or deselected
4930 // area below the old or new selection.
4931 need_refresh[3] = true;
4932 rect[3] = BlockToDeviceRect(
4933 wxGridCellCoords( bottomRow + 1, leftCol ),
4934 wxGridCellCoords( oldBottom, rightCol ) );
4935 }
4936
4937 // various Refresh() calls
4938 for (i = 0; i < 4; i++ )
4939 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
4940 m_gridWin->Refresh( false, &(rect[i]) );
4941 }
4942
4943 // change selection
4944 m_selectedBlockTopLeft = updateTopLeft;
4945 m_selectedBlockBottomRight = updateBottomRight;
4946 }
4947
4948 //
4949 // ------ functions to get/send data (see also public functions)
4950 //
4951
4952 bool wxGrid::GetModelValues()
4953 {
4954 // Hide the editor, so it won't hide a changed value.
4955 HideCellEditControl();
4956
4957 if ( m_table )
4958 {
4959 // all we need to do is repaint the grid
4960 //
4961 m_gridWin->Refresh();
4962 return true;
4963 }
4964
4965 return false;
4966 }
4967
4968 bool wxGrid::SetModelValues()
4969 {
4970 int row, col;
4971
4972 // Disable the editor, so it won't hide a changed value.
4973 // Do we also want to save the current value of the editor first?
4974 // I think so ...
4975 DisableCellEditControl();
4976
4977 if ( m_table )
4978 {
4979 for ( row = 0; row < m_numRows; row++ )
4980 {
4981 for ( col = 0; col < m_numCols; col++ )
4982 {
4983 m_table->SetValue( row, col, GetCellValue(row, col) );
4984 }
4985 }
4986
4987 return true;
4988 }
4989
4990 return false;
4991 }
4992
4993 // Note - this function only draws cells that are in the list of
4994 // exposed cells (usually set from the update region by
4995 // CalcExposedCells)
4996 //
4997 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
4998 {
4999 if ( !m_numRows || !m_numCols )
5000 return;
5001
5002 int i, numCells = cells.GetCount();
5003 int row, col, cell_rows, cell_cols;
5004 wxGridCellCoordsArray redrawCells;
5005
5006 for ( i = numCells - 1; i >= 0; i-- )
5007 {
5008 row = cells[i].GetRow();
5009 col = cells[i].GetCol();
5010 GetCellSize( row, col, &cell_rows, &cell_cols );
5011
5012 // If this cell is part of a multicell block, find owner for repaint
5013 if ( cell_rows <= 0 || cell_cols <= 0 )
5014 {
5015 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
5016 bool marked = false;
5017 for ( int j = 0; j < numCells; j++ )
5018 {
5019 if ( cell == cells[j] )
5020 {
5021 marked = true;
5022 break;
5023 }
5024 }
5025
5026 if (!marked)
5027 {
5028 int count = redrawCells.GetCount();
5029 for (int j = 0; j < count; j++)
5030 {
5031 if ( cell == redrawCells[j] )
5032 {
5033 marked = true;
5034 break;
5035 }
5036 }
5037
5038 if (!marked)
5039 redrawCells.Add( cell );
5040 }
5041
5042 // don't bother drawing this cell
5043 continue;
5044 }
5045
5046 // If this cell is empty, find cell to left that might want to overflow
5047 if (m_table && m_table->IsEmptyCell(row, col))
5048 {
5049 for ( int l = 0; l < cell_rows; l++ )
5050 {
5051 // find a cell in this row to leave already marked for repaint
5052 int left = col;
5053 for (int k = 0; k < int(redrawCells.GetCount()); k++)
5054 if ((redrawCells[k].GetCol() < left) &&
5055 (redrawCells[k].GetRow() == row))
5056 {
5057 left = redrawCells[k].GetCol();
5058 }
5059
5060 if (left == col)
5061 left = 0; // oh well
5062
5063 for (int j = col - 1; j >= left; j--)
5064 {
5065 if (!m_table->IsEmptyCell(row + l, j))
5066 {
5067 if (GetCellOverflow(row + l, j))
5068 {
5069 wxGridCellCoords cell(row + l, j);
5070 bool marked = false;
5071
5072 for (int k = 0; k < numCells; k++)
5073 {
5074 if ( cell == cells[k] )
5075 {
5076 marked = true;
5077 break;
5078 }
5079 }
5080
5081 if (!marked)
5082 {
5083 int count = redrawCells.GetCount();
5084 for (int k = 0; k < count; k++)
5085 {
5086 if ( cell == redrawCells[k] )
5087 {
5088 marked = true;
5089 break;
5090 }
5091 }
5092 if (!marked)
5093 redrawCells.Add( cell );
5094 }
5095 }
5096 break;
5097 }
5098 }
5099 }
5100 }
5101
5102 DrawCell( dc, cells[i] );
5103 }
5104
5105 numCells = redrawCells.GetCount();
5106
5107 for ( i = numCells - 1; i >= 0; i-- )
5108 {
5109 DrawCell( dc, redrawCells[i] );
5110 }
5111 }
5112
5113 void wxGrid::DrawGridSpace( wxDC& dc )
5114 {
5115 int cw, ch;
5116 m_gridWin->GetClientSize( &cw, &ch );
5117
5118 int right, bottom;
5119 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5120
5121 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
5122 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
5123
5124 if ( right > rightCol || bottom > bottomRow )
5125 {
5126 int left, top;
5127 CalcUnscrolledPosition( 0, 0, &left, &top );
5128
5129 dc.SetBrush(GetDefaultCellBackgroundColour());
5130 dc.SetPen( *wxTRANSPARENT_PEN );
5131
5132 if ( right > rightCol )
5133 {
5134 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
5135 }
5136
5137 if ( bottom > bottomRow )
5138 {
5139 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
5140 }
5141 }
5142 }
5143
5144 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
5145 {
5146 int row = coords.GetRow();
5147 int col = coords.GetCol();
5148
5149 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5150 return;
5151
5152 // we draw the cell border ourselves
5153 wxGridCellAttr* attr = GetCellAttr(row, col);
5154
5155 bool isCurrent = coords == m_currentCellCoords;
5156
5157 wxRect rect = CellToRect( row, col );
5158
5159 // if the editor is shown, we should use it and not the renderer
5160 // Note: However, only if it is really _shown_, i.e. not hidden!
5161 if ( isCurrent && IsCellEditControlShown() )
5162 {
5163 // NB: this "#if..." is temporary and fixes a problem where the
5164 // edit control is erased by this code after being rendered.
5165 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5166 // implicitly, causing this out-of order render.
5167 #if !defined(__WXMAC__)
5168 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5169 editor->PaintBackground(rect, attr);
5170 editor->DecRef();
5171 #endif
5172 }
5173 else
5174 {
5175 // but all the rest is drawn by the cell renderer and hence may be customized
5176 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
5177 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
5178 renderer->DecRef();
5179 }
5180
5181 attr->DecRef();
5182 }
5183
5184 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
5185 {
5186 // don't show highlight when the grid doesn't have focus
5187 if ( !HasFocus() )
5188 return;
5189
5190 int row = m_currentCellCoords.GetRow();
5191 int col = m_currentCellCoords.GetCol();
5192
5193 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5194 return;
5195
5196 wxRect rect = CellToRect(row, col);
5197
5198 // hmmm... what could we do here to show that the cell is disabled?
5199 // for now, I just draw a thinner border than for the other ones, but
5200 // it doesn't look really good
5201
5202 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
5203
5204 if (penWidth > 0)
5205 {
5206 // The center of the drawn line is where the position/width/height of
5207 // the rectangle is actually at (on wxMSW at least), so the
5208 // size of the rectangle is reduced to compensate for the thickness of
5209 // the line. If this is too strange on non-wxMSW platforms then
5210 // please #ifdef this appropriately.
5211 rect.x += penWidth / 2;
5212 rect.y += penWidth / 2;
5213 rect.width -= penWidth - 1;
5214 rect.height -= penWidth - 1;
5215
5216 // Now draw the rectangle
5217 // use the cellHighlightColour if the cell is inside a selection, this
5218 // will ensure the cell is always visible.
5219 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground
5220 : m_cellHighlightColour,
5221 penWidth));
5222 dc.SetBrush(*wxTRANSPARENT_BRUSH);
5223 dc.DrawRectangle(rect);
5224 }
5225 }
5226
5227 wxPen wxGrid::GetDefaultGridLinePen()
5228 {
5229 return wxPen(GetGridLineColour());
5230 }
5231
5232 wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
5233 {
5234 return GetDefaultGridLinePen();
5235 }
5236
5237 wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
5238 {
5239 return GetDefaultGridLinePen();
5240 }
5241
5242 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5243 {
5244 int row = coords.GetRow();
5245 int col = coords.GetCol();
5246 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5247 return;
5248
5249
5250 wxRect rect = CellToRect( row, col );
5251
5252 // right hand border
5253 dc.SetPen( GetColGridLinePen(col) );
5254 dc.DrawLine( rect.x + rect.width, rect.y,
5255 rect.x + rect.width, rect.y + rect.height + 1 );
5256
5257 // bottom border
5258 dc.SetPen( GetRowGridLinePen(row) );
5259 dc.DrawLine( rect.x, rect.y + rect.height,
5260 rect.x + rect.width, rect.y + rect.height);
5261 }
5262
5263 void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
5264 {
5265 // This if block was previously in wxGrid::OnPaint but that doesn't
5266 // seem to get called under wxGTK - MB
5267 //
5268 if ( m_currentCellCoords == wxGridNoCellCoords &&
5269 m_numRows && m_numCols )
5270 {
5271 m_currentCellCoords.Set(0, 0);
5272 }
5273
5274 if ( IsCellEditControlShown() )
5275 {
5276 // don't show highlight when the edit control is shown
5277 return;
5278 }
5279
5280 // if the active cell was repainted, repaint its highlight too because it
5281 // might have been damaged by the grid lines
5282 size_t count = cells.GetCount();
5283 for ( size_t n = 0; n < count; n++ )
5284 {
5285 wxGridCellCoords cell = cells[n];
5286
5287 // If we are using attributes, then we may have just exposed another
5288 // cell in a partially-visible merged cluster of cells. If the "anchor"
5289 // (upper left) cell of this merged cluster is the cell indicated by
5290 // m_currentCellCoords, then we need to refresh the cell highlight even
5291 // though the "anchor" itself is not part of our update segment.
5292 if ( CanHaveAttributes() )
5293 {
5294 int rows = 0,
5295 cols = 0;
5296 GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols);
5297
5298 if ( rows < 0 )
5299 cell.SetRow(cell.GetRow() + rows);
5300
5301 if ( cols < 0 )
5302 cell.SetCol(cell.GetCol() + cols);
5303 }
5304
5305 if ( cell == m_currentCellCoords )
5306 {
5307 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5308 DrawCellHighlight(dc, attr);
5309 attr->DecRef();
5310
5311 break;
5312 }
5313 }
5314 }
5315
5316 // This is used to redraw all grid lines e.g. when the grid line colour
5317 // has been changed
5318 //
5319 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
5320 {
5321 if ( !m_gridLinesEnabled )
5322 return;
5323
5324 int top, bottom, left, right;
5325
5326 int cw, ch;
5327 m_gridWin->GetClientSize(&cw, &ch);
5328 CalcUnscrolledPosition( 0, 0, &left, &top );
5329 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5330
5331 // avoid drawing grid lines past the last row and col
5332 if ( m_gridLinesClipHorz )
5333 {
5334 if ( !m_numCols )
5335 return;
5336
5337 const int lastColRight = GetColRight(GetColAt(m_numCols - 1));
5338 if ( right > lastColRight )
5339 right = lastColRight;
5340 }
5341
5342 if ( m_gridLinesClipVert )
5343 {
5344 if ( !m_numRows )
5345 return;
5346
5347 const int lastRowBottom = GetRowBottom(m_numRows - 1);
5348 if ( bottom > lastRowBottom )
5349 bottom = lastRowBottom;
5350 }
5351
5352 // no gridlines inside multicells, clip them out
5353 int leftCol = GetColPos( internalXToCol(left) );
5354 int topRow = internalYToRow(top);
5355 int rightCol = GetColPos( internalXToCol(right) );
5356 int bottomRow = internalYToRow(bottom);
5357
5358 wxRegion clippedcells(0, 0, cw, ch);
5359
5360 int cell_rows, cell_cols;
5361 wxRect rect;
5362
5363 for ( int j = topRow; j <= bottomRow; j++ )
5364 {
5365 for ( int colPos = leftCol; colPos <= rightCol; colPos++ )
5366 {
5367 int i = GetColAt( colPos );
5368
5369 GetCellSize( j, i, &cell_rows, &cell_cols );
5370 if ((cell_rows > 1) || (cell_cols > 1))
5371 {
5372 rect = CellToRect(j,i);
5373 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5374 clippedcells.Subtract(rect);
5375 }
5376 else if ((cell_rows < 0) || (cell_cols < 0))
5377 {
5378 rect = CellToRect(j + cell_rows, i + cell_cols);
5379 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5380 clippedcells.Subtract(rect);
5381 }
5382 }
5383 }
5384
5385 dc.SetDeviceClippingRegion( clippedcells );
5386
5387
5388 // horizontal grid lines
5389 for ( int i = internalYToRow(top); i < m_numRows; i++ )
5390 {
5391 int bot = GetRowBottom(i) - 1;
5392
5393 if ( bot > bottom )
5394 break;
5395
5396 if ( bot >= top )
5397 {
5398 dc.SetPen( GetRowGridLinePen(i) );
5399 dc.DrawLine( left, bot, right, bot );
5400 }
5401 }
5402
5403 // vertical grid lines
5404 for ( int colPos = leftCol; colPos < m_numCols; colPos++ )
5405 {
5406 int i = GetColAt( colPos );
5407
5408 int colRight = GetColRight(i);
5409 #ifdef __WXGTK__
5410 if (GetLayoutDirection() != wxLayout_RightToLeft)
5411 #endif
5412 colRight--;
5413
5414 if ( colRight > right )
5415 break;
5416
5417 if ( colRight >= left )
5418 {
5419 dc.SetPen( GetColGridLinePen(i) );
5420 dc.DrawLine( colRight, top, colRight, bottom );
5421 }
5422 }
5423
5424 dc.DestroyClippingRegion();
5425 }
5426
5427 void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
5428 {
5429 if ( !m_numRows )
5430 return;
5431
5432 const size_t numLabels = rows.GetCount();
5433 for ( size_t i = 0; i < numLabels; i++ )
5434 {
5435 DrawRowLabel( dc, rows[i] );
5436 }
5437 }
5438
5439 void wxGrid::DrawRowLabel( wxDC& dc, int row )
5440 {
5441 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
5442 return;
5443
5444 wxGridCellAttrProvider * const
5445 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
5446 const wxGridRowHeaderRenderer&
5447 rend = attrProvider ? attrProvider->GetRowHeaderRenderer(row)
5448 : gs_defaultHeaderRenderers.rowRenderer;
5449 wxRect rect(0, GetRowTop(row), m_rowLabelWidth, GetRowHeight(row));
5450 rend.DrawBorder(*this, dc, rect);
5451
5452 int hAlign, vAlign;
5453 GetRowLabelAlignment(&hAlign, &vAlign);
5454
5455 rend.DrawLabel(*this, dc, GetRowLabelValue(row),
5456 rect, hAlign, vAlign, wxHORIZONTAL);
5457 }
5458
5459 void wxGrid::UseNativeColHeader(bool native)
5460 {
5461 if ( native == m_useNativeHeader )
5462 return;
5463
5464 delete m_colWindow;
5465 m_useNativeHeader = native;
5466
5467 CreateColumnWindow();
5468
5469 if ( m_useNativeHeader )
5470 GetGridColHeader()->SetColumnCount(m_numCols);
5471 CalcWindowSizes();
5472 }
5473
5474 void wxGrid::SetUseNativeColLabels( bool native )
5475 {
5476 wxASSERT_MSG( !m_useNativeHeader,
5477 "doesn't make sense when using native header" );
5478
5479 m_nativeColumnLabels = native;
5480 if (native)
5481 {
5482 int height = wxRendererNative::Get().GetHeaderButtonHeight( this );
5483 SetColLabelSize( height );
5484 }
5485
5486 GetColLabelWindow()->Refresh();
5487 m_cornerLabelWin->Refresh();
5488 }
5489
5490 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
5491 {
5492 if ( !m_numCols )
5493 return;
5494
5495 const size_t numLabels = cols.GetCount();
5496 for ( size_t i = 0; i < numLabels; i++ )
5497 {
5498 DrawColLabel( dc, cols[i] );
5499 }
5500 }
5501
5502 void wxGrid::DrawCornerLabel(wxDC& dc)
5503 {
5504 wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight));
5505
5506 if ( m_nativeColumnLabels )
5507 {
5508 rect.Deflate(1);
5509
5510 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0);
5511 }
5512 else
5513 {
5514 rect.width++;
5515 rect.height++;
5516
5517 wxGridCellAttrProvider * const
5518 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
5519 const wxGridCornerHeaderRenderer&
5520 rend = attrProvider ? attrProvider->GetCornerRenderer()
5521 : gs_defaultHeaderRenderers.cornerRenderer;
5522
5523 rend.DrawBorder(*this, dc, rect);
5524 }
5525 }
5526
5527 void wxGrid::DrawColLabel(wxDC& dc, int col)
5528 {
5529 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
5530 return;
5531
5532 int colLeft = GetColLeft(col);
5533
5534 wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight);
5535 wxGridCellAttrProvider * const
5536 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
5537 const wxGridColumnHeaderRenderer&
5538 rend = attrProvider ? attrProvider->GetColumnHeaderRenderer(col)
5539 : gs_defaultHeaderRenderers.colRenderer;
5540
5541 if ( m_nativeColumnLabels )
5542 {
5543 wxRendererNative::Get().DrawHeaderButton
5544 (
5545 GetColLabelWindow(),
5546 dc,
5547 rect,
5548 0,
5549 IsSortingBy(col)
5550 ? IsSortOrderAscending()
5551 ? wxHDR_SORT_ICON_UP
5552 : wxHDR_SORT_ICON_DOWN
5553 : wxHDR_SORT_ICON_NONE
5554 );
5555 rect.Deflate(2);
5556 }
5557 else
5558 {
5559 rend.DrawBorder(*this, dc, rect);
5560 }
5561
5562 int hAlign, vAlign;
5563 GetColLabelAlignment(&hAlign, &vAlign);
5564 const int orient = GetColLabelTextOrientation();
5565
5566 rend.DrawLabel(*this, dc, GetColLabelValue(col), rect, hAlign, vAlign, orient);
5567 }
5568
5569 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5570 // we just have to add textOrientation support
5571 void wxGrid::DrawTextRectangle( wxDC& dc,
5572 const wxString& value,
5573 const wxRect& rect,
5574 int horizAlign,
5575 int vertAlign,
5576 int textOrientation ) const
5577 {
5578 wxArrayString lines;
5579
5580 StringToLines( value, lines );
5581
5582 DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation);
5583 }
5584
5585 void wxGrid::DrawTextRectangle(wxDC& dc,
5586 const wxArrayString& lines,
5587 const wxRect& rect,
5588 int horizAlign,
5589 int vertAlign,
5590 int textOrientation) const
5591 {
5592 if ( lines.empty() )
5593 return;
5594
5595 wxDCClipper clip(dc, rect);
5596
5597 long textWidth,
5598 textHeight;
5599
5600 if ( textOrientation == wxHORIZONTAL )
5601 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5602 else
5603 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
5604
5605 int x = 0,
5606 y = 0;
5607 switch ( vertAlign )
5608 {
5609 case wxALIGN_BOTTOM:
5610 if ( textOrientation == wxHORIZONTAL )
5611 y = rect.y + (rect.height - textHeight - 1);
5612 else
5613 x = rect.x + rect.width - textWidth;
5614 break;
5615
5616 case wxALIGN_CENTRE:
5617 if ( textOrientation == wxHORIZONTAL )
5618 y = rect.y + ((rect.height - textHeight) / 2);
5619 else
5620 x = rect.x + ((rect.width - textWidth) / 2);
5621 break;
5622
5623 case wxALIGN_TOP:
5624 default:
5625 if ( textOrientation == wxHORIZONTAL )
5626 y = rect.y + 1;
5627 else
5628 x = rect.x + 1;
5629 break;
5630 }
5631
5632 // Align each line of a multi-line label
5633 size_t nLines = lines.GetCount();
5634 for ( size_t l = 0; l < nLines; l++ )
5635 {
5636 const wxString& line = lines[l];
5637
5638 if ( line.empty() )
5639 {
5640 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
5641 continue;
5642 }
5643
5644 wxCoord lineWidth = 0,
5645 lineHeight = 0;
5646 dc.GetTextExtent(line, &lineWidth, &lineHeight);
5647
5648 switch ( horizAlign )
5649 {
5650 case wxALIGN_RIGHT:
5651 if ( textOrientation == wxHORIZONTAL )
5652 x = rect.x + (rect.width - lineWidth - 1);
5653 else
5654 y = rect.y + lineWidth + 1;
5655 break;
5656
5657 case wxALIGN_CENTRE:
5658 if ( textOrientation == wxHORIZONTAL )
5659 x = rect.x + ((rect.width - lineWidth) / 2);
5660 else
5661 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
5662 break;
5663
5664 case wxALIGN_LEFT:
5665 default:
5666 if ( textOrientation == wxHORIZONTAL )
5667 x = rect.x + 1;
5668 else
5669 y = rect.y + rect.height - 1;
5670 break;
5671 }
5672
5673 if ( textOrientation == wxHORIZONTAL )
5674 {
5675 dc.DrawText( line, x, y );
5676 y += lineHeight;
5677 }
5678 else
5679 {
5680 dc.DrawRotatedText( line, x, y, 90.0 );
5681 x += lineHeight;
5682 }
5683 }
5684 }
5685
5686 // Split multi-line text up into an array of strings.
5687 // Any existing contents of the string array are preserved.
5688 //
5689 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5690 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const
5691 {
5692 int startPos = 0;
5693 int pos;
5694 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
5695 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
5696
5697 while ( startPos < (int)tVal.length() )
5698 {
5699 pos = tVal.Mid(startPos).Find( eol );
5700 if ( pos < 0 )
5701 {
5702 break;
5703 }
5704 else if ( pos == 0 )
5705 {
5706 lines.Add( wxEmptyString );
5707 }
5708 else
5709 {
5710 lines.Add( tVal.Mid(startPos, pos) );
5711 }
5712
5713 startPos += pos + 1;
5714 }
5715
5716 if ( startPos < (int)tVal.length() )
5717 {
5718 lines.Add( tVal.Mid( startPos ) );
5719 }
5720 }
5721
5722 void wxGrid::GetTextBoxSize( const wxDC& dc,
5723 const wxArrayString& lines,
5724 long *width, long *height ) const
5725 {
5726 wxCoord w = 0;
5727 wxCoord h = 0;
5728 wxCoord lineW = 0, lineH = 0;
5729
5730 size_t i;
5731 for ( i = 0; i < lines.GetCount(); i++ )
5732 {
5733 dc.GetTextExtent( lines[i], &lineW, &lineH );
5734 w = wxMax( w, lineW );
5735 h += lineH;
5736 }
5737
5738 *width = w;
5739 *height = h;
5740 }
5741
5742 //
5743 // ------ Batch processing.
5744 //
5745 void wxGrid::EndBatch()
5746 {
5747 if ( m_batchCount > 0 )
5748 {
5749 m_batchCount--;
5750 if ( !m_batchCount )
5751 {
5752 CalcDimensions();
5753 m_rowLabelWin->Refresh();
5754 m_colWindow->Refresh();
5755 m_cornerLabelWin->Refresh();
5756 m_gridWin->Refresh();
5757 }
5758 }
5759 }
5760
5761 // Use this, rather than wxWindow::Refresh(), to force an immediate
5762 // repainting of the grid. Has no effect if you are already inside a
5763 // BeginBatch / EndBatch block.
5764 //
5765 void wxGrid::ForceRefresh()
5766 {
5767 BeginBatch();
5768 EndBatch();
5769 }
5770
5771 bool wxGrid::Enable(bool enable)
5772 {
5773 if ( !wxScrolledWindow::Enable(enable) )
5774 return false;
5775
5776 // redraw in the new state
5777 m_gridWin->Refresh();
5778
5779 return true;
5780 }
5781
5782 //
5783 // ------ Edit control functions
5784 //
5785
5786 void wxGrid::EnableEditing( bool edit )
5787 {
5788 if ( edit != m_editable )
5789 {
5790 if (!edit)
5791 EnableCellEditControl(edit);
5792 m_editable = edit;
5793 }
5794 }
5795
5796 void wxGrid::EnableCellEditControl( bool enable )
5797 {
5798 if (! m_editable)
5799 return;
5800
5801 if ( enable != m_cellEditCtrlEnabled )
5802 {
5803 if ( enable )
5804 {
5805 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 )
5806 return;
5807
5808 // this should be checked by the caller!
5809 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5810
5811 // do it before ShowCellEditControl()
5812 m_cellEditCtrlEnabled = enable;
5813
5814 ShowCellEditControl();
5815 }
5816 else
5817 {
5818 SendEvent(wxEVT_GRID_EDITOR_HIDDEN);
5819
5820 HideCellEditControl();
5821 SaveEditControlValue();
5822
5823 // do it after HideCellEditControl()
5824 m_cellEditCtrlEnabled = enable;
5825 }
5826 }
5827 }
5828
5829 bool wxGrid::IsCurrentCellReadOnly() const
5830 {
5831 // const_cast
5832 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5833 bool readonly = attr->IsReadOnly();
5834 attr->DecRef();
5835
5836 return readonly;
5837 }
5838
5839 bool wxGrid::CanEnableCellControl() const
5840 {
5841 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
5842 !IsCurrentCellReadOnly();
5843 }
5844
5845 bool wxGrid::IsCellEditControlEnabled() const
5846 {
5847 // the cell edit control might be disable for all cells or just for the
5848 // current one if it's read only
5849 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
5850 }
5851
5852 bool wxGrid::IsCellEditControlShown() const
5853 {
5854 bool isShown = false;
5855
5856 if ( m_cellEditCtrlEnabled )
5857 {
5858 int row = m_currentCellCoords.GetRow();
5859 int col = m_currentCellCoords.GetCol();
5860 wxGridCellAttr* attr = GetCellAttr(row, col);
5861 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
5862 attr->DecRef();
5863
5864 if ( editor )
5865 {
5866 if ( editor->IsCreated() )
5867 {
5868 isShown = editor->GetControl()->IsShown();
5869 }
5870
5871 editor->DecRef();
5872 }
5873 }
5874
5875 return isShown;
5876 }
5877
5878 void wxGrid::ShowCellEditControl()
5879 {
5880 if ( IsCellEditControlEnabled() )
5881 {
5882 if ( !IsVisible( m_currentCellCoords, false ) )
5883 {
5884 m_cellEditCtrlEnabled = false;
5885 return;
5886 }
5887 else
5888 {
5889 wxRect rect = CellToRect( m_currentCellCoords );
5890 int row = m_currentCellCoords.GetRow();
5891 int col = m_currentCellCoords.GetCol();
5892
5893 // if this is part of a multicell, find owner (topleft)
5894 int cell_rows, cell_cols;
5895 GetCellSize( row, col, &cell_rows, &cell_cols );
5896 if ( cell_rows <= 0 || cell_cols <= 0 )
5897 {
5898 row += cell_rows;
5899 col += cell_cols;
5900 m_currentCellCoords.SetRow( row );
5901 m_currentCellCoords.SetCol( col );
5902 }
5903
5904 // erase the highlight and the cell contents because the editor
5905 // might not cover the entire cell
5906 wxClientDC dc( m_gridWin );
5907 PrepareDC( dc );
5908 wxGridCellAttr* attr = GetCellAttr(row, col);
5909 dc.SetBrush(wxBrush(attr->GetBackgroundColour()));
5910 dc.SetPen(*wxTRANSPARENT_PEN);
5911 dc.DrawRectangle(rect);
5912
5913 // convert to scrolled coords
5914 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5915
5916 int nXMove = 0;
5917 if (rect.x < 0)
5918 nXMove = rect.x;
5919
5920 // cell is shifted by one pixel
5921 // However, don't allow x or y to become negative
5922 // since the SetSize() method interprets that as
5923 // "don't change."
5924 if (rect.x > 0)
5925 rect.x--;
5926 if (rect.y > 0)
5927 rect.y--;
5928
5929 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5930 if ( !editor->IsCreated() )
5931 {
5932 editor->Create(m_gridWin, wxID_ANY,
5933 new wxGridCellEditorEvtHandler(this, editor));
5934
5935 wxGridEditorCreatedEvent evt(GetId(),
5936 wxEVT_GRID_EDITOR_CREATED,
5937 this,
5938 row,
5939 col,
5940 editor->GetControl());
5941 GetEventHandler()->ProcessEvent(evt);
5942 }
5943
5944 // resize editor to overflow into righthand cells if allowed
5945 int maxWidth = rect.width;
5946 wxString value = GetCellValue(row, col);
5947 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
5948 {
5949 int y;
5950 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
5951 if (maxWidth < rect.width)
5952 maxWidth = rect.width;
5953 }
5954
5955 int client_right = m_gridWin->GetClientSize().GetWidth();
5956 if (rect.x + maxWidth > client_right)
5957 maxWidth = client_right - rect.x;
5958
5959 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
5960 {
5961 GetCellSize( row, col, &cell_rows, &cell_cols );
5962 // may have changed earlier
5963 for (int i = col + cell_cols; i < m_numCols; i++)
5964 {
5965 int c_rows, c_cols;
5966 GetCellSize( row, i, &c_rows, &c_cols );
5967
5968 // looks weird going over a multicell
5969 if (m_table->IsEmptyCell( row, i ) &&
5970 (rect.width < maxWidth) && (c_rows == 1))
5971 {
5972 rect.width += GetColWidth( i );
5973 }
5974 else
5975 break;
5976 }
5977
5978 if (rect.GetRight() > client_right)
5979 rect.SetRight( client_right - 1 );
5980 }
5981
5982 editor->SetCellAttr( attr );
5983 editor->SetSize( rect );
5984 if (nXMove != 0)
5985 editor->GetControl()->Move(
5986 editor->GetControl()->GetPosition().x + nXMove,
5987 editor->GetControl()->GetPosition().y );
5988 editor->Show( true, attr );
5989
5990 // recalc dimensions in case we need to
5991 // expand the scrolled window to account for editor
5992 CalcDimensions();
5993
5994 editor->BeginEdit(row, col, this);
5995 editor->SetCellAttr(NULL);
5996
5997 editor->DecRef();
5998 attr->DecRef();
5999 }
6000 }
6001 }
6002
6003 void wxGrid::HideCellEditControl()
6004 {
6005 if ( IsCellEditControlEnabled() )
6006 {
6007 int row = m_currentCellCoords.GetRow();
6008 int col = m_currentCellCoords.GetCol();
6009
6010 wxGridCellAttr *attr = GetCellAttr(row, col);
6011 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6012 const bool editorHadFocus = editor->GetControl()->HasFocus();
6013 editor->Show( false );
6014 editor->DecRef();
6015 attr->DecRef();
6016
6017 // return the focus to the grid itself if the editor had it
6018 //
6019 // note that we must not do this unconditionally to avoid stealing
6020 // focus from the window which just received it if we are hiding the
6021 // editor precisely because we lost focus
6022 if ( editorHadFocus )
6023 m_gridWin->SetFocus();
6024
6025 // refresh whole row to the right
6026 wxRect rect( CellToRect(row, col) );
6027 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
6028 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
6029
6030 #ifdef __WXMAC__
6031 // ensure that the pixels under the focus ring get refreshed as well
6032 rect.Inflate(10, 10);
6033 #endif
6034
6035 m_gridWin->Refresh( false, &rect );
6036 }
6037 }
6038
6039 void wxGrid::SaveEditControlValue()
6040 {
6041 if ( IsCellEditControlEnabled() )
6042 {
6043 int row = m_currentCellCoords.GetRow();
6044 int col = m_currentCellCoords.GetCol();
6045
6046 wxString oldval = GetCellValue(row, col);
6047
6048 wxGridCellAttr* attr = GetCellAttr(row, col);
6049 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
6050
6051 wxString newval;
6052 bool changed = editor->EndEdit(row, col, this, oldval, &newval);
6053
6054 if ( changed && SendEvent(wxEVT_GRID_CELL_CHANGING, newval) != -1 )
6055 {
6056 editor->ApplyEdit(row, col, this);
6057
6058 // for compatibility reasons dating back to wx 2.8 when this event
6059 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6060 // didn't exist we allow vetoing this one too
6061 if ( SendEvent(wxEVT_GRID_CELL_CHANGED, oldval) == -1 )
6062 {
6063 // Event has been vetoed, set the data back.
6064 SetCellValue(row, col, oldval);
6065 }
6066 }
6067
6068 editor->DecRef();
6069 attr->DecRef();
6070 }
6071 }
6072
6073 //
6074 // ------ Grid location functions
6075 // Note that all of these functions work with the logical coordinates of
6076 // grid cells and labels so you will need to convert from device
6077 // coordinates for mouse events etc.
6078 //
6079
6080 wxGridCellCoords wxGrid::XYToCell(int x, int y) const
6081 {
6082 int row = YToRow(y);
6083 int col = XToCol(x);
6084
6085 return row == -1 || col == -1 ? wxGridNoCellCoords
6086 : wxGridCellCoords(row, col);
6087 }
6088
6089 // compute row or column from some (unscrolled) coordinate value, using either
6090 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6091 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6092 // for large grids)
6093 int wxGrid::PosToLinePos(int coord,
6094 bool clipToMinMax,
6095 const wxGridOperations& oper) const
6096 {
6097 const int numLines = oper.GetNumberOfLines(this);
6098
6099 if ( coord < 0 )
6100 return clipToMinMax && numLines > 0 ? 0 : wxNOT_FOUND;
6101
6102 const int defaultLineSize = oper.GetDefaultLineSize(this);
6103 wxCHECK_MSG( defaultLineSize, -1, "can't have 0 default line size" );
6104
6105 int maxPos = coord / defaultLineSize,
6106 minPos = 0;
6107
6108 // check for the simplest case: if we have no explicit line sizes
6109 // configured, then we already know the line this position falls in
6110 const wxArrayInt& lineEnds = oper.GetLineEnds(this);
6111 if ( lineEnds.empty() )
6112 {
6113 if ( maxPos < numLines )
6114 return maxPos;
6115
6116 return clipToMinMax ? numLines - 1 : -1;
6117 }
6118
6119
6120 // adjust maxPos before starting the binary search
6121 if ( maxPos >= numLines )
6122 {
6123 maxPos = numLines - 1;
6124 }
6125 else
6126 {
6127 if ( coord >= lineEnds[oper.GetLineAt(this, maxPos)])
6128 {
6129 minPos = maxPos;
6130 const int minDist = oper.GetMinimalAcceptableLineSize(this);
6131 if ( minDist )
6132 maxPos = coord / minDist;
6133 else
6134 maxPos = numLines - 1;
6135 }
6136
6137 if ( maxPos >= numLines )
6138 maxPos = numLines - 1;
6139 }
6140
6141 // check if the position is beyond the last column
6142 const int lineAtMaxPos = oper.GetLineAt(this, maxPos);
6143 if ( coord >= lineEnds[lineAtMaxPos] )
6144 return clipToMinMax ? maxPos : -1;
6145
6146 // or before the first one
6147 const int lineAt0 = oper.GetLineAt(this, 0);
6148 if ( coord < lineEnds[lineAt0] )
6149 return 0;
6150
6151
6152 // finally do perform the binary search
6153 while ( minPos < maxPos )
6154 {
6155 wxCHECK_MSG( lineEnds[oper.GetLineAt(this, minPos)] <= coord &&
6156 coord < lineEnds[oper.GetLineAt(this, maxPos)],
6157 -1,
6158 "wxGrid: internal error in PosToLinePos()" );
6159
6160 if ( coord >= lineEnds[oper.GetLineAt(this, maxPos - 1)] )
6161 return maxPos;
6162 else
6163 maxPos--;
6164
6165 const int median = minPos + (maxPos - minPos + 1) / 2;
6166 if ( coord < lineEnds[oper.GetLineAt(this, median)] )
6167 maxPos = median;
6168 else
6169 minPos = median;
6170 }
6171
6172 return maxPos;
6173 }
6174
6175 int
6176 wxGrid::PosToLine(int coord,
6177 bool clipToMinMax,
6178 const wxGridOperations& oper) const
6179 {
6180 int pos = PosToLinePos(coord, clipToMinMax, oper);
6181
6182 return pos == wxNOT_FOUND ? wxNOT_FOUND : oper.GetLineAt(this, pos);
6183 }
6184
6185 int wxGrid::YToRow(int y, bool clipToMinMax) const
6186 {
6187 return PosToLine(y, clipToMinMax, wxGridRowOperations());
6188 }
6189
6190 int wxGrid::XToCol(int x, bool clipToMinMax) const
6191 {
6192 return PosToLine(x, clipToMinMax, wxGridColumnOperations());
6193 }
6194
6195 int wxGrid::XToPos(int x) const
6196 {
6197 return PosToLinePos(x, true /* clip */, wxGridColumnOperations());
6198 }
6199
6200 // return the row number such that the y coord is near the edge of, or -1 if
6201 // not near an edge.
6202 //
6203 // notice that position can only possibly be near an edge if the row/column is
6204 // large enough to still allow for an "inner" area that is _not_ near the edge
6205 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6206 // _never_ be considered to be near the edge).
6207 int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const
6208 {
6209 const int line = oper.PosToLine(this, pos, true);
6210
6211 if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE )
6212 {
6213 // We know that we are in this line, test whether we are close enough
6214 // to start or end border, respectively.
6215 if ( abs(oper.GetLineEndPos(this, line) - pos) < WXGRID_LABEL_EDGE_ZONE )
6216 return line;
6217 else if ( line > 0 &&
6218 pos - oper.GetLineStartPos(this,
6219 line) < WXGRID_LABEL_EDGE_ZONE )
6220 return line - 1;
6221 }
6222
6223 return -1;
6224 }
6225
6226 int wxGrid::YToEdgeOfRow(int y) const
6227 {
6228 return PosToEdgeOfLine(y, wxGridRowOperations());
6229 }
6230
6231 int wxGrid::XToEdgeOfCol(int x) const
6232 {
6233 return PosToEdgeOfLine(x, wxGridColumnOperations());
6234 }
6235
6236 wxRect wxGrid::CellToRect( int row, int col ) const
6237 {
6238 wxRect rect( -1, -1, -1, -1 );
6239
6240 if ( row >= 0 && row < m_numRows &&
6241 col >= 0 && col < m_numCols )
6242 {
6243 int i, cell_rows, cell_cols;
6244 rect.width = rect.height = 0;
6245 GetCellSize( row, col, &cell_rows, &cell_cols );
6246 // if negative then find multicell owner
6247 if (cell_rows < 0)
6248 row += cell_rows;
6249 if (cell_cols < 0)
6250 col += cell_cols;
6251 GetCellSize( row, col, &cell_rows, &cell_cols );
6252
6253 rect.x = GetColLeft(col);
6254 rect.y = GetRowTop(row);
6255 for (i=col; i < col + cell_cols; i++)
6256 rect.width += GetColWidth(i);
6257 for (i=row; i < row + cell_rows; i++)
6258 rect.height += GetRowHeight(i);
6259
6260 // if grid lines are enabled, then the area of the cell is a bit smaller
6261 if (m_gridLinesEnabled)
6262 {
6263 rect.width -= 1;
6264 rect.height -= 1;
6265 }
6266 }
6267
6268 return rect;
6269 }
6270
6271 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const
6272 {
6273 // get the cell rectangle in logical coords
6274 //
6275 wxRect r( CellToRect( row, col ) );
6276
6277 // convert to device coords
6278 //
6279 int left, top, right, bottom;
6280 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6281 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
6282
6283 // check against the client area of the grid window
6284 int cw, ch;
6285 m_gridWin->GetClientSize( &cw, &ch );
6286
6287 if ( wholeCellVisible )
6288 {
6289 // is the cell wholly visible ?
6290 return ( left >= 0 && right <= cw &&
6291 top >= 0 && bottom <= ch );
6292 }
6293 else
6294 {
6295 // is the cell partly visible ?
6296 //
6297 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
6298 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
6299 }
6300 }
6301
6302 // make the specified cell location visible by doing a minimal amount
6303 // of scrolling
6304 //
6305 void wxGrid::MakeCellVisible( int row, int col )
6306 {
6307 int i;
6308 int xpos = -1, ypos = -1;
6309
6310 if ( row >= 0 && row < m_numRows &&
6311 col >= 0 && col < m_numCols )
6312 {
6313 // get the cell rectangle in logical coords
6314 wxRect r( CellToRect( row, col ) );
6315
6316 // convert to device coords
6317 int left, top, right, bottom;
6318 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6319 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
6320
6321 int cw, ch;
6322 m_gridWin->GetClientSize( &cw, &ch );
6323
6324 if ( top < 0 )
6325 {
6326 ypos = r.GetTop();
6327 }
6328 else if ( bottom > ch )
6329 {
6330 int h = r.GetHeight();
6331 ypos = r.GetTop();
6332 for ( i = row - 1; i >= 0; i-- )
6333 {
6334 int rowHeight = GetRowHeight(i);
6335 if ( h + rowHeight > ch )
6336 break;
6337
6338 h += rowHeight;
6339 ypos -= rowHeight;
6340 }
6341
6342 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6343 // have rounding errors (this is important, because if we do,
6344 // we might not scroll at all and some cells won't be redrawn)
6345 //
6346 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6347 // so just add a full scroll unit...
6348 ypos += m_scrollLineY;
6349 }
6350
6351 // special handling for wide cells - show always left part of the cell!
6352 // Otherwise, e.g. when stepping from row to row, it would jump between
6353 // left and right part of the cell on every step!
6354 // if ( left < 0 )
6355 if ( left < 0 || (right - left) >= cw )
6356 {
6357 xpos = r.GetLeft();
6358 }
6359 else if ( right > cw )
6360 {
6361 // position the view so that the cell is on the right
6362 int x0, y0;
6363 CalcUnscrolledPosition(0, 0, &x0, &y0);
6364 xpos = x0 + (right - cw);
6365
6366 // see comment for ypos above
6367 xpos += m_scrollLineX;
6368 }
6369
6370 if ( xpos != -1 || ypos != -1 )
6371 {
6372 if ( xpos != -1 )
6373 xpos /= m_scrollLineX;
6374 if ( ypos != -1 )
6375 ypos /= m_scrollLineY;
6376 Scroll( xpos, ypos );
6377 AdjustScrollbars();
6378 }
6379 }
6380 }
6381
6382 //
6383 // ------ Grid cursor movement functions
6384 //
6385
6386 bool
6387 wxGrid::DoMoveCursor(bool expandSelection,
6388 const wxGridDirectionOperations& diroper)
6389 {
6390 if ( m_currentCellCoords == wxGridNoCellCoords )
6391 return false;
6392
6393 if ( expandSelection )
6394 {
6395 wxGridCellCoords coords = m_selectedBlockCorner;
6396 if ( coords == wxGridNoCellCoords )
6397 coords = m_currentCellCoords;
6398
6399 if ( diroper.IsAtBoundary(coords) )
6400 return false;
6401
6402 diroper.Advance(coords);
6403
6404 UpdateBlockBeingSelected(m_currentCellCoords, coords);
6405 }
6406 else // don't expand selection
6407 {
6408 ClearSelection();
6409
6410 if ( diroper.IsAtBoundary(m_currentCellCoords) )
6411 return false;
6412
6413 wxGridCellCoords coords = m_currentCellCoords;
6414 diroper.Advance(coords);
6415
6416 GoToCell(coords);
6417 }
6418
6419 return true;
6420 }
6421
6422 bool wxGrid::MoveCursorUp(bool expandSelection)
6423 {
6424 return DoMoveCursor(expandSelection,
6425 wxGridBackwardOperations(this, wxGridRowOperations()));
6426 }
6427
6428 bool wxGrid::MoveCursorDown(bool expandSelection)
6429 {
6430 return DoMoveCursor(expandSelection,
6431 wxGridForwardOperations(this, wxGridRowOperations()));
6432 }
6433
6434 bool wxGrid::MoveCursorLeft(bool expandSelection)
6435 {
6436 return DoMoveCursor(expandSelection,
6437 wxGridBackwardOperations(this, wxGridColumnOperations()));
6438 }
6439
6440 bool wxGrid::MoveCursorRight(bool expandSelection)
6441 {
6442 return DoMoveCursor(expandSelection,
6443 wxGridForwardOperations(this, wxGridColumnOperations()));
6444 }
6445
6446 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations& diroper)
6447 {
6448 if ( m_currentCellCoords == wxGridNoCellCoords )
6449 return false;
6450
6451 if ( diroper.IsAtBoundary(m_currentCellCoords) )
6452 return false;
6453
6454 const int oldRow = m_currentCellCoords.GetRow();
6455 int newRow = diroper.MoveByPixelDistance(oldRow, m_gridWin->GetClientSize().y);
6456 if ( newRow == oldRow )
6457 {
6458 wxGridCellCoords coords(m_currentCellCoords);
6459 diroper.Advance(coords);
6460 newRow = coords.GetRow();
6461 }
6462
6463 GoToCell(newRow, m_currentCellCoords.GetCol());
6464
6465 return true;
6466 }
6467
6468 bool wxGrid::MovePageUp()
6469 {
6470 return DoMoveCursorByPage(
6471 wxGridBackwardOperations(this, wxGridRowOperations()));
6472 }
6473
6474 bool wxGrid::MovePageDown()
6475 {
6476 return DoMoveCursorByPage(
6477 wxGridForwardOperations(this, wxGridRowOperations()));
6478 }
6479
6480 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6481 // until we find a non-empty cell or reach the grid end
6482 void
6483 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords,
6484 const wxGridDirectionOperations& diroper)
6485 {
6486 while ( !diroper.IsAtBoundary(coords) )
6487 {
6488 diroper.Advance(coords);
6489 if ( !m_table->IsEmpty(coords) )
6490 break;
6491 }
6492 }
6493
6494 bool
6495 wxGrid::DoMoveCursorByBlock(bool expandSelection,
6496 const wxGridDirectionOperations& diroper)
6497 {
6498 if ( !m_table || m_currentCellCoords == wxGridNoCellCoords )
6499 return false;
6500
6501 if ( diroper.IsAtBoundary(m_currentCellCoords) )
6502 return false;
6503
6504 wxGridCellCoords coords(m_currentCellCoords);
6505 if ( m_table->IsEmpty(coords) )
6506 {
6507 // we are in an empty cell: find the next block of non-empty cells
6508 AdvanceToNextNonEmpty(coords, diroper);
6509 }
6510 else // current cell is not empty
6511 {
6512 diroper.Advance(coords);
6513 if ( m_table->IsEmpty(coords) )
6514 {
6515 // we started at the end of a block, find the next one
6516 AdvanceToNextNonEmpty(coords, diroper);
6517 }
6518 else // we're in a middle of a block
6519 {
6520 // go to the end of it, i.e. find the last cell before the next
6521 // empty one
6522 while ( !diroper.IsAtBoundary(coords) )
6523 {
6524 wxGridCellCoords coordsNext(coords);
6525 diroper.Advance(coordsNext);
6526 if ( m_table->IsEmpty(coordsNext) )
6527 break;
6528
6529 coords = coordsNext;
6530 }
6531 }
6532 }
6533
6534 if ( expandSelection )
6535 {
6536 UpdateBlockBeingSelected(m_currentCellCoords, coords);
6537 }
6538 else
6539 {
6540 ClearSelection();
6541 GoToCell(coords);
6542 }
6543
6544 return true;
6545 }
6546
6547 bool wxGrid::MoveCursorUpBlock(bool expandSelection)
6548 {
6549 return DoMoveCursorByBlock(
6550 expandSelection,
6551 wxGridBackwardOperations(this, wxGridRowOperations())
6552 );
6553 }
6554
6555 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
6556 {
6557 return DoMoveCursorByBlock(
6558 expandSelection,
6559 wxGridForwardOperations(this, wxGridRowOperations())
6560 );
6561 }
6562
6563 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
6564 {
6565 return DoMoveCursorByBlock(
6566 expandSelection,
6567 wxGridBackwardOperations(this, wxGridColumnOperations())
6568 );
6569 }
6570
6571 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
6572 {
6573 return DoMoveCursorByBlock(
6574 expandSelection,
6575 wxGridForwardOperations(this, wxGridColumnOperations())
6576 );
6577 }
6578
6579 //
6580 // ------ Label values and formatting
6581 //
6582
6583 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const
6584 {
6585 if ( horiz )
6586 *horiz = m_rowLabelHorizAlign;
6587 if ( vert )
6588 *vert = m_rowLabelVertAlign;
6589 }
6590
6591 void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const
6592 {
6593 if ( horiz )
6594 *horiz = m_colLabelHorizAlign;
6595 if ( vert )
6596 *vert = m_colLabelVertAlign;
6597 }
6598
6599 int wxGrid::GetColLabelTextOrientation() const
6600 {
6601 return m_colLabelTextOrientation;
6602 }
6603
6604 wxString wxGrid::GetRowLabelValue( int row ) const
6605 {
6606 if ( m_table )
6607 {
6608 return m_table->GetRowLabelValue( row );
6609 }
6610 else
6611 {
6612 wxString s;
6613 s << row;
6614 return s;
6615 }
6616 }
6617
6618 wxString wxGrid::GetColLabelValue( int col ) const
6619 {
6620 if ( m_table )
6621 {
6622 return m_table->GetColLabelValue( col );
6623 }
6624 else
6625 {
6626 wxString s;
6627 s << col;
6628 return s;
6629 }
6630 }
6631
6632 void wxGrid::SetRowLabelSize( int width )
6633 {
6634 wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE );
6635
6636 if ( width == wxGRID_AUTOSIZE )
6637 {
6638 width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW);
6639 }
6640
6641 if ( width != m_rowLabelWidth )
6642 {
6643 if ( width == 0 )
6644 {
6645 m_rowLabelWin->Show( false );
6646 m_cornerLabelWin->Show( false );
6647 }
6648 else if ( m_rowLabelWidth == 0 )
6649 {
6650 m_rowLabelWin->Show( true );
6651 if ( m_colLabelHeight > 0 )
6652 m_cornerLabelWin->Show( true );
6653 }
6654
6655 m_rowLabelWidth = width;
6656 CalcWindowSizes();
6657 wxScrolledWindow::Refresh( true );
6658 }
6659 }
6660
6661 void wxGrid::SetColLabelSize( int height )
6662 {
6663 wxASSERT( height >=0 || height == wxGRID_AUTOSIZE );
6664
6665 if ( height == wxGRID_AUTOSIZE )
6666 {
6667 height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN);
6668 }
6669
6670 if ( height != m_colLabelHeight )
6671 {
6672 if ( height == 0 )
6673 {
6674 m_colWindow->Show( false );
6675 m_cornerLabelWin->Show( false );
6676 }
6677 else if ( m_colLabelHeight == 0 )
6678 {
6679 m_colWindow->Show( true );
6680 if ( m_rowLabelWidth > 0 )
6681 m_cornerLabelWin->Show( true );
6682 }
6683
6684 m_colLabelHeight = height;
6685 CalcWindowSizes();
6686 wxScrolledWindow::Refresh( true );
6687 }
6688 }
6689
6690 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6691 {
6692 if ( m_labelBackgroundColour != colour )
6693 {
6694 m_labelBackgroundColour = colour;
6695 m_rowLabelWin->SetBackgroundColour( colour );
6696 m_colWindow->SetBackgroundColour( colour );
6697 m_cornerLabelWin->SetBackgroundColour( colour );
6698
6699 if ( !GetBatchCount() )
6700 {
6701 m_rowLabelWin->Refresh();
6702 m_colWindow->Refresh();
6703 m_cornerLabelWin->Refresh();
6704 }
6705 }
6706 }
6707
6708 void wxGrid::SetLabelTextColour( const wxColour& colour )
6709 {
6710 if ( m_labelTextColour != colour )
6711 {
6712 m_labelTextColour = colour;
6713 if ( !GetBatchCount() )
6714 {
6715 m_rowLabelWin->Refresh();
6716 m_colWindow->Refresh();
6717 }
6718 }
6719 }
6720
6721 void wxGrid::SetLabelFont( const wxFont& font )
6722 {
6723 m_labelFont = font;
6724 if ( !GetBatchCount() )
6725 {
6726 m_rowLabelWin->Refresh();
6727 m_colWindow->Refresh();
6728 }
6729 }
6730
6731 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6732 {
6733 // allow old (incorrect) defs to be used
6734 switch ( horiz )
6735 {
6736 case wxLEFT: horiz = wxALIGN_LEFT; break;
6737 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
6738 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
6739 }
6740
6741 switch ( vert )
6742 {
6743 case wxTOP: vert = wxALIGN_TOP; break;
6744 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
6745 case wxCENTRE: vert = wxALIGN_CENTRE; break;
6746 }
6747
6748 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
6749 {
6750 m_rowLabelHorizAlign = horiz;
6751 }
6752
6753 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
6754 {
6755 m_rowLabelVertAlign = vert;
6756 }
6757
6758 if ( !GetBatchCount() )
6759 {
6760 m_rowLabelWin->Refresh();
6761 }
6762 }
6763
6764 void wxGrid::SetColLabelAlignment( int horiz, int vert )
6765 {
6766 // allow old (incorrect) defs to be used
6767 switch ( horiz )
6768 {
6769 case wxLEFT: horiz = wxALIGN_LEFT; break;
6770 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
6771 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
6772 }
6773
6774 switch ( vert )
6775 {
6776 case wxTOP: vert = wxALIGN_TOP; break;
6777 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
6778 case wxCENTRE: vert = wxALIGN_CENTRE; break;
6779 }
6780
6781 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
6782 {
6783 m_colLabelHorizAlign = horiz;
6784 }
6785
6786 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
6787 {
6788 m_colLabelVertAlign = vert;
6789 }
6790
6791 if ( !GetBatchCount() )
6792 {
6793 m_colWindow->Refresh();
6794 }
6795 }
6796
6797 // Note: under MSW, the default column label font must be changed because it
6798 // does not support vertical printing
6799 //
6800 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6801 // pGrid->SetLabelFont(font);
6802 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6803 //
6804 void wxGrid::SetColLabelTextOrientation( int textOrientation )
6805 {
6806 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
6807 m_colLabelTextOrientation = textOrientation;
6808
6809 if ( !GetBatchCount() )
6810 m_colWindow->Refresh();
6811 }
6812
6813 void wxGrid::SetRowLabelValue( int row, const wxString& s )
6814 {
6815 if ( m_table )
6816 {
6817 m_table->SetRowLabelValue( row, s );
6818 if ( !GetBatchCount() )
6819 {
6820 wxRect rect = CellToRect( row, 0 );
6821 if ( rect.height > 0 )
6822 {
6823 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
6824 rect.x = 0;
6825 rect.width = m_rowLabelWidth;
6826 m_rowLabelWin->Refresh( true, &rect );
6827 }
6828 }
6829 }
6830 }
6831
6832 void wxGrid::SetColLabelValue( int col, const wxString& s )
6833 {
6834 if ( m_table )
6835 {
6836 m_table->SetColLabelValue( col, s );
6837 if ( !GetBatchCount() )
6838 {
6839 if ( m_useNativeHeader )
6840 {
6841 GetGridColHeader()->UpdateColumn(col);
6842 }
6843 else
6844 {
6845 wxRect rect = CellToRect( 0, col );
6846 if ( rect.width > 0 )
6847 {
6848 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
6849 rect.y = 0;
6850 rect.height = m_colLabelHeight;
6851 GetColLabelWindow()->Refresh( true, &rect );
6852 }
6853 }
6854 }
6855 }
6856 }
6857
6858 void wxGrid::SetGridLineColour( const wxColour& colour )
6859 {
6860 if ( m_gridLineColour != colour )
6861 {
6862 m_gridLineColour = colour;
6863
6864 if ( GridLinesEnabled() )
6865 RedrawGridLines();
6866 }
6867 }
6868
6869 void wxGrid::SetCellHighlightColour( const wxColour& colour )
6870 {
6871 if ( m_cellHighlightColour != colour )
6872 {
6873 m_cellHighlightColour = colour;
6874
6875 wxClientDC dc( m_gridWin );
6876 PrepareDC( dc );
6877 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
6878 DrawCellHighlight(dc, attr);
6879 attr->DecRef();
6880 }
6881 }
6882
6883 void wxGrid::SetCellHighlightPenWidth(int width)
6884 {
6885 if (m_cellHighlightPenWidth != width)
6886 {
6887 m_cellHighlightPenWidth = width;
6888
6889 // Just redrawing the cell highlight is not enough since that won't
6890 // make any visible change if the the thickness is getting smaller.
6891 int row = m_currentCellCoords.GetRow();
6892 int col = m_currentCellCoords.GetCol();
6893 if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6894 return;
6895
6896 wxRect rect = CellToRect(row, col);
6897 m_gridWin->Refresh(true, &rect);
6898 }
6899 }
6900
6901 void wxGrid::SetCellHighlightROPenWidth(int width)
6902 {
6903 if (m_cellHighlightROPenWidth != width)
6904 {
6905 m_cellHighlightROPenWidth = width;
6906
6907 // Just redrawing the cell highlight is not enough since that won't
6908 // make any visible change if the the thickness is getting smaller.
6909 int row = m_currentCellCoords.GetRow();
6910 int col = m_currentCellCoords.GetCol();
6911 if ( row == -1 || col == -1 ||
6912 GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6913 return;
6914
6915 wxRect rect = CellToRect(row, col);
6916 m_gridWin->Refresh(true, &rect);
6917 }
6918 }
6919
6920 void wxGrid::RedrawGridLines()
6921 {
6922 // the lines will be redrawn when the window is thawn
6923 if ( GetBatchCount() )
6924 return;
6925
6926 if ( GridLinesEnabled() )
6927 {
6928 wxClientDC dc( m_gridWin );
6929 PrepareDC( dc );
6930 DrawAllGridLines( dc, wxRegion() );
6931 }
6932 else // remove the grid lines
6933 {
6934 m_gridWin->Refresh();
6935 }
6936 }
6937
6938 void wxGrid::EnableGridLines( bool enable )
6939 {
6940 if ( enable != m_gridLinesEnabled )
6941 {
6942 m_gridLinesEnabled = enable;
6943
6944 RedrawGridLines();
6945 }
6946 }
6947
6948 void wxGrid::DoClipGridLines(bool& var, bool clip)
6949 {
6950 if ( clip != var )
6951 {
6952 var = clip;
6953
6954 if ( GridLinesEnabled() )
6955 RedrawGridLines();
6956 }
6957 }
6958
6959 int wxGrid::GetDefaultRowSize() const
6960 {
6961 return m_defaultRowHeight;
6962 }
6963
6964 int wxGrid::GetRowSize( int row ) const
6965 {
6966 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, wxT("invalid row index") );
6967
6968 return GetRowHeight(row);
6969 }
6970
6971 int wxGrid::GetDefaultColSize() const
6972 {
6973 return m_defaultColWidth;
6974 }
6975
6976 int wxGrid::GetColSize( int col ) const
6977 {
6978 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, wxT("invalid column index") );
6979
6980 return GetColWidth(col);
6981 }
6982
6983 // ============================================================================
6984 // access to the grid attributes: each of them has a default value in the grid
6985 // itself and may be overidden on a per-cell basis
6986 // ============================================================================
6987
6988 // ----------------------------------------------------------------------------
6989 // setting default attributes
6990 // ----------------------------------------------------------------------------
6991
6992 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6993 {
6994 m_defaultCellAttr->SetBackgroundColour(col);
6995 #ifdef __WXGTK__
6996 m_gridWin->SetBackgroundColour(col);
6997 #endif
6998 }
6999
7000 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
7001 {
7002 m_defaultCellAttr->SetTextColour(col);
7003 }
7004
7005 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
7006 {
7007 m_defaultCellAttr->SetAlignment(horiz, vert);
7008 }
7009
7010 void wxGrid::SetDefaultCellOverflow( bool allow )
7011 {
7012 m_defaultCellAttr->SetOverflow(allow);
7013 }
7014
7015 void wxGrid::SetDefaultCellFont( const wxFont& font )
7016 {
7017 m_defaultCellAttr->SetFont(font);
7018 }
7019
7020 // For editors and renderers the type registry takes precedence over the
7021 // default attr, so we need to register the new editor/renderer for the string
7022 // data type in order to make setting a default editor/renderer appear to
7023 // work correctly.
7024
7025 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
7026 {
7027 RegisterDataType(wxGRID_VALUE_STRING,
7028 renderer,
7029 GetDefaultEditorForType(wxGRID_VALUE_STRING));
7030 }
7031
7032 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
7033 {
7034 RegisterDataType(wxGRID_VALUE_STRING,
7035 GetDefaultRendererForType(wxGRID_VALUE_STRING),
7036 editor);
7037 }
7038
7039 // ----------------------------------------------------------------------------
7040 // access to the default attributes
7041 // ----------------------------------------------------------------------------
7042
7043 wxColour wxGrid::GetDefaultCellBackgroundColour() const
7044 {
7045 return m_defaultCellAttr->GetBackgroundColour();
7046 }
7047
7048 wxColour wxGrid::GetDefaultCellTextColour() const
7049 {
7050 return m_defaultCellAttr->GetTextColour();
7051 }
7052
7053 wxFont wxGrid::GetDefaultCellFont() const
7054 {
7055 return m_defaultCellAttr->GetFont();
7056 }
7057
7058 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
7059 {
7060 m_defaultCellAttr->GetAlignment(horiz, vert);
7061 }
7062
7063 bool wxGrid::GetDefaultCellOverflow() const
7064 {
7065 return m_defaultCellAttr->GetOverflow();
7066 }
7067
7068 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
7069 {
7070 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
7071 }
7072
7073 wxGridCellEditor *wxGrid::GetDefaultEditor() const
7074 {
7075 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
7076 }
7077
7078 // ----------------------------------------------------------------------------
7079 // access to cell attributes
7080 // ----------------------------------------------------------------------------
7081
7082 wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
7083 {
7084 wxGridCellAttr *attr = GetCellAttr(row, col);
7085 wxColour colour = attr->GetBackgroundColour();
7086 attr->DecRef();
7087
7088 return colour;
7089 }
7090
7091 wxColour wxGrid::GetCellTextColour( int row, int col ) const
7092 {
7093 wxGridCellAttr *attr = GetCellAttr(row, col);
7094 wxColour colour = attr->GetTextColour();
7095 attr->DecRef();
7096
7097 return colour;
7098 }
7099
7100 wxFont wxGrid::GetCellFont( int row, int col ) const
7101 {
7102 wxGridCellAttr *attr = GetCellAttr(row, col);
7103 wxFont font = attr->GetFont();
7104 attr->DecRef();
7105
7106 return font;
7107 }
7108
7109 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
7110 {
7111 wxGridCellAttr *attr = GetCellAttr(row, col);
7112 attr->GetAlignment(horiz, vert);
7113 attr->DecRef();
7114 }
7115
7116 bool wxGrid::GetCellOverflow( int row, int col ) const
7117 {
7118 wxGridCellAttr *attr = GetCellAttr(row, col);
7119 bool allow = attr->GetOverflow();
7120 attr->DecRef();
7121
7122 return allow;
7123 }
7124
7125 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
7126 {
7127 wxGridCellAttr *attr = GetCellAttr(row, col);
7128 attr->GetSize( num_rows, num_cols );
7129 attr->DecRef();
7130 }
7131
7132 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
7133 {
7134 wxGridCellAttr* attr = GetCellAttr(row, col);
7135 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
7136 attr->DecRef();
7137
7138 return renderer;
7139 }
7140
7141 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
7142 {
7143 wxGridCellAttr* attr = GetCellAttr(row, col);
7144 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7145 attr->DecRef();
7146
7147 return editor;
7148 }
7149
7150 bool wxGrid::IsReadOnly(int row, int col) const
7151 {
7152 wxGridCellAttr* attr = GetCellAttr(row, col);
7153 bool isReadOnly = attr->IsReadOnly();
7154 attr->DecRef();
7155
7156 return isReadOnly;
7157 }
7158
7159 // ----------------------------------------------------------------------------
7160 // attribute support: cache, automatic provider creation, ...
7161 // ----------------------------------------------------------------------------
7162
7163 bool wxGrid::CanHaveAttributes() const
7164 {
7165 if ( !m_table )
7166 {
7167 return false;
7168 }
7169
7170 return m_table->CanHaveAttributes();
7171 }
7172
7173 void wxGrid::ClearAttrCache()
7174 {
7175 if ( m_attrCache.row != -1 )
7176 {
7177 wxGridCellAttr *oldAttr = m_attrCache.attr;
7178 m_attrCache.attr = NULL;
7179 m_attrCache.row = -1;
7180 // wxSafeDecRec(...) might cause event processing that accesses
7181 // the cached attribute, if one exists (e.g. by deleting the
7182 // editor stored within the attribute). Therefore it is important
7183 // to invalidate the cache before calling wxSafeDecRef!
7184 wxSafeDecRef(oldAttr);
7185 }
7186 }
7187
7188 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
7189 {
7190 if ( attr != NULL )
7191 {
7192 wxGrid *self = (wxGrid *)this; // const_cast
7193
7194 self->ClearAttrCache();
7195 self->m_attrCache.row = row;
7196 self->m_attrCache.col = col;
7197 self->m_attrCache.attr = attr;
7198 wxSafeIncRef(attr);
7199 }
7200 }
7201
7202 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
7203 {
7204 if ( row == m_attrCache.row && col == m_attrCache.col )
7205 {
7206 *attr = m_attrCache.attr;
7207 wxSafeIncRef(m_attrCache.attr);
7208
7209 #ifdef DEBUG_ATTR_CACHE
7210 gs_nAttrCacheHits++;
7211 #endif
7212
7213 return true;
7214 }
7215 else
7216 {
7217 #ifdef DEBUG_ATTR_CACHE
7218 gs_nAttrCacheMisses++;
7219 #endif
7220
7221 return false;
7222 }
7223 }
7224
7225 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
7226 {
7227 wxGridCellAttr *attr = NULL;
7228 // Additional test to avoid looking at the cache e.g. for
7229 // wxNoCellCoords, as this will confuse memory management.
7230 if ( row >= 0 )
7231 {
7232 if ( !LookupAttr(row, col, &attr) )
7233 {
7234 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
7235 : NULL;
7236 CacheAttr(row, col, attr);
7237 }
7238 }
7239
7240 if (attr)
7241 {
7242 attr->SetDefAttr(m_defaultCellAttr);
7243 }
7244 else
7245 {
7246 attr = m_defaultCellAttr;
7247 attr->IncRef();
7248 }
7249
7250 return attr;
7251 }
7252
7253 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
7254 {
7255 wxGridCellAttr *attr = NULL;
7256 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
7257
7258 wxCHECK_MSG( canHave, attr, wxT("Cell attributes not allowed"));
7259 wxCHECK_MSG( m_table, attr, wxT("must have a table") );
7260
7261 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
7262 if ( !attr )
7263 {
7264 attr = new wxGridCellAttr(m_defaultCellAttr);
7265
7266 // artificially inc the ref count to match DecRef() in caller
7267 attr->IncRef();
7268 m_table->SetAttr(attr, row, col);
7269 }
7270
7271 return attr;
7272 }
7273
7274 // ----------------------------------------------------------------------------
7275 // setting column attributes (wrappers around SetColAttr)
7276 // ----------------------------------------------------------------------------
7277
7278 void wxGrid::SetColFormatBool(int col)
7279 {
7280 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
7281 }
7282
7283 void wxGrid::SetColFormatNumber(int col)
7284 {
7285 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
7286 }
7287
7288 void wxGrid::SetColFormatFloat(int col, int width, int precision)
7289 {
7290 wxString typeName = wxGRID_VALUE_FLOAT;
7291 if ( (width != -1) || (precision != -1) )
7292 {
7293 typeName << wxT(':') << width << wxT(',') << precision;
7294 }
7295
7296 SetColFormatCustom(col, typeName);
7297 }
7298
7299 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
7300 {
7301 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
7302 if (!attr)
7303 attr = new wxGridCellAttr;
7304 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
7305 attr->SetRenderer(renderer);
7306 wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
7307 attr->SetEditor(editor);
7308
7309 SetColAttr(col, attr);
7310
7311 }
7312
7313 // ----------------------------------------------------------------------------
7314 // setting cell attributes: this is forwarded to the table
7315 // ----------------------------------------------------------------------------
7316
7317 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
7318 {
7319 if ( CanHaveAttributes() )
7320 {
7321 m_table->SetAttr(attr, row, col);
7322 ClearAttrCache();
7323 }
7324 else
7325 {
7326 wxSafeDecRef(attr);
7327 }
7328 }
7329
7330 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
7331 {
7332 if ( CanHaveAttributes() )
7333 {
7334 m_table->SetRowAttr(attr, row);
7335 ClearAttrCache();
7336 }
7337 else
7338 {
7339 wxSafeDecRef(attr);
7340 }
7341 }
7342
7343 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
7344 {
7345 if ( CanHaveAttributes() )
7346 {
7347 m_table->SetColAttr(attr, col);
7348 ClearAttrCache();
7349 }
7350 else
7351 {
7352 wxSafeDecRef(attr);
7353 }
7354 }
7355
7356 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
7357 {
7358 if ( CanHaveAttributes() )
7359 {
7360 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7361 attr->SetBackgroundColour(colour);
7362 attr->DecRef();
7363 }
7364 }
7365
7366 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
7367 {
7368 if ( CanHaveAttributes() )
7369 {
7370 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7371 attr->SetTextColour(colour);
7372 attr->DecRef();
7373 }
7374 }
7375
7376 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
7377 {
7378 if ( CanHaveAttributes() )
7379 {
7380 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7381 attr->SetFont(font);
7382 attr->DecRef();
7383 }
7384 }
7385
7386 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
7387 {
7388 if ( CanHaveAttributes() )
7389 {
7390 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7391 attr->SetAlignment(horiz, vert);
7392 attr->DecRef();
7393 }
7394 }
7395
7396 void wxGrid::SetCellOverflow( int row, int col, bool allow )
7397 {
7398 if ( CanHaveAttributes() )
7399 {
7400 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7401 attr->SetOverflow(allow);
7402 attr->DecRef();
7403 }
7404 }
7405
7406 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
7407 {
7408 if ( CanHaveAttributes() )
7409 {
7410 int cell_rows, cell_cols;
7411
7412 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7413 attr->GetSize(&cell_rows, &cell_cols);
7414 attr->SetSize(num_rows, num_cols);
7415 attr->DecRef();
7416
7417 // Cannot set the size of a cell to 0 or negative values
7418 // While it is perfectly legal to do that, this function cannot
7419 // handle all the possibilies, do it by hand by getting the CellAttr.
7420 // You can only set the size of a cell to 1,1 or greater with this fn
7421 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
7422 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7423 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
7424 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7425
7426 // if this was already a multicell then "turn off" the other cells first
7427 if ((cell_rows > 1) || (cell_cols > 1))
7428 {
7429 int i, j;
7430 for (j=row; j < row + cell_rows; j++)
7431 {
7432 for (i=col; i < col + cell_cols; i++)
7433 {
7434 if ((i != col) || (j != row))
7435 {
7436 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
7437 attr_stub->SetSize( 1, 1 );
7438 attr_stub->DecRef();
7439 }
7440 }
7441 }
7442 }
7443
7444 // mark the cells that will be covered by this cell to
7445 // negative or zero values to point back at this cell
7446 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
7447 {
7448 int i, j;
7449 for (j=row; j < row + num_rows; j++)
7450 {
7451 for (i=col; i < col + num_cols; i++)
7452 {
7453 if ((i != col) || (j != row))
7454 {
7455 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
7456 attr_stub->SetSize( row - j, col - i );
7457 attr_stub->DecRef();
7458 }
7459 }
7460 }
7461 }
7462 }
7463 }
7464
7465 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
7466 {
7467 if ( CanHaveAttributes() )
7468 {
7469 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7470 attr->SetRenderer(renderer);
7471 attr->DecRef();
7472 }
7473 }
7474
7475 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
7476 {
7477 if ( CanHaveAttributes() )
7478 {
7479 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7480 attr->SetEditor(editor);
7481 attr->DecRef();
7482 }
7483 }
7484
7485 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
7486 {
7487 if ( CanHaveAttributes() )
7488 {
7489 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7490 attr->SetReadOnly(isReadOnly);
7491 attr->DecRef();
7492 }
7493 }
7494
7495 // ----------------------------------------------------------------------------
7496 // Data type registration
7497 // ----------------------------------------------------------------------------
7498
7499 void wxGrid::RegisterDataType(const wxString& typeName,
7500 wxGridCellRenderer* renderer,
7501 wxGridCellEditor* editor)
7502 {
7503 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
7504 }
7505
7506
7507 wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
7508 {
7509 wxString typeName = m_table->GetTypeName(row, col);
7510 return GetDefaultEditorForType(typeName);
7511 }
7512
7513 wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
7514 {
7515 wxString typeName = m_table->GetTypeName(row, col);
7516 return GetDefaultRendererForType(typeName);
7517 }
7518
7519 wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
7520 {
7521 int index = m_typeRegistry->FindOrCloneDataType(typeName);
7522 if ( index == wxNOT_FOUND )
7523 {
7524 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
7525
7526 return NULL;
7527 }
7528
7529 return m_typeRegistry->GetEditor(index);
7530 }
7531
7532 wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
7533 {
7534 int index = m_typeRegistry->FindOrCloneDataType(typeName);
7535 if ( index == wxNOT_FOUND )
7536 {
7537 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
7538
7539 return NULL;
7540 }
7541
7542 return m_typeRegistry->GetRenderer(index);
7543 }
7544
7545 // ----------------------------------------------------------------------------
7546 // row/col size
7547 // ----------------------------------------------------------------------------
7548
7549 void wxGrid::DoDisableLineResize(int line, wxGridFixedIndicesSet *& setFixed)
7550 {
7551 if ( !setFixed )
7552 {
7553 setFixed = new wxGridFixedIndicesSet;
7554 }
7555
7556 setFixed->insert(line);
7557 }
7558
7559 bool
7560 wxGrid::DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const
7561 {
7562 return !setFixed || !setFixed->count(line);
7563 }
7564
7565 void wxGrid::EnableDragRowSize( bool enable )
7566 {
7567 m_canDragRowSize = enable;
7568 }
7569
7570 void wxGrid::EnableDragColSize( bool enable )
7571 {
7572 m_canDragColSize = enable;
7573 }
7574
7575 void wxGrid::EnableDragGridSize( bool enable )
7576 {
7577 m_canDragGridSize = enable;
7578 }
7579
7580 void wxGrid::EnableDragCell( bool enable )
7581 {
7582 m_canDragCell = enable;
7583 }
7584
7585 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
7586 {
7587 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
7588
7589 if ( resizeExistingRows )
7590 {
7591 // since we are resizing all rows to the default row size,
7592 // we can simply clear the row heights and row bottoms
7593 // arrays (which also allows us to take advantage of
7594 // some speed optimisations)
7595 m_rowHeights.Empty();
7596 m_rowBottoms.Empty();
7597 if ( !GetBatchCount() )
7598 CalcDimensions();
7599 }
7600 }
7601
7602 void wxGrid::SetRowSize( int row, int height )
7603 {
7604 wxCHECK_RET( row >= 0 && row < m_numRows, wxT("invalid row index") );
7605
7606 // if < 0 then calculate new height from label
7607 if ( height < 0 )
7608 {
7609 long w, h;
7610 wxArrayString lines;
7611 wxClientDC dc(m_rowLabelWin);
7612 dc.SetFont(GetLabelFont());
7613 StringToLines(GetRowLabelValue( row ), lines);
7614 GetTextBoxSize( dc, lines, &w, &h );
7615 //check that it is not less than the minimal height
7616 height = wxMax(h, GetRowMinimalAcceptableHeight());
7617 }
7618
7619 // See comment in SetColSize
7620 if ( height < GetRowMinimalAcceptableHeight())
7621 return;
7622
7623 if ( m_rowHeights.IsEmpty() )
7624 {
7625 // need to really create the array
7626 InitRowHeights();
7627 }
7628
7629 int h = wxMax( 0, height );
7630 int diff = h - m_rowHeights[row];
7631
7632 m_rowHeights[row] = h;
7633 for ( int i = row; i < m_numRows; i++ )
7634 {
7635 m_rowBottoms[i] += diff;
7636 }
7637
7638 if ( !GetBatchCount() )
7639 CalcDimensions();
7640 }
7641
7642 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
7643 {
7644 // we dont allow zero default column width
7645 m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
7646
7647 if ( resizeExistingCols )
7648 {
7649 // since we are resizing all columns to the default column size,
7650 // we can simply clear the col widths and col rights
7651 // arrays (which also allows us to take advantage of
7652 // some speed optimisations)
7653 m_colWidths.Empty();
7654 m_colRights.Empty();
7655 if ( !GetBatchCount() )
7656 CalcDimensions();
7657 }
7658 }
7659
7660 void wxGrid::SetColSize( int col, int width )
7661 {
7662 wxCHECK_RET( col >= 0 && col < m_numCols, wxT("invalid column index") );
7663
7664 // if < 0 then calculate new width from label
7665 if ( width < 0 )
7666 {
7667 long w, h;
7668 wxArrayString lines;
7669 wxClientDC dc(m_colWindow);
7670 dc.SetFont(GetLabelFont());
7671 StringToLines(GetColLabelValue(col), lines);
7672 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
7673 GetTextBoxSize( dc, lines, &w, &h );
7674 else
7675 GetTextBoxSize( dc, lines, &h, &w );
7676 width = w + 6;
7677 //check that it is not less than the minimal width
7678 width = wxMax(width, GetColMinimalAcceptableWidth());
7679 }
7680
7681 // we intentionally don't test whether the width is less than
7682 // GetColMinimalWidth() here but we do compare it with
7683 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7684 // #651) -- and we also always allow the width of 0 as it has the special
7685 // sense of hiding the column
7686 if ( width > 0 && width < GetColMinimalAcceptableWidth() )
7687 return;
7688
7689 if ( m_colWidths.IsEmpty() )
7690 {
7691 // need to really create the array
7692 InitColWidths();
7693 }
7694
7695 const int diff = width - m_colWidths[col];
7696 m_colWidths[col] = width;
7697 if ( m_useNativeHeader )
7698 GetGridColHeader()->UpdateColumn(col);
7699 //else: will be refreshed when the header is redrawn
7700
7701 for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
7702 {
7703 m_colRights[GetColAt(colPos)] += diff;
7704 }
7705
7706 if ( !GetBatchCount() )
7707 {
7708 CalcDimensions();
7709 Refresh();
7710 }
7711 }
7712
7713 void wxGrid::SetColMinimalWidth( int col, int width )
7714 {
7715 if (width > GetColMinimalAcceptableWidth())
7716 {
7717 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
7718 m_colMinWidths[key] = width;
7719 }
7720 }
7721
7722 void wxGrid::SetRowMinimalHeight( int row, int width )
7723 {
7724 if (width > GetRowMinimalAcceptableHeight())
7725 {
7726 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
7727 m_rowMinHeights[key] = width;
7728 }
7729 }
7730
7731 int wxGrid::GetColMinimalWidth(int col) const
7732 {
7733 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
7734 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
7735
7736 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
7737 }
7738
7739 int wxGrid::GetRowMinimalHeight(int row) const
7740 {
7741 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
7742 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
7743
7744 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
7745 }
7746
7747 void wxGrid::SetColMinimalAcceptableWidth( int width )
7748 {
7749 // We do allow a width of 0 since this gives us
7750 // an easy way to temporarily hiding columns.
7751 if ( width >= 0 )
7752 m_minAcceptableColWidth = width;
7753 }
7754
7755 void wxGrid::SetRowMinimalAcceptableHeight( int height )
7756 {
7757 // We do allow a height of 0 since this gives us
7758 // an easy way to temporarily hiding rows.
7759 if ( height >= 0 )
7760 m_minAcceptableRowHeight = height;
7761 }
7762
7763 int wxGrid::GetColMinimalAcceptableWidth() const
7764 {
7765 return m_minAcceptableColWidth;
7766 }
7767
7768 int wxGrid::GetRowMinimalAcceptableHeight() const
7769 {
7770 return m_minAcceptableRowHeight;
7771 }
7772
7773 // ----------------------------------------------------------------------------
7774 // auto sizing
7775 // ----------------------------------------------------------------------------
7776
7777 void
7778 wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
7779 {
7780 const bool column = direction == wxGRID_COLUMN;
7781
7782 wxClientDC dc(m_gridWin);
7783
7784 // cancel editing of cell
7785 HideCellEditControl();
7786 SaveEditControlValue();
7787
7788 // init both of them to avoid compiler warnings, even if we only need one
7789 int row = -1,
7790 col = -1;
7791 if ( column )
7792 col = colOrRow;
7793 else
7794 row = colOrRow;
7795
7796 wxCoord extent, extentMax = 0;
7797 int max = column ? m_numRows : m_numCols;
7798 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
7799 {
7800 if ( column )
7801 row = rowOrCol;
7802 else
7803 col = rowOrCol;
7804
7805 wxGridCellAttr *attr = GetCellAttr(row, col);
7806 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7807 if ( renderer )
7808 {
7809 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
7810 extent = column ? size.x : size.y;
7811 if ( extent > extentMax )
7812 extentMax = extent;
7813
7814 renderer->DecRef();
7815 }
7816
7817 attr->DecRef();
7818 }
7819
7820 // now also compare with the column label extent
7821 wxCoord w, h;
7822 dc.SetFont( GetLabelFont() );
7823
7824 if ( column )
7825 {
7826 dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h );
7827 if ( GetColLabelTextOrientation() == wxVERTICAL )
7828 w = h;
7829 }
7830 else
7831 dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h );
7832
7833 extent = column ? w : h;
7834 if ( extent > extentMax )
7835 extentMax = extent;
7836
7837 if ( !extentMax )
7838 {
7839 // empty column - give default extent (notice that if extentMax is less
7840 // than default extent but != 0, it's OK)
7841 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
7842 }
7843 else
7844 {
7845 if ( column )
7846 // leave some space around text
7847 extentMax += 10;
7848 else
7849 extentMax += 6;
7850 }
7851
7852 if ( column )
7853 {
7854 // Ensure automatic width is not less than minimal width. See the
7855 // comment in SetColSize() for explanation of why this isn't done
7856 // in SetColSize().
7857 if ( !setAsMin )
7858 extentMax = wxMax(extentMax, GetColMinimalWidth(col));
7859
7860 SetColSize( col, extentMax );
7861 if ( !GetBatchCount() )
7862 {
7863 if ( m_useNativeHeader )
7864 {
7865 GetGridColHeader()->UpdateColumn(col);
7866 }
7867 else
7868 {
7869 int cw, ch, dummy;
7870 m_gridWin->GetClientSize( &cw, &ch );
7871 wxRect rect ( CellToRect( 0, col ) );
7872 rect.y = 0;
7873 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
7874 rect.width = cw - rect.x;
7875 rect.height = m_colLabelHeight;
7876 GetColLabelWindow()->Refresh( true, &rect );
7877 }
7878 }
7879 }
7880 else
7881 {
7882 // Ensure automatic width is not less than minimal height. See the
7883 // comment in SetColSize() for explanation of why this isn't done
7884 // in SetRowSize().
7885 if ( !setAsMin )
7886 extentMax = wxMax(extentMax, GetRowMinimalHeight(row));
7887
7888 SetRowSize(row, extentMax);
7889 if ( !GetBatchCount() )
7890 {
7891 int cw, ch, dummy;
7892 m_gridWin->GetClientSize( &cw, &ch );
7893 wxRect rect( CellToRect( row, 0 ) );
7894 rect.x = 0;
7895 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
7896 rect.width = m_rowLabelWidth;
7897 rect.height = ch - rect.y;
7898 m_rowLabelWin->Refresh( true, &rect );
7899 }
7900 }
7901
7902 if ( setAsMin )
7903 {
7904 if ( column )
7905 SetColMinimalWidth(col, extentMax);
7906 else
7907 SetRowMinimalHeight(row, extentMax);
7908 }
7909 }
7910
7911 wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction)
7912 {
7913 // calculate size for the rows or columns?
7914 const bool calcRows = direction == wxGRID_ROW;
7915
7916 wxClientDC dc(calcRows ? GetGridRowLabelWindow()
7917 : GetGridColLabelWindow());
7918 dc.SetFont(GetLabelFont());
7919
7920 // which dimension should we take into account for calculations?
7921 //
7922 // for columns, the text can be only horizontal so it's easy but for rows
7923 // we also have to take into account the text orientation
7924 const bool
7925 useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL);
7926
7927 wxArrayString lines;
7928 wxCoord extentMax = 0;
7929
7930 const int numRowsOrCols = calcRows ? m_numRows : m_numCols;
7931 for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ )
7932 {
7933 lines.Clear();
7934
7935 wxString label = calcRows ? GetRowLabelValue(rowOrCol)
7936 : GetColLabelValue(rowOrCol);
7937 StringToLines(label, lines);
7938
7939 long w, h;
7940 GetTextBoxSize(dc, lines, &w, &h);
7941
7942 const wxCoord extent = useWidth ? w : h;
7943 if ( extent > extentMax )
7944 extentMax = extent;
7945 }
7946
7947 if ( !extentMax )
7948 {
7949 // empty column - give default extent (notice that if extentMax is less
7950 // than default extent but != 0, it's OK)
7951 extentMax = calcRows ? GetDefaultRowLabelSize()
7952 : GetDefaultColLabelSize();
7953 }
7954
7955 // leave some space around text (taken from AutoSizeColOrRow)
7956 if ( calcRows )
7957 extentMax += 10;
7958 else
7959 extentMax += 6;
7960
7961 return extentMax;
7962 }
7963
7964 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
7965 {
7966 int width = m_rowLabelWidth;
7967
7968 wxGridUpdateLocker locker;
7969 if(!calcOnly)
7970 locker.Create(this);
7971
7972 for ( int col = 0; col < m_numCols; col++ )
7973 {
7974 if ( !calcOnly )
7975 AutoSizeColumn(col, setAsMin);
7976
7977 width += GetColWidth(col);
7978 }
7979
7980 return width;
7981 }
7982
7983 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
7984 {
7985 int height = m_colLabelHeight;
7986
7987 wxGridUpdateLocker locker;
7988 if(!calcOnly)
7989 locker.Create(this);
7990
7991 for ( int row = 0; row < m_numRows; row++ )
7992 {
7993 if ( !calcOnly )
7994 AutoSizeRow(row, setAsMin);
7995
7996 height += GetRowHeight(row);
7997 }
7998
7999 return height;
8000 }
8001
8002 void wxGrid::AutoSize()
8003 {
8004 wxGridUpdateLocker locker(this);
8005
8006 wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth,
8007 SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight);
8008
8009 // we know that we're not going to have scrollbars so disable them now to
8010 // avoid trouble in SetClientSize() which can otherwise set the correct
8011 // client size but also leave space for (not needed any more) scrollbars
8012 SetScrollbars(0, 0, 0, 0, 0, 0, true);
8013
8014 // restore the scroll rate parameters overwritten by SetScrollbars()
8015 SetScrollRate(m_scrollLineX, m_scrollLineY);
8016
8017 SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight);
8018 }
8019
8020 void wxGrid::AutoSizeRowLabelSize( int row )
8021 {
8022 // Hide the edit control, so it
8023 // won't interfere with drag-shrinking.
8024 if ( IsCellEditControlShown() )
8025 {
8026 HideCellEditControl();
8027 SaveEditControlValue();
8028 }
8029
8030 // autosize row height depending on label text
8031 SetRowSize(row, -1);
8032 ForceRefresh();
8033 }
8034
8035 void wxGrid::AutoSizeColLabelSize( int col )
8036 {
8037 // Hide the edit control, so it
8038 // won't interfere with drag-shrinking.
8039 if ( IsCellEditControlShown() )
8040 {
8041 HideCellEditControl();
8042 SaveEditControlValue();
8043 }
8044
8045 // autosize column width depending on label text
8046 SetColSize(col, -1);
8047 ForceRefresh();
8048 }
8049
8050 wxSize wxGrid::DoGetBestSize() const
8051 {
8052 wxGrid *self = (wxGrid *)this; // const_cast
8053
8054 // we do the same as in AutoSize() here with the exception that we don't
8055 // change the column/row sizes, only calculate them
8056 wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth,
8057 self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight);
8058
8059 // NOTE: This size should be cached, but first we need to add calls to
8060 // InvalidateBestSize everywhere that could change the results of this
8061 // calculation.
8062 // CacheBestSize(size);
8063
8064 return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight)
8065 + GetWindowBorderSize();
8066 }
8067
8068 void wxGrid::Fit()
8069 {
8070 AutoSize();
8071 }
8072
8073 wxPen& wxGrid::GetDividerPen() const
8074 {
8075 return wxNullPen;
8076 }
8077
8078 // ----------------------------------------------------------------------------
8079 // cell value accessor functions
8080 // ----------------------------------------------------------------------------
8081
8082 void wxGrid::SetCellValue( int row, int col, const wxString& s )
8083 {
8084 if ( m_table )
8085 {
8086 m_table->SetValue( row, col, s );
8087 if ( !GetBatchCount() )
8088 {
8089 int dummy;
8090 wxRect rect( CellToRect( row, col ) );
8091 rect.x = 0;
8092 rect.width = m_gridWin->GetClientSize().GetWidth();
8093 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
8094 m_gridWin->Refresh( false, &rect );
8095 }
8096
8097 if ( m_currentCellCoords.GetRow() == row &&
8098 m_currentCellCoords.GetCol() == col &&
8099 IsCellEditControlShown())
8100 // Note: If we are using IsCellEditControlEnabled,
8101 // this interacts badly with calling SetCellValue from
8102 // an EVT_GRID_CELL_CHANGE handler.
8103 {
8104 HideCellEditControl();
8105 ShowCellEditControl(); // will reread data from table
8106 }
8107 }
8108 }
8109
8110 // ----------------------------------------------------------------------------
8111 // block, row and column selection
8112 // ----------------------------------------------------------------------------
8113
8114 void wxGrid::SelectRow( int row, bool addToSelected )
8115 {
8116 if ( !m_selection )
8117 return;
8118
8119 if ( !addToSelected )
8120 ClearSelection();
8121
8122 m_selection->SelectRow(row);
8123 }
8124
8125 void wxGrid::SelectCol( int col, bool addToSelected )
8126 {
8127 if ( !m_selection )
8128 return;
8129
8130 if ( !addToSelected )
8131 ClearSelection();
8132
8133 m_selection->SelectCol(col);
8134 }
8135
8136 void wxGrid::SelectBlock(int topRow, int leftCol, int bottomRow, int rightCol,
8137 bool addToSelected)
8138 {
8139 if ( !m_selection )
8140 return;
8141
8142 if ( !addToSelected )
8143 ClearSelection();
8144
8145 m_selection->SelectBlock(topRow, leftCol, bottomRow, rightCol);
8146 }
8147
8148 void wxGrid::SelectAll()
8149 {
8150 if ( m_numRows > 0 && m_numCols > 0 )
8151 {
8152 if ( m_selection )
8153 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
8154 }
8155 }
8156
8157 // ----------------------------------------------------------------------------
8158 // cell, row and col deselection
8159 // ----------------------------------------------------------------------------
8160
8161 void wxGrid::DeselectLine(int line, const wxGridOperations& oper)
8162 {
8163 if ( !m_selection )
8164 return;
8165
8166 const wxGridSelectionModes mode = m_selection->GetSelectionMode();
8167 if ( mode == oper.GetSelectionMode() ||
8168 mode == wxGrid::wxGridSelectRowsOrColumns )
8169 {
8170 const wxGridCellCoords c(oper.MakeCoords(line, 0));
8171 if ( m_selection->IsInSelection(c) )
8172 m_selection->ToggleCellSelection(c);
8173 }
8174 else if ( mode != oper.Dual().GetSelectionMode() )
8175 {
8176 const int nOther = oper.Dual().GetNumberOfLines(this);
8177 for ( int i = 0; i < nOther; i++ )
8178 {
8179 const wxGridCellCoords c(oper.MakeCoords(line, i));
8180 if ( m_selection->IsInSelection(c) )
8181 m_selection->ToggleCellSelection(c);
8182 }
8183 }
8184 //else: can only select orthogonal lines so no lines in this direction
8185 // could have been selected anyhow
8186 }
8187
8188 void wxGrid::DeselectRow(int row)
8189 {
8190 DeselectLine(row, wxGridRowOperations());
8191 }
8192
8193 void wxGrid::DeselectCol(int col)
8194 {
8195 DeselectLine(col, wxGridColumnOperations());
8196 }
8197
8198 void wxGrid::DeselectCell( int row, int col )
8199 {
8200 if ( m_selection && m_selection->IsInSelection(row, col) )
8201 m_selection->ToggleCellSelection(row, col);
8202 }
8203
8204 bool wxGrid::IsSelection() const
8205 {
8206 return ( m_selection && (m_selection->IsSelection() ||
8207 ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
8208 m_selectedBlockBottomRight != wxGridNoCellCoords) ) );
8209 }
8210
8211 bool wxGrid::IsInSelection( int row, int col ) const
8212 {
8213 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
8214 ( row >= m_selectedBlockTopLeft.GetRow() &&
8215 col >= m_selectedBlockTopLeft.GetCol() &&
8216 row <= m_selectedBlockBottomRight.GetRow() &&
8217 col <= m_selectedBlockBottomRight.GetCol() )) );
8218 }
8219
8220 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
8221 {
8222 if (!m_selection)
8223 {
8224 wxGridCellCoordsArray a;
8225 return a;
8226 }
8227
8228 return m_selection->m_cellSelection;
8229 }
8230
8231 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
8232 {
8233 if (!m_selection)
8234 {
8235 wxGridCellCoordsArray a;
8236 return a;
8237 }
8238
8239 return m_selection->m_blockSelectionTopLeft;
8240 }
8241
8242 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
8243 {
8244 if (!m_selection)
8245 {
8246 wxGridCellCoordsArray a;
8247 return a;
8248 }
8249
8250 return m_selection->m_blockSelectionBottomRight;
8251 }
8252
8253 wxArrayInt wxGrid::GetSelectedRows() const
8254 {
8255 if (!m_selection)
8256 {
8257 wxArrayInt a;
8258 return a;
8259 }
8260
8261 return m_selection->m_rowSelection;
8262 }
8263
8264 wxArrayInt wxGrid::GetSelectedCols() const
8265 {
8266 if (!m_selection)
8267 {
8268 wxArrayInt a;
8269 return a;
8270 }
8271
8272 return m_selection->m_colSelection;
8273 }
8274
8275 void wxGrid::ClearSelection()
8276 {
8277 wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft,
8278 m_selectedBlockBottomRight);
8279 wxRect r2 = BlockToDeviceRect(m_currentCellCoords,
8280 m_selectedBlockCorner);
8281
8282 m_selectedBlockTopLeft =
8283 m_selectedBlockBottomRight =
8284 m_selectedBlockCorner = wxGridNoCellCoords;
8285
8286 if ( !r1.IsEmpty() )
8287 RefreshRect(r1, false);
8288 if ( !r2.IsEmpty() )
8289 RefreshRect(r2, false);
8290
8291 if ( m_selection )
8292 m_selection->ClearSelection();
8293 }
8294
8295 // This function returns the rectangle that encloses the given block
8296 // in device coords clipped to the client size of the grid window.
8297 //
8298 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft,
8299 const wxGridCellCoords& bottomRight ) const
8300 {
8301 wxRect resultRect;
8302 wxRect tempCellRect = CellToRect(topLeft);
8303 if ( tempCellRect != wxGridNoCellRect )
8304 {
8305 resultRect = tempCellRect;
8306 }
8307 else
8308 {
8309 resultRect = wxRect(0, 0, 0, 0);
8310 }
8311
8312 tempCellRect = CellToRect(bottomRight);
8313 if ( tempCellRect != wxGridNoCellRect )
8314 {
8315 resultRect += tempCellRect;
8316 }
8317 else
8318 {
8319 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8320 return wxGridNoCellRect;
8321 }
8322
8323 // Ensure that left/right and top/bottom pairs are in order.
8324 int left = resultRect.GetLeft();
8325 int top = resultRect.GetTop();
8326 int right = resultRect.GetRight();
8327 int bottom = resultRect.GetBottom();
8328
8329 int leftCol = topLeft.GetCol();
8330 int topRow = topLeft.GetRow();
8331 int rightCol = bottomRight.GetCol();
8332 int bottomRow = bottomRight.GetRow();
8333
8334 if (left > right)
8335 {
8336 int tmp = left;
8337 left = right;
8338 right = tmp;
8339
8340 tmp = leftCol;
8341 leftCol = rightCol;
8342 rightCol = tmp;
8343 }
8344
8345 if (top > bottom)
8346 {
8347 int tmp = top;
8348 top = bottom;
8349 bottom = tmp;
8350
8351 tmp = topRow;
8352 topRow = bottomRow;
8353 bottomRow = tmp;
8354 }
8355
8356 // The following loop is ONLY necessary to detect and handle merged cells.
8357 int cw, ch;
8358 m_gridWin->GetClientSize( &cw, &ch );
8359
8360 // Get the origin coordinates: notice that they will be negative if the
8361 // grid is scrolled downwards/to the right.
8362 int gridOriginX = 0;
8363 int gridOriginY = 0;
8364 CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY);
8365
8366 int onScreenLeftmostCol = internalXToCol(-gridOriginX);
8367 int onScreenUppermostRow = internalYToRow(-gridOriginY);
8368
8369 int onScreenRightmostCol = internalXToCol(-gridOriginX + cw);
8370 int onScreenBottommostRow = internalYToRow(-gridOriginY + ch);
8371
8372 // Bound our loop so that we only examine the portion of the selected block
8373 // that is shown on screen. Therefore, we compare the Top-Left block values
8374 // to the Top-Left screen values, and the Bottom-Right block values to the
8375 // Bottom-Right screen values, choosing appropriately.
8376 const int visibleTopRow = wxMax(topRow, onScreenUppermostRow);
8377 const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow);
8378 const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol);
8379 const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol);
8380
8381 for ( int j = visibleTopRow; j <= visibleBottomRow; j++ )
8382 {
8383 for ( int i = visibleLeftCol; i <= visibleRightCol; i++ )
8384 {
8385 if ( (j == visibleTopRow) || (j == visibleBottomRow) ||
8386 (i == visibleLeftCol) || (i == visibleRightCol) )
8387 {
8388 tempCellRect = CellToRect( j, i );
8389
8390 if (tempCellRect.x < left)
8391 left = tempCellRect.x;
8392 if (tempCellRect.y < top)
8393 top = tempCellRect.y;
8394 if (tempCellRect.x + tempCellRect.width > right)
8395 right = tempCellRect.x + tempCellRect.width;
8396 if (tempCellRect.y + tempCellRect.height > bottom)
8397 bottom = tempCellRect.y + tempCellRect.height;
8398 }
8399 else
8400 {
8401 i = visibleRightCol; // jump over inner cells.
8402 }
8403 }
8404 }
8405
8406 // Convert to scrolled coords
8407 CalcScrolledPosition( left, top, &left, &top );
8408 CalcScrolledPosition( right, bottom, &right, &bottom );
8409
8410 if (right < 0 || bottom < 0 || left > cw || top > ch)
8411 return wxRect(0,0,0,0);
8412
8413 resultRect.SetLeft( wxMax(0, left) );
8414 resultRect.SetTop( wxMax(0, top) );
8415 resultRect.SetRight( wxMin(cw, right) );
8416 resultRect.SetBottom( wxMin(ch, bottom) );
8417
8418 return resultRect;
8419 }
8420
8421 void wxGrid::DoSetSizes(const wxGridSizesInfo& sizeInfo,
8422 const wxGridOperations& oper)
8423 {
8424 BeginBatch();
8425 oper.SetDefaultLineSize(this, sizeInfo.m_sizeDefault, true);
8426 const int numLines = oper.GetNumberOfLines(this);
8427 for ( int i = 0; i < numLines; i++ )
8428 {
8429 int size = sizeInfo.GetSize(i);
8430 if ( size != sizeInfo.m_sizeDefault)
8431 oper.SetLineSize(this, i, size);
8432 }
8433 EndBatch();
8434 }
8435
8436 void wxGrid::SetColSizes(const wxGridSizesInfo& sizeInfo)
8437 {
8438 DoSetSizes(sizeInfo, wxGridColumnOperations());
8439 }
8440
8441 void wxGrid::SetRowSizes(const wxGridSizesInfo& sizeInfo)
8442 {
8443 DoSetSizes(sizeInfo, wxGridRowOperations());
8444 }
8445
8446 wxGridSizesInfo::wxGridSizesInfo(int defSize, const wxArrayInt& allSizes)
8447 {
8448 m_sizeDefault = defSize;
8449 for ( size_t i = 0; i < allSizes.size(); i++ )
8450 {
8451 if ( allSizes[i] != defSize )
8452 m_customSizes[i] = allSizes[i];
8453 }
8454 }
8455
8456 int wxGridSizesInfo::GetSize(unsigned pos) const
8457 {
8458 wxUnsignedToIntHashMap::const_iterator it = m_customSizes.find(pos);
8459
8460 return it == m_customSizes.end() ? m_sizeDefault : it->second;
8461 }
8462
8463 // ----------------------------------------------------------------------------
8464 // drop target
8465 // ----------------------------------------------------------------------------
8466
8467 #if wxUSE_DRAG_AND_DROP
8468
8469 // this allow setting drop target directly on wxGrid
8470 void wxGrid::SetDropTarget(wxDropTarget *dropTarget)
8471 {
8472 GetGridWindow()->SetDropTarget(dropTarget);
8473 }
8474
8475 #endif // wxUSE_DRAG_AND_DROP
8476
8477 // ----------------------------------------------------------------------------
8478 // grid event classes
8479 // ----------------------------------------------------------------------------
8480
8481 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
8482
8483 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
8484 int row, int col, int x, int y, bool sel,
8485 bool control, bool shift, bool alt, bool meta )
8486 : wxNotifyEvent( type, id ),
8487 wxKeyboardState(control, shift, alt, meta)
8488 {
8489 Init(row, col, x, y, sel);
8490
8491 SetEventObject(obj);
8492 }
8493
8494 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
8495
8496 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
8497 int rowOrCol, int x, int y,
8498 bool control, bool shift, bool alt, bool meta )
8499 : wxNotifyEvent( type, id ),
8500 wxKeyboardState(control, shift, alt, meta)
8501 {
8502 Init(rowOrCol, x, y);
8503
8504 SetEventObject(obj);
8505 }
8506
8507
8508 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
8509
8510 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8511 const wxGridCellCoords& topLeft,
8512 const wxGridCellCoords& bottomRight,
8513 bool sel, bool control,
8514 bool shift, bool alt, bool meta )
8515 : wxNotifyEvent( type, id ),
8516 wxKeyboardState(control, shift, alt, meta)
8517 {
8518 Init(topLeft, bottomRight, sel);
8519
8520 SetEventObject(obj);
8521 }
8522
8523
8524 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
8525
8526 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
8527 wxObject* obj, int row,
8528 int col, wxControl* ctrl)
8529 : wxCommandEvent(type, id)
8530 {
8531 SetEventObject(obj);
8532 m_row = row;
8533 m_col = col;
8534 m_ctrl = ctrl;
8535 }
8536
8537
8538 // ----------------------------------------------------------------------------
8539 // wxGridTypeRegistry
8540 // ----------------------------------------------------------------------------
8541
8542 wxGridTypeRegistry::~wxGridTypeRegistry()
8543 {
8544 size_t count = m_typeinfo.GetCount();
8545 for ( size_t i = 0; i < count; i++ )
8546 delete m_typeinfo[i];
8547 }
8548
8549 void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
8550 wxGridCellRenderer* renderer,
8551 wxGridCellEditor* editor)
8552 {
8553 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
8554
8555 // is it already registered?
8556 int loc = FindRegisteredDataType(typeName);
8557 if ( loc != wxNOT_FOUND )
8558 {
8559 delete m_typeinfo[loc];
8560 m_typeinfo[loc] = info;
8561 }
8562 else
8563 {
8564 m_typeinfo.Add(info);
8565 }
8566 }
8567
8568 int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
8569 {
8570 size_t count = m_typeinfo.GetCount();
8571 for ( size_t i = 0; i < count; i++ )
8572 {
8573 if ( typeName == m_typeinfo[i]->m_typeName )
8574 {
8575 return i;
8576 }
8577 }
8578
8579 return wxNOT_FOUND;
8580 }
8581
8582 int wxGridTypeRegistry::FindDataType(const wxString& typeName)
8583 {
8584 int index = FindRegisteredDataType(typeName);
8585 if ( index == wxNOT_FOUND )
8586 {
8587 // check whether this is one of the standard ones, in which case
8588 // register it "on the fly"
8589 #if wxUSE_TEXTCTRL
8590 if ( typeName == wxGRID_VALUE_STRING )
8591 {
8592 RegisterDataType(wxGRID_VALUE_STRING,
8593 new wxGridCellStringRenderer,
8594 new wxGridCellTextEditor);
8595 }
8596 else
8597 #endif // wxUSE_TEXTCTRL
8598 #if wxUSE_CHECKBOX
8599 if ( typeName == wxGRID_VALUE_BOOL )
8600 {
8601 RegisterDataType(wxGRID_VALUE_BOOL,
8602 new wxGridCellBoolRenderer,
8603 new wxGridCellBoolEditor);
8604 }
8605 else
8606 #endif // wxUSE_CHECKBOX
8607 #if wxUSE_TEXTCTRL
8608 if ( typeName == wxGRID_VALUE_NUMBER )
8609 {
8610 RegisterDataType(wxGRID_VALUE_NUMBER,
8611 new wxGridCellNumberRenderer,
8612 new wxGridCellNumberEditor);
8613 }
8614 else if ( typeName == wxGRID_VALUE_FLOAT )
8615 {
8616 RegisterDataType(wxGRID_VALUE_FLOAT,
8617 new wxGridCellFloatRenderer,
8618 new wxGridCellFloatEditor);
8619 }
8620 else
8621 #endif // wxUSE_TEXTCTRL
8622 #if wxUSE_COMBOBOX
8623 if ( typeName == wxGRID_VALUE_CHOICE )
8624 {
8625 RegisterDataType(wxGRID_VALUE_CHOICE,
8626 new wxGridCellStringRenderer,
8627 new wxGridCellChoiceEditor);
8628 }
8629 else
8630 #endif // wxUSE_COMBOBOX
8631 {
8632 return wxNOT_FOUND;
8633 }
8634
8635 // we get here only if just added the entry for this type, so return
8636 // the last index
8637 index = m_typeinfo.GetCount() - 1;
8638 }
8639
8640 return index;
8641 }
8642
8643 int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
8644 {
8645 int index = FindDataType(typeName);
8646 if ( index == wxNOT_FOUND )
8647 {
8648 // the first part of the typename is the "real" type, anything after ':'
8649 // are the parameters for the renderer
8650 index = FindDataType(typeName.BeforeFirst(wxT(':')));
8651 if ( index == wxNOT_FOUND )
8652 {
8653 return wxNOT_FOUND;
8654 }
8655
8656 wxGridCellRenderer *renderer = GetRenderer(index);
8657 wxGridCellRenderer *rendererOld = renderer;
8658 renderer = renderer->Clone();
8659 rendererOld->DecRef();
8660
8661 wxGridCellEditor *editor = GetEditor(index);
8662 wxGridCellEditor *editorOld = editor;
8663 editor = editor->Clone();
8664 editorOld->DecRef();
8665
8666 // do it even if there are no parameters to reset them to defaults
8667 wxString params = typeName.AfterFirst(wxT(':'));
8668 renderer->SetParameters(params);
8669 editor->SetParameters(params);
8670
8671 // register the new typename
8672 RegisterDataType(typeName, renderer, editor);
8673
8674 // we just registered it, it's the last one
8675 index = m_typeinfo.GetCount() - 1;
8676 }
8677
8678 return index;
8679 }
8680
8681 wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
8682 {
8683 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
8684 if (renderer)
8685 renderer->IncRef();
8686
8687 return renderer;
8688 }
8689
8690 wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
8691 {
8692 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
8693 if (editor)
8694 editor->IncRef();
8695
8696 return editor;
8697 }
8698
8699 #endif // wxUSE_GRID