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