]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Fixes #10247: wxDataViewCtrl - event is generated when clicking on a non existing row
[wxWidgets.git] / src / generic / grid.cpp
CommitLineData
2796cce3 1///////////////////////////////////////////////////////////////////////////
faa94f3e 2// Name: src/generic/grid.cpp
f85afd4e
MB
3// Purpose: wxGrid and related classes
4// Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
d4175745 5// Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
f85afd4e
MB
6// Created: 1/08/1999
7// RCS-ID: $Id$
8// Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
65571936 9// Licence: wxWindows licence
f85afd4e
MB
10/////////////////////////////////////////////////////////////////////////////
11
bec70262
VZ
12/*
13 TODO:
14
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
10a4531d
VZ
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
bec70262
VZ
19 */
20
95427194 21// For compilers that support precompilation, includes "wx/wx.h".
4d85bcd1
JS
22#include "wx/wxprec.h"
23
f85afd4e
MB
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
27b92ca4
VZ
28#if wxUSE_GRID
29
4c44eb66
PC
30#include "wx/grid.h"
31
f85afd4e
MB
32#ifndef WX_PRECOMP
33 #include "wx/utils.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
36 #include "wx/log.h"
508011ce
VZ
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
4ee5fc9c 39 #include "wx/combobox.h"
816be743 40 #include "wx/valtext.h"
60d876f3 41 #include "wx/intl.h"
c77a6796 42 #include "wx/math.h"
2a673eb1 43 #include "wx/listbox.h"
f85afd4e
MB
44#endif
45
cb5df486 46#include "wx/textfile.h"
816be743 47#include "wx/spinctrl.h"
c4608a8a 48#include "wx/tokenzr.h"
4d1bc39c 49#include "wx/renderer.h"
ad805b9e 50#include "wx/headerctrl.h"
6d004f67 51
b5808881 52#include "wx/generic/gridsel.h"
07296f0b 53
23318a53 54const char wxGridNameStr[] = "grid";
4c44eb66 55
0b7e6e7d
SN
56#if defined(__WXMOTIF__)
57 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
c78b3acd 58#else
0b7e6e7d 59 #define WXUNUSED_MOTIF(identifier) identifier
c78b3acd
SN
60#endif
61
62#if defined(__WXGTK__)
63 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
64#else
65 #define WXUNUSED_GTK(identifier) identifier
66#endif
67
3f8e5072
JS
68// Required for wxIs... functions
69#include <ctype.h>
70
b99be8fb 71// ----------------------------------------------------------------------------
758cbedf 72// array classes
b99be8fb
VZ
73// ----------------------------------------------------------------------------
74
d5d29b8a 75WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs,
160ba750 76 class WXDLLIMPEXP_ADV);
758cbedf 77
b99be8fb
VZ
78struct wxGridCellWithAttr
79{
2e9a6788
VZ
80 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
81 : coords(row, col), attr(attr_)
b99be8fb 82 {
6f292345
VZ
83 wxASSERT( attr );
84 }
85
86 wxGridCellWithAttr(const wxGridCellWithAttr& other)
87 : coords(other.coords),
88 attr(other.attr)
89 {
90 attr->IncRef();
91 }
92
93 wxGridCellWithAttr& operator=(const wxGridCellWithAttr& other)
94 {
95 coords = other.coords;
d1b021ff
SN
96 if (attr != other.attr)
97 {
98 attr->DecRef();
99 attr = other.attr;
100 attr->IncRef();
101 }
6f292345 102 return *this;
b99be8fb
VZ
103 }
104
96ca74cd
SN
105 void ChangeAttr(wxGridCellAttr* new_attr)
106 {
107 if (attr != new_attr)
108 {
ace8d849 109 // "Delete" (i.e. DecRef) the old attribute.
96ca74cd
SN
110 attr->DecRef();
111 attr = new_attr;
112 // Take ownership of the new attribute, i.e. no IncRef.
113 }
114 }
115
2e9a6788
VZ
116 ~wxGridCellWithAttr()
117 {
118 attr->DecRef();
119 }
120
b99be8fb 121 wxGridCellCoords coords;
2e9a6788 122 wxGridCellAttr *attr;
b99be8fb
VZ
123};
124
160ba750
VS
125WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray,
126 class WXDLLIMPEXP_ADV);
b99be8fb
VZ
127
128#include "wx/arrimpl.cpp"
129
130WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
131WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
132
0f442030
RR
133// ----------------------------------------------------------------------------
134// events
135// ----------------------------------------------------------------------------
136
2e4df4bf
VZ
137DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK)
138DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK)
139DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK)
140DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK)
79dbea21 141DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG)
2e4df4bf
VZ
142DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK)
143DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK)
144DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK)
145DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK)
146DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE)
147DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE)
d4175745 148DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE)
11393d29 149DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SORT)
2e4df4bf
VZ
150DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT)
151DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE)
152DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL)
153DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN)
154DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN)
bf7945ce 155DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED)
0f442030 156
b99be8fb
VZ
157// ----------------------------------------------------------------------------
158// private classes
159// ----------------------------------------------------------------------------
160
ad805b9e
VZ
161// header column providing access to the column information stored in wxGrid
162// via wxHeaderColumn interface
163class wxGridHeaderColumn : public wxHeaderColumn
164{
165public:
166 wxGridHeaderColumn(wxGrid *grid, int col)
167 : m_grid(grid),
168 m_col(col)
169 {
170 }
171
172 virtual wxString GetTitle() const { return m_grid->GetColLabelValue(m_col); }
173 virtual wxBitmap GetBitmap() const { return wxNullBitmap; }
174 virtual int GetWidth() const { return m_grid->GetColSize(m_col); }
175 virtual int GetMinWidth() const { return 0; }
176 virtual wxAlignment GetAlignment() const
177 {
178 int horz,
179 vert;
180 m_grid->GetColLabelAlignment(&horz, &vert);
181
182 return static_cast<wxAlignment>(horz);
183 }
184
185 virtual int GetFlags() const
186 {
009c7216
VZ
187 // we can't know in advance whether we can sort by this column or not
188 // with wxGrid API so suppose we can by default
189 int flags = wxCOL_SORTABLE;
ad805b9e
VZ
190 if ( m_grid->CanDragColSize() )
191 flags |= wxCOL_RESIZABLE;
192 if ( m_grid->CanDragColMove() )
193 flags |= wxCOL_REORDERABLE;
009c7216
VZ
194 if ( GetWidth() == 0 )
195 flags |= wxCOL_HIDDEN;
ad805b9e
VZ
196
197 return flags;
198 }
199
11393d29
VZ
200 virtual bool IsSortKey() const
201 {
202 return m_grid->IsSortingBy(m_col);
203 }
204
205 virtual bool IsSortOrderAscending() const
206 {
207 return m_grid->IsSortOrderAscending();
208 }
ad805b9e
VZ
209
210private:
e5182b1b
VZ
211 // these really should be const but are not because the column needs to be
212 // assignable to be used in a wxVector (in STL build, in non-STL build we
213 // avoid the need for this)
214 wxGrid *m_grid;
215 int m_col;
ad805b9e
VZ
216};
217
218// header control retreiving column information from the grid
219class wxGridHeaderCtrl : public wxHeaderCtrl
220{
221public:
222 wxGridHeaderCtrl(wxGrid *owner)
223 : wxHeaderCtrl(owner,
224 wxID_ANY,
225 wxDefaultPosition,
226 wxDefaultSize,
613de0e8
VZ
227 wxHD_ALLOW_HIDE |
228 (owner->CanDragColMove() ? wxHD_ALLOW_REORDER : 0))
ad805b9e
VZ
229 {
230 }
231
232protected:
233 virtual wxHeaderColumn& GetColumn(unsigned int idx)
234 {
235 return m_columns[idx];
236 }
237
238private:
239 wxGrid *GetOwner() const { return static_cast<wxGrid *>(GetParent()); }
240
241 // override the base class method to update our m_columns array
242 virtual void OnColumnCountChanging(unsigned int count)
243 {
244 const unsigned countOld = m_columns.size();
245 if ( count < countOld )
246 {
247 // just discard the columns which don't exist any more (notice that
248 // we can't use resize() here as it would require the vector
249 // value_type, i.e. wxGridHeaderColumn to be default constructible,
250 // which it is not)
251 m_columns.erase(m_columns.begin() + count, m_columns.end());
252 }
253 else // new columns added
254 {
255 // add columns for the new elements
256 for ( unsigned n = countOld; n < count; n++ )
257 m_columns.push_back(wxGridHeaderColumn(GetOwner(), n));
258 }
259 }
260
261 // override to implement column auto sizing
262 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
263 {
11393d29
VZ
264 // TODO: currently grid doesn't support computing the column best width
265 // from its contents so we just use the best label width as is
ad805b9e
VZ
266 GetOwner()->SetColSize(idx, widthTitle);
267
268 return true;
269 }
270
613de0e8
VZ
271 // overridden to react to the actions using the columns popup menu
272 virtual void UpdateColumnVisibility(unsigned int idx, bool show)
273 {
274 GetOwner()->SetColSize(idx, show ? wxGRID_AUTOSIZE : 0);
275
276 // as this is done by the user we should notify the main program about
277 // it
278 GetOwner()->SendEvent(wxEVT_GRID_COL_SIZE, -1, idx);
279 }
280
ad805b9e 281 // event handlers forwarding wxHeaderCtrl events to wxGrid
11393d29
VZ
282 void OnClick(wxHeaderCtrlEvent& event)
283 {
284 GetOwner()->DoColHeaderClick(event.GetColumn());
285 }
286
ad805b9e
VZ
287 void OnBeginResize(wxHeaderCtrlEvent& event)
288 {
289 GetOwner()->DoStartResizeCol(event.GetColumn());
290
291 event.Skip();
292 }
293
294 void OnResizing(wxHeaderCtrlEvent& event)
295 {
296 GetOwner()->DoUpdateResizeColWidth(event.GetWidth());
297 }
298
299 void OnEndResize(wxHeaderCtrlEvent& event)
300 {
301 GetOwner()->DoEndDragResizeCol();
302
303 event.Skip();
304 }
305
cd68daf5
VZ
306 void OnBeginReorder(wxHeaderCtrlEvent& event)
307 {
308 GetOwner()->DoStartMoveCol(event.GetColumn());
309 }
310
ad805b9e
VZ
311 void OnEndReorder(wxHeaderCtrlEvent& event)
312 {
cd68daf5 313 GetOwner()->DoEndMoveCol(event.GetNewOrder());
ad805b9e
VZ
314 }
315
316 wxVector<wxGridHeaderColumn> m_columns;
317
318 DECLARE_EVENT_TABLE()
319 DECLARE_NO_COPY_CLASS(wxGridHeaderCtrl)
320};
321
322BEGIN_EVENT_TABLE(wxGridHeaderCtrl, wxHeaderCtrl)
11393d29
VZ
323 EVT_HEADER_CLICK(wxID_ANY, wxGridHeaderCtrl::OnClick)
324
ad805b9e
VZ
325 EVT_HEADER_BEGIN_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnBeginResize)
326 EVT_HEADER_RESIZING(wxID_ANY, wxGridHeaderCtrl::OnResizing)
327 EVT_HEADER_END_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnEndResize)
328
cd68daf5 329 EVT_HEADER_BEGIN_REORDER(wxID_ANY, wxGridHeaderCtrl::OnBeginReorder)
ad805b9e
VZ
330 EVT_HEADER_END_REORDER(wxID_ANY, wxGridHeaderCtrl::OnEndReorder)
331END_EVENT_TABLE()
332
86033c4b
VZ
333// common base class for various grid subwindows
334class WXDLLIMPEXP_ADV wxGridSubwindow : public wxWindow
b99be8fb
VZ
335{
336public:
86033c4b 337 wxGridSubwindow(wxGrid *owner,
86033c4b
VZ
338 int additionalStyle = 0,
339 const wxString& name = wxPanelNameStr)
ad805b9e
VZ
340 : wxWindow(owner, wxID_ANY,
341 wxDefaultPosition, wxDefaultSize,
760be3f7 342 wxBORDER_NONE | additionalStyle,
86033c4b
VZ
343 name)
344 {
345 m_owner = owner;
346 }
347
760be3f7
VS
348 virtual bool AcceptsFocus() const { return false; }
349
86033c4b
VZ
350 wxGrid *GetOwner() { return m_owner; }
351
352protected:
353 void OnMouseCaptureLost(wxMouseCaptureLostEvent& event);
354
355 wxGrid *m_owner;
356
357 DECLARE_EVENT_TABLE()
358 DECLARE_NO_COPY_CLASS(wxGridSubwindow)
359};
360
361class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxGridSubwindow
362{
363public:
ad805b9e
VZ
364 wxGridRowLabelWindow(wxGrid *parent)
365 : wxGridSubwindow(parent)
366 {
367 }
368
b99be8fb
VZ
369
370private:
b99be8fb
VZ
371 void OnPaint( wxPaintEvent& event );
372 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 373 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 374
b99be8fb 375 DECLARE_EVENT_TABLE()
22f3361e 376 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow)
b99be8fb
VZ
377};
378
379
86033c4b 380class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxGridSubwindow
b99be8fb
VZ
381{
382public:
ad805b9e
VZ
383 wxGridColLabelWindow(wxGrid *parent)
384 : wxGridSubwindow(parent)
385 {
386 }
387
b99be8fb
VZ
388
389private:
a9339fe2 390 void OnPaint( wxPaintEvent& event );
b99be8fb 391 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 392 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 393
b99be8fb 394 DECLARE_EVENT_TABLE()
22f3361e 395 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow)
b99be8fb
VZ
396};
397
398
86033c4b 399class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxGridSubwindow
b99be8fb
VZ
400{
401public:
ad805b9e
VZ
402 wxGridCornerLabelWindow(wxGrid *parent)
403 : wxGridSubwindow(parent)
404 {
405 }
b99be8fb
VZ
406
407private:
b99be8fb 408 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 409 void OnMouseWheel( wxMouseEvent& event );
b99be8fb
VZ
410 void OnPaint( wxPaintEvent& event );
411
b99be8fb 412 DECLARE_EVENT_TABLE()
22f3361e 413 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow)
b99be8fb
VZ
414};
415
86033c4b 416class WXDLLIMPEXP_ADV wxGridWindow : public wxGridSubwindow
b99be8fb
VZ
417{
418public:
ad805b9e
VZ
419 wxGridWindow(wxGrid *parent)
420 : wxGridSubwindow(parent,
421 wxWANTS_CHARS | wxCLIP_CHILDREN,
422 "GridWindow")
b99be8fb 423 {
b99be8fb
VZ
424 }
425
b99be8fb 426
ad805b9e 427 virtual void ScrollWindow( int dx, int dy, const wxRect *rect );
b99be8fb 428
760be3f7
VS
429 virtual bool AcceptsFocus() const { return true; }
430
b99be8fb 431private:
b99be8fb 432 void OnPaint( wxPaintEvent &event );
b51c3f27 433 void OnMouseWheel( wxMouseEvent& event );
b99be8fb
VZ
434 void OnMouseEvent( wxMouseEvent& event );
435 void OnKeyDown( wxKeyEvent& );
f6bcfd97 436 void OnKeyUp( wxKeyEvent& );
63e2147c 437 void OnChar( wxKeyEvent& );
2796cce3 438 void OnEraseBackground( wxEraseEvent& );
80acaf25 439 void OnFocus( wxFocusEvent& );
b99be8fb 440
b99be8fb 441 DECLARE_EVENT_TABLE()
22f3361e 442 DECLARE_NO_COPY_CLASS(wxGridWindow)
b99be8fb
VZ
443};
444
2796cce3 445
2796cce3
RD
446class wxGridCellEditorEvtHandler : public wxEvtHandler
447{
448public:
2796cce3 449 wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
140954fd 450 : m_grid(grid),
08dd04d0
JS
451 m_editor(editor),
452 m_inSetFocus(false)
140954fd
VZ
453 {
454 }
2796cce3 455
140954fd 456 void OnKillFocus(wxFocusEvent& event);
2796cce3 457 void OnKeyDown(wxKeyEvent& event);
fb0de762 458 void OnChar(wxKeyEvent& event);
2796cce3 459
08dd04d0
JS
460 void SetInSetFocus(bool inSetFocus) { m_inSetFocus = inSetFocus; }
461
2796cce3 462private:
2f024384
DS
463 wxGrid *m_grid;
464 wxGridCellEditor *m_editor;
140954fd 465
08dd04d0
JS
466 // Work around the fact that a focus kill event can be sent to
467 // a combobox within a set focus event.
468 bool m_inSetFocus;
7448de8d 469
2796cce3 470 DECLARE_EVENT_TABLE()
140954fd 471 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
22f3361e 472 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler)
2796cce3
RD
473};
474
475
140954fd
VZ
476IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler)
477
2796cce3 478BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
140954fd 479 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus )
2796cce3 480 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
fb0de762 481 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
2796cce3
RD
482END_EVENT_TABLE()
483
484
758cbedf 485// ----------------------------------------------------------------------------
b99be8fb 486// the internal data representation used by wxGridCellAttrProvider
758cbedf
VZ
487// ----------------------------------------------------------------------------
488
489// this class stores attributes set for cells
12f190b0 490class WXDLLIMPEXP_ADV wxGridCellAttrData
b99be8fb
VZ
491{
492public:
2e9a6788 493 void SetAttr(wxGridCellAttr *attr, int row, int col);
b99be8fb 494 wxGridCellAttr *GetAttr(int row, int col) const;
4d60017a
SN
495 void UpdateAttrRows( size_t pos, int numRows );
496 void UpdateAttrCols( size_t pos, int numCols );
b99be8fb
VZ
497
498private:
499 // searches for the attr for given cell, returns wxNOT_FOUND if not found
500 int FindIndex(int row, int col) const;
501
502 wxGridCellWithAttrArray m_attrs;
503};
504
758cbedf 505// this class stores attributes set for rows or columns
12f190b0 506class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
758cbedf
VZ
507{
508public:
ee6694a7 509 // empty ctor to suppress warnings
2f024384 510 wxGridRowOrColAttrData() {}
758cbedf
VZ
511 ~wxGridRowOrColAttrData();
512
513 void SetAttr(wxGridCellAttr *attr, int rowOrCol);
514 wxGridCellAttr *GetAttr(int rowOrCol) const;
4d60017a 515 void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
758cbedf
VZ
516
517private:
518 wxArrayInt m_rowsOrCols;
519 wxArrayAttrs m_attrs;
520};
521
522// NB: this is just a wrapper around 3 objects: one which stores cell
523// attributes, and 2 others for row/col ones
12f190b0 524class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
758cbedf
VZ
525{
526public:
527 wxGridCellAttrData m_cellAttrs;
528 wxGridRowOrColAttrData m_rowAttrs,
529 m_colAttrs;
530};
531
f2d76237
RD
532
533// ----------------------------------------------------------------------------
534// data structures used for the data type registry
535// ----------------------------------------------------------------------------
536
b94ae1ea
VZ
537struct wxGridDataTypeInfo
538{
f2d76237
RD
539 wxGridDataTypeInfo(const wxString& typeName,
540 wxGridCellRenderer* renderer,
541 wxGridCellEditor* editor)
542 : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
2f024384 543 {}
f2d76237 544
39bcce60
VZ
545 ~wxGridDataTypeInfo()
546 {
547 wxSafeDecRef(m_renderer);
548 wxSafeDecRef(m_editor);
549 }
f2d76237
RD
550
551 wxString m_typeName;
552 wxGridCellRenderer* m_renderer;
553 wxGridCellEditor* m_editor;
22f3361e
VZ
554
555 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo)
f2d76237
RD
556};
557
558
d5d29b8a 559WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray,
160ba750 560 class WXDLLIMPEXP_ADV);
f2d76237
RD
561
562
12f190b0 563class WXDLLIMPEXP_ADV wxGridTypeRegistry
b94ae1ea 564{
f2d76237 565public:
bec70262 566 wxGridTypeRegistry() {}
f2d76237 567 ~wxGridTypeRegistry();
b94ae1ea 568
f2d76237
RD
569 void RegisterDataType(const wxString& typeName,
570 wxGridCellRenderer* renderer,
571 wxGridCellEditor* editor);
c4608a8a
VZ
572
573 // find one of already registered data types
574 int FindRegisteredDataType(const wxString& typeName);
575
576 // try to FindRegisteredDataType(), if this fails and typeName is one of
577 // standard typenames, register it and return its index
f2d76237 578 int FindDataType(const wxString& typeName);
c4608a8a
VZ
579
580 // try to FindDataType(), if it fails see if it is not one of already
581 // registered data types with some params in which case clone the
582 // registered data type and set params for it
583 int FindOrCloneDataType(const wxString& typeName);
584
f2d76237
RD
585 wxGridCellRenderer* GetRenderer(int index);
586 wxGridCellEditor* GetEditor(int index);
587
588private:
589 wxGridDataTypeInfoArray m_typeinfo;
590};
591
bec70262
VZ
592// ----------------------------------------------------------------------------
593// operations classes abstracting the difference between operating on rows and
594// columns
595// ----------------------------------------------------------------------------
596
597// This class allows to write a function only once because by using its methods
598// it will apply to both columns and rows.
599//
600// This is an abstract interface definition, the two concrete implementations
601// below should be used when working with rows and columns respectively.
602class wxGridOperations
603{
604public:
605 // Returns the operations in the other direction, i.e. wxGridRowOperations
606 // if this object is a wxGridColumnOperations and vice versa.
607 virtual wxGridOperations& Dual() const = 0;
608
609 // Return the number of rows or columns.
610 virtual int GetNumberOfLines(const wxGrid *grid) const = 0;
611
612 // Return the selection mode which allows selecting rows or columns.
613 virtual wxGrid::wxGridSelectionModes GetSelectionMode() const = 0;
614
615 // Make a wxGridCellCoords from the given components: thisDir is row or
616 // column and otherDir is column or row
617 virtual wxGridCellCoords MakeCoords(int thisDir, int otherDir) const = 0;
618
619 // Calculate the scrolled position of the given abscissa or ordinate.
620 virtual int CalcScrolledPosition(wxGrid *grid, int pos) const = 0;
621
622 // Selects the horizontal or vertical component from the given object.
10a4531d 623 virtual int Select(const wxGridCellCoords& coords) const = 0;
bec70262
VZ
624 virtual int Select(const wxPoint& pt) const = 0;
625 virtual int Select(const wxSize& sz) const = 0;
626 virtual int Select(const wxRect& r) const = 0;
627 virtual int& Select(wxRect& r) const = 0;
628
629 // Returns width or height of the rectangle
630 virtual int& SelectSize(wxRect& r) const = 0;
631
632 // Make a wxSize such that Select() applied to it returns first component
633 virtual wxSize MakeSize(int first, int second) const = 0;
634
10a4531d
VZ
635 // Sets the row or column component of the given cell coordinates
636 virtual void Set(wxGridCellCoords& coords, int line) const = 0;
637
bec70262
VZ
638
639 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
8a3e536c 640 // pos is the horizontal or vertical position of the line and start and end
bec70262
VZ
641 // are the coordinates of the line extremities in the other direction
642 virtual void
643 DrawParallelLine(wxDC& dc, int start, int end, int pos) const = 0;
644
8a3e536c
VZ
645 // Draw a horizontal or vertical line across the given rectangle
646 // (this is implemented in terms of above and uses Select() to extract
647 // start and end from the given rectangle)
648 void DrawParallelLineInRect(wxDC& dc, const wxRect& rect, int pos) const
649 {
650 const int posStart = Select(rect.GetPosition());
651 DrawParallelLine(dc, posStart, posStart + Select(rect.GetSize()), pos);
652 }
653
bec70262 654
cd4f6f5f 655 // Return the index of the row or column at the given pixel coordinate.
10a4531d
VZ
656 virtual int
657 PosToLine(const wxGrid *grid, int pos, bool clip = false) const = 0;
bec70262
VZ
658
659 // Get the top/left position, in pixels, of the given row or column
660 virtual int GetLineStartPos(const wxGrid *grid, int line) const = 0;
661
10a4531d
VZ
662 // Get the bottom/right position, in pixels, of the given row or column
663 virtual int GetLineEndPos(const wxGrid *grid, int line) const = 0;
664
665 // Get the height/width of the given row/column
666 virtual int GetLineSize(const wxGrid *grid, int line) const = 0;
667
bec70262
VZ
668 // Get wxGrid::m_rowBottoms/m_colRights array
669 virtual const wxArrayInt& GetLineEnds(const wxGrid *grid) const = 0;
670
671 // Get default height row height or column width
672 virtual int GetDefaultLineSize(const wxGrid *grid) const = 0;
673
674 // Return the minimal acceptable row height or column width
675 virtual int GetMinimalAcceptableLineSize(const wxGrid *grid) const = 0;
676
677 // Return the minimal row height or column width
678 virtual int GetMinimalLineSize(const wxGrid *grid, int line) const = 0;
679
680 // Set the row height or column width
681 virtual void SetLineSize(wxGrid *grid, int line, int size) const = 0;
682
10a4531d
VZ
683 // True if rows/columns can be resized by user
684 virtual bool CanResizeLines(const wxGrid *grid) const = 0;
685
bec70262
VZ
686
687 // Return the index of the line at the given position
688 //
689 // NB: currently this is always identity for the rows as reordering is only
690 // implemented for the lines
691 virtual int GetLineAt(const wxGrid *grid, int line) const = 0;
692
693
694 // Get the row or column label window
695 virtual wxWindow *GetHeaderWindow(wxGrid *grid) const = 0;
696
697 // Get the width or height of the row or column label window
698 virtual int GetHeaderWindowSize(wxGrid *grid) const = 0;
10a4531d
VZ
699
700
701 // This class is never used polymorphically but give it a virtual dtor
702 // anyhow to suppress g++ complaints about it
703 virtual ~wxGridOperations() { }
bec70262
VZ
704};
705
706class wxGridRowOperations : public wxGridOperations
707{
708public:
709 virtual wxGridOperations& Dual() const;
710
711 virtual int GetNumberOfLines(const wxGrid *grid) const
712 { return grid->GetNumberRows(); }
713
714 virtual wxGrid::wxGridSelectionModes GetSelectionMode() const
715 { return wxGrid::wxGridSelectRows; }
716
717 virtual wxGridCellCoords MakeCoords(int thisDir, int otherDir) const
718 { return wxGridCellCoords(thisDir, otherDir); }
719
720 virtual int CalcScrolledPosition(wxGrid *grid, int pos) const
721 { return grid->CalcScrolledPosition(wxPoint(pos, 0)).x; }
722
10a4531d 723 virtual int Select(const wxGridCellCoords& c) const { return c.GetRow(); }
bec70262
VZ
724 virtual int Select(const wxPoint& pt) const { return pt.x; }
725 virtual int Select(const wxSize& sz) const { return sz.x; }
726 virtual int Select(const wxRect& r) const { return r.x; }
727 virtual int& Select(wxRect& r) const { return r.x; }
728 virtual int& SelectSize(wxRect& r) const { return r.width; }
729 virtual wxSize MakeSize(int first, int second) const
730 { return wxSize(first, second); }
10a4531d
VZ
731 virtual void Set(wxGridCellCoords& coords, int line) const
732 { coords.SetRow(line); }
bec70262
VZ
733
734 virtual void DrawParallelLine(wxDC& dc, int start, int end, int pos) const
735 { dc.DrawLine(start, pos, end, pos); }
736
10a4531d 737 virtual int PosToLine(const wxGrid *grid, int pos, bool clip = false) const
bec70262
VZ
738 { return grid->YToRow(pos, clip); }
739 virtual int GetLineStartPos(const wxGrid *grid, int line) const
740 { return grid->GetRowTop(line); }
10a4531d
VZ
741 virtual int GetLineEndPos(const wxGrid *grid, int line) const
742 { return grid->GetRowBottom(line); }
743 virtual int GetLineSize(const wxGrid *grid, int line) const
744 { return grid->GetRowHeight(line); }
bec70262
VZ
745 virtual const wxArrayInt& GetLineEnds(const wxGrid *grid) const
746 { return grid->m_rowBottoms; }
747 virtual int GetDefaultLineSize(const wxGrid *grid) const
748 { return grid->GetDefaultRowSize(); }
749 virtual int GetMinimalAcceptableLineSize(const wxGrid *grid) const
750 { return grid->GetRowMinimalAcceptableHeight(); }
751 virtual int GetMinimalLineSize(const wxGrid *grid, int line) const
752 { return grid->GetRowMinimalHeight(line); }
753 virtual void SetLineSize(wxGrid *grid, int line, int size) const
754 { grid->SetRowSize(line, size); }
10a4531d
VZ
755 virtual bool CanResizeLines(const wxGrid *grid) const
756 { return grid->CanDragRowSize(); }
bec70262
VZ
757
758 virtual int GetLineAt(const wxGrid * WXUNUSED(grid), int line) const
759 { return line; } // TODO: implement row reordering
760
761 virtual wxWindow *GetHeaderWindow(wxGrid *grid) const
762 { return grid->GetGridRowLabelWindow(); }
763 virtual int GetHeaderWindowSize(wxGrid *grid) const
764 { return grid->GetRowLabelSize(); }
765};
766
767class wxGridColumnOperations : public wxGridOperations
768{
769public:
770 virtual wxGridOperations& Dual() const;
771
772 virtual int GetNumberOfLines(const wxGrid *grid) const
773 { return grid->GetNumberCols(); }
774
775 virtual wxGrid::wxGridSelectionModes GetSelectionMode() const
776 { return wxGrid::wxGridSelectColumns; }
777
778 virtual wxGridCellCoords MakeCoords(int thisDir, int otherDir) const
779 { return wxGridCellCoords(otherDir, thisDir); }
780
781 virtual int CalcScrolledPosition(wxGrid *grid, int pos) const
782 { return grid->CalcScrolledPosition(wxPoint(0, pos)).y; }
783
10a4531d 784 virtual int Select(const wxGridCellCoords& c) const { return c.GetCol(); }
bec70262
VZ
785 virtual int Select(const wxPoint& pt) const { return pt.y; }
786 virtual int Select(const wxSize& sz) const { return sz.y; }
787 virtual int Select(const wxRect& r) const { return r.y; }
788 virtual int& Select(wxRect& r) const { return r.y; }
789 virtual int& SelectSize(wxRect& r) const { return r.height; }
790 virtual wxSize MakeSize(int first, int second) const
791 { return wxSize(second, first); }
10a4531d
VZ
792 virtual void Set(wxGridCellCoords& coords, int line) const
793 { coords.SetCol(line); }
bec70262
VZ
794
795 virtual void DrawParallelLine(wxDC& dc, int start, int end, int pos) const
796 { dc.DrawLine(pos, start, pos, end); }
797
10a4531d 798 virtual int PosToLine(const wxGrid *grid, int pos, bool clip = false) const
bec70262
VZ
799 { return grid->XToCol(pos, clip); }
800 virtual int GetLineStartPos(const wxGrid *grid, int line) const
801 { return grid->GetColLeft(line); }
10a4531d
VZ
802 virtual int GetLineEndPos(const wxGrid *grid, int line) const
803 { return grid->GetColRight(line); }
804 virtual int GetLineSize(const wxGrid *grid, int line) const
805 { return grid->GetColWidth(line); }
bec70262
VZ
806 virtual const wxArrayInt& GetLineEnds(const wxGrid *grid) const
807 { return grid->m_colRights; }
808 virtual int GetDefaultLineSize(const wxGrid *grid) const
809 { return grid->GetDefaultColSize(); }
810 virtual int GetMinimalAcceptableLineSize(const wxGrid *grid) const
811 { return grid->GetColMinimalAcceptableWidth(); }
812 virtual int GetMinimalLineSize(const wxGrid *grid, int line) const
813 { return grid->GetColMinimalWidth(line); }
814 virtual void SetLineSize(wxGrid *grid, int line, int size) const
815 { grid->SetColSize(line, size); }
10a4531d
VZ
816 virtual bool CanResizeLines(const wxGrid *grid) const
817 { return grid->CanDragColSize(); }
bec70262
VZ
818
819 virtual int GetLineAt(const wxGrid *grid, int line) const
820 { return grid->GetColAt(line); }
821
822 virtual wxWindow *GetHeaderWindow(wxGrid *grid) const
823 { return grid->GetGridColLabelWindow(); }
824 virtual int GetHeaderWindowSize(wxGrid *grid) const
825 { return grid->GetColLabelSize(); }
826};
827
828wxGridOperations& wxGridRowOperations::Dual() const
829{
830 static wxGridColumnOperations s_colOper;
831
832 return s_colOper;
833}
834
835wxGridOperations& wxGridColumnOperations::Dual() const
836{
837 static wxGridRowOperations s_rowOper;
838
839 return s_rowOper;
840}
841
10a4531d
VZ
842// This class abstracts the difference between operations going forward
843// (down/right) and backward (up/left) and allows to use the same code for
844// functions which differ only in the direction of grid traversal
845//
846// Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
847// it, this is a normal object and not just a function dispatch table and has a
848// non-default ctor.
849//
850// Note: the explanation of this discrepancy is the existence of (very useful)
851// Dual() method in wxGridOperations which forces us to make wxGridOperations a
852// function dispatcher only.
853class wxGridDirectionOperations
854{
855public:
856 // The oper parameter to ctor selects whether we work with rows or columns
857 wxGridDirectionOperations(wxGrid *grid, const wxGridOperations& oper)
858 : m_grid(grid),
859 m_oper(oper)
860 {
861 }
862
863 // Check if the component of this point in our direction is at the
864 // boundary, i.e. is the first/last row/column
865 virtual bool IsAtBoundary(const wxGridCellCoords& coords) const = 0;
866
867 // Increment the component of this point in our direction
868 virtual void Advance(wxGridCellCoords& coords) const = 0;
869
870 // Find the line at the given distance, in pixels, away from this one
871 // (this uses clipping, i.e. anything after the last line is counted as the
872 // last one and anything before the first one as 0)
873 virtual int MoveByPixelDistance(int line, int distance) const = 0;
874
875 // This class is never used polymorphically but give it a virtual dtor
876 // anyhow to suppress g++ complaints about it
877 virtual ~wxGridDirectionOperations() { }
878
879protected:
880 wxGrid * const m_grid;
881 const wxGridOperations& m_oper;
882};
883
884class wxGridBackwardOperations : public wxGridDirectionOperations
885{
886public:
887 wxGridBackwardOperations(wxGrid *grid, const wxGridOperations& oper)
888 : wxGridDirectionOperations(grid, oper)
889 {
890 }
891
892 virtual bool IsAtBoundary(const wxGridCellCoords& coords) const
893 {
894 wxASSERT_MSG( m_oper.Select(coords) >= 0, "invalid row/column" );
895
896 return m_oper.Select(coords) == 0;
897 }
898
899 virtual void Advance(wxGridCellCoords& coords) const
900 {
901 wxASSERT( !IsAtBoundary(coords) );
902
903 m_oper.Set(coords, m_oper.Select(coords) - 1);
904 }
905
906 virtual int MoveByPixelDistance(int line, int distance) const
907 {
908 int pos = m_oper.GetLineStartPos(m_grid, line);
909 return m_oper.PosToLine(m_grid, pos - distance + 1, true);
910 }
911};
912
913class wxGridForwardOperations : public wxGridDirectionOperations
914{
915public:
916 wxGridForwardOperations(wxGrid *grid, const wxGridOperations& oper)
917 : wxGridDirectionOperations(grid, oper),
918 m_numLines(oper.GetNumberOfLines(grid))
919 {
920 }
921
922 virtual bool IsAtBoundary(const wxGridCellCoords& coords) const
923 {
924 wxASSERT_MSG( m_oper.Select(coords) < m_numLines, "invalid row/column" );
925
926 return m_oper.Select(coords) == m_numLines - 1;
927 }
928
929 virtual void Advance(wxGridCellCoords& coords) const
930 {
931 wxASSERT( !IsAtBoundary(coords) );
932
933 m_oper.Set(coords, m_oper.Select(coords) + 1);
934 }
935
936 virtual int MoveByPixelDistance(int line, int distance) const
937 {
938 int pos = m_oper.GetLineStartPos(m_grid, line);
939 return m_oper.PosToLine(m_grid, pos + distance, true);
940 }
941
942private:
943 const int m_numLines;
944};
bec70262 945
0a976765
VZ
946// ----------------------------------------------------------------------------
947// globals
948// ----------------------------------------------------------------------------
949
950//#define DEBUG_ATTR_CACHE
951#ifdef DEBUG_ATTR_CACHE
952 static size_t gs_nAttrCacheHits = 0;
953 static size_t gs_nAttrCacheMisses = 0;
2f024384 954#endif
f85afd4e 955
43947979
VZ
956// ----------------------------------------------------------------------------
957// constants
958// ----------------------------------------------------------------------------
959
f85afd4e 960wxGridCellCoords wxGridNoCellCoords( -1, -1 );
2f024384 961wxRect wxGridNoCellRect( -1, -1, -1, -1 );
f85afd4e 962
8a3e536c
VZ
963namespace
964{
965
f0102d2a 966// scroll line size
8a3e536c
VZ
967const size_t GRID_SCROLL_LINE_X = 15;
968const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
f85afd4e 969
43947979
VZ
970// the size of hash tables used a bit everywhere (the max number of elements
971// in these hash tables is the number of rows/columns)
8a3e536c
VZ
972const int GRID_HASH_SIZE = 100;
973
974// the minimal distance in pixels the mouse needs to move to start a drag
975// operation
976const int DRAG_SENSITIVITY = 3;
977
978} // anonymous namespace
43947979 979
1372f8cc
VZ
980// ----------------------------------------------------------------------------
981// private helpers
982// ----------------------------------------------------------------------------
983
984namespace
985{
986
987// ensure that first is less or equal to second, swapping the values if
988// necessary
989void EnsureFirstLessThanSecond(int& first, int& second)
990{
991 if ( first > second )
992 wxSwap(first, second);
993}
994
995} // anonymous namespace
996
ab79958a
VZ
997// ============================================================================
998// implementation
999// ============================================================================
1000
2796cce3
RD
1001// ----------------------------------------------------------------------------
1002// wxGridCellEditor
1003// ----------------------------------------------------------------------------
1004
1005wxGridCellEditor::wxGridCellEditor()
1006{
1007 m_control = NULL;
1bd71df9 1008 m_attr = NULL;
2796cce3
RD
1009}
1010
2796cce3
RD
1011wxGridCellEditor::~wxGridCellEditor()
1012{
1013 Destroy();
1014}
1015
508011ce
VZ
1016void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
1017 wxWindowID WXUNUSED(id),
1018 wxEvtHandler* evtHandler)
1019{
189d0213 1020 if ( evtHandler )
508011ce
VZ
1021 m_control->PushEventHandler(evtHandler);
1022}
2796cce3 1023
189d0213
VZ
1024void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
1025 wxGridCellAttr *attr)
1026{
1027 // erase the background because we might not fill the cell
1028 wxClientDC dc(m_control->GetParent());
b819b854
JS
1029 wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow);
1030 if (gridWindow)
1031 gridWindow->GetOwner()->PrepareDC(dc);
ef5df12b 1032
189d0213 1033 dc.SetPen(*wxTRANSPARENT_PEN);
ff72f628 1034 dc.SetBrush(wxBrush(attr->GetBackgroundColour()));
189d0213
VZ
1035 dc.DrawRectangle(rectCell);
1036
1037 // redraw the control we just painted over
1038 m_control->Refresh();
1039}
1040
2796cce3
RD
1041void wxGridCellEditor::Destroy()
1042{
508011ce
VZ
1043 if (m_control)
1044 {
a9339fe2 1045 m_control->PopEventHandler( true /* delete it*/ );
b94ae1ea 1046
2796cce3
RD
1047 m_control->Destroy();
1048 m_control = NULL;
1049 }
1050}
1051
3da93aae 1052void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
2796cce3 1053{
2f024384
DS
1054 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
1055
2796cce3 1056 m_control->Show(show);
3da93aae
VZ
1057
1058 if ( show )
1059 {
1060 // set the colours/fonts if we have any
1061 if ( attr )
1062 {
f2d76237
RD
1063 m_colFgOld = m_control->GetForegroundColour();
1064 m_control->SetForegroundColour(attr->GetTextColour());
3da93aae 1065
f2d76237
RD
1066 m_colBgOld = m_control->GetBackgroundColour();
1067 m_control->SetBackgroundColour(attr->GetBackgroundColour());
3da93aae 1068
0e871ad0 1069// Workaround for GTK+1 font setting problem on some platforms
ea2d542c 1070#if !defined(__WXGTK__) || defined(__WXGTK20__)
f2d76237
RD
1071 m_fontOld = m_control->GetFont();
1072 m_control->SetFont(attr->GetFont());
ea2d542c 1073#endif
a9339fe2 1074
3da93aae
VZ
1075 // can't do anything more in the base class version, the other
1076 // attributes may only be used by the derived classes
1077 }
1078 }
1079 else
1080 {
1081 // restore the standard colours fonts
1082 if ( m_colFgOld.Ok() )
1083 {
1084 m_control->SetForegroundColour(m_colFgOld);
1085 m_colFgOld = wxNullColour;
1086 }
1087
1088 if ( m_colBgOld.Ok() )
1089 {
1090 m_control->SetBackgroundColour(m_colBgOld);
1091 m_colBgOld = wxNullColour;
1092 }
2f024384 1093
0e871ad0 1094// Workaround for GTK+1 font setting problem on some platforms
ea2d542c 1095#if !defined(__WXGTK__) || defined(__WXGTK20__)
3da93aae
VZ
1096 if ( m_fontOld.Ok() )
1097 {
1098 m_control->SetFont(m_fontOld);
1099 m_fontOld = wxNullFont;
1100 }
ea2d542c 1101#endif
3da93aae 1102 }
2796cce3
RD
1103}
1104
1105void wxGridCellEditor::SetSize(const wxRect& rect)
1106{
2f024384
DS
1107 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
1108
28a77bc4 1109 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
2796cce3
RD
1110}
1111
1112void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
1113{
1114 event.Skip();
1115}
1116
f6bcfd97
BP
1117bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
1118{
63e2147c
RD
1119 bool ctrl = event.ControlDown();
1120 bool alt = event.AltDown();
2f024384 1121
63e2147c
RD
1122#ifdef __WXMAC__
1123 // On the Mac the Alt key is more like shift and is used for entry of
1124 // valid characters, so check for Ctrl and Meta instead.
1125 alt = event.MetaDown();
1126#endif
1127
1128 // Assume it's not a valid char if ctrl or alt is down, but if both are
1129 // down then it may be because of an AltGr key combination, so let them
1130 // through in that case.
1131 if ((ctrl || alt) && !(ctrl && alt))
1132 return false;
902725ee 1133
2f024384 1134#if wxUSE_UNICODE
63e2147c
RD
1135 // if the unicode key code is not really a unicode character (it may
1136 // be a function key or etc., the platforms appear to always give us a
2f024384 1137 // small value in this case) then fallback to the ASCII key code but
63e2147c 1138 // don't do anything for function keys or etc.
e822d1bd
VZ
1139 if ( event.GetUnicodeKey() > 127 && event.GetKeyCode() > 127 )
1140 return false;
2f024384 1141#else
e822d1bd
VZ
1142 if ( event.GetKeyCode() > 255 )
1143 return false;
2f024384
DS
1144#endif
1145
e822d1bd 1146 return true;
f6bcfd97 1147}
2796cce3 1148
2c9a89e0
RD
1149void wxGridCellEditor::StartingKey(wxKeyEvent& event)
1150{
e195a54c
VZ
1151 event.Skip();
1152}
2c9a89e0 1153
e195a54c
VZ
1154void wxGridCellEditor::StartingClick()
1155{
b54ba671 1156}
2c9a89e0 1157
3a8c693a
VZ
1158#if wxUSE_TEXTCTRL
1159
b54ba671
VZ
1160// ----------------------------------------------------------------------------
1161// wxGridCellTextEditor
1162// ----------------------------------------------------------------------------
2c9a89e0 1163
2796cce3
RD
1164wxGridCellTextEditor::wxGridCellTextEditor()
1165{
c4608a8a 1166 m_maxChars = 0;
2796cce3
RD
1167}
1168
1169void wxGridCellTextEditor::Create(wxWindow* parent,
1170 wxWindowID id,
2796cce3
RD
1171 wxEvtHandler* evtHandler)
1172{
1d5fda5d
VZ
1173 DoCreate(parent, id, evtHandler);
1174}
1175
1176void wxGridCellTextEditor::DoCreate(wxWindow* parent,
1177 wxWindowID id,
1178 wxEvtHandler* evtHandler,
1179 long style)
1180{
053ac76f 1181 style |= wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxNO_BORDER;
1d5fda5d
VZ
1182
1183 m_control = new wxTextCtrl(parent, id, wxEmptyString,
1184 wxDefaultPosition, wxDefaultSize,
1185 style);
2796cce3 1186
46a5010a 1187 // set max length allowed in the textctrl, if the parameter was set
1d5fda5d 1188 if ( m_maxChars != 0 )
46a5010a 1189 {
1d5fda5d 1190 Text()->SetMaxLength(m_maxChars);
46a5010a 1191 }
c4608a8a 1192
508011ce 1193 wxGridCellEditor::Create(parent, id, evtHandler);
2796cce3
RD
1194}
1195
189d0213
VZ
1196void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
1197 wxGridCellAttr * WXUNUSED(attr))
1198{
a9339fe2
DS
1199 // as we fill the entire client area,
1200 // don't do anything here to minimize flicker
189d0213 1201}
2796cce3 1202
99306db2
VZ
1203void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
1204{
1205 wxRect rect(rectOrig);
1206
2f024384 1207 // Make the edit control large enough to allow for internal margins
99306db2 1208 //
2f024384 1209 // TODO: remove this if the text ctrl sizing is improved esp. for unix
99306db2
VZ
1210 //
1211#if defined(__WXGTK__)
b0e282b3
RR
1212 if (rect.x != 0)
1213 {
1214 rect.x += 1;
1215 rect.y += 1;
1216 rect.width -= 1;
1217 rect.height -= 1;
1218 }
d4175745
VZ
1219#elif defined(__WXMSW__)
1220 if ( rect.x == 0 )
1221 rect.x += 2;
1222 else
1223 rect.x += 3;
84912ef8 1224
d4175745
VZ
1225 if ( rect.y == 0 )
1226 rect.y += 2;
1227 else
1228 rect.y += 3;
1229
1230 rect.width -= 2;
1231 rect.height -= 2;
a0948e27 1232#else
d4175745 1233 int extra_x = ( rect.x > 2 ) ? 2 : 1;
2f024384 1234 int extra_y = ( rect.y > 2 ) ? 2 : 1;
a0948e27 1235
d4175745
VZ
1236 #if defined(__WXMOTIF__)
1237 extra_x *= 2;
1238 extra_y *= 2;
1239 #endif
2f024384 1240
cb105ad4
SN
1241 rect.SetLeft( wxMax(0, rect.x - extra_x) );
1242 rect.SetTop( wxMax(0, rect.y - extra_y) );
2f024384
DS
1243 rect.SetRight( rect.GetRight() + 2 * extra_x );
1244 rect.SetBottom( rect.GetBottom() + 2 * extra_y );
d4175745 1245#endif
99306db2
VZ
1246
1247 wxGridCellEditor::SetSize(rect);
1248}
1249
3da93aae 1250void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
2796cce3 1251{
2f024384 1252 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
2796cce3
RD
1253
1254 m_startValue = grid->GetTable()->GetValue(row, col);
816be743
VZ
1255
1256 DoBeginEdit(m_startValue);
1257}
1258
1259void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
1260{
1261 Text()->SetValue(startValue);
b54ba671 1262 Text()->SetInsertionPointEnd();
2f024384 1263 Text()->SetSelection(-1, -1);
b54ba671 1264 Text()->SetFocus();
2796cce3
RD
1265}
1266
ccdee36f 1267bool wxGridCellTextEditor::EndEdit(int row, int col, wxGrid* grid)
2796cce3 1268{
2f024384 1269 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
2796cce3 1270
ca65c044 1271 bool changed = false;
b54ba671 1272 wxString value = Text()->GetValue();
2796cce3 1273 if (value != m_startValue)
ca65c044 1274 changed = true;
2796cce3
RD
1275
1276 if (changed)
1277 grid->GetTable()->SetValue(row, col, value);
2c9a89e0 1278
3da93aae 1279 m_startValue = wxEmptyString;
a9339fe2 1280
7b519e5e
JS
1281 // No point in setting the text of the hidden control
1282 //Text()->SetValue(m_startValue);
2796cce3
RD
1283
1284 return changed;
1285}
1286
2796cce3
RD
1287void wxGridCellTextEditor::Reset()
1288{
2f024384 1289 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
2796cce3 1290
816be743
VZ
1291 DoReset(m_startValue);
1292}
1293
1294void wxGridCellTextEditor::DoReset(const wxString& startValue)
1295{
1296 Text()->SetValue(startValue);
b54ba671 1297 Text()->SetInsertionPointEnd();
2796cce3
RD
1298}
1299
f6bcfd97
BP
1300bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
1301{
63e2147c 1302 return wxGridCellEditor::IsAcceptedKey(event);
f6bcfd97
BP
1303}
1304
2c9a89e0
RD
1305void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
1306{
63e2147c
RD
1307 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1308 // longer an appropriate way to get the character into the text control.
1309 // Do it ourselves instead. We know that if we get this far that we have
1310 // a valid character, so not a whole lot of testing needs to be done.
1311
1312 wxTextCtrl* tc = Text();
1313 wxChar ch;
1314 long pos;
902725ee 1315
63e2147c
RD
1316#if wxUSE_UNICODE
1317 ch = event.GetUnicodeKey();
1318 if (ch <= 127)
6f0d2cee 1319 ch = (wxChar)event.GetKeyCode();
63e2147c 1320#else
6f0d2cee 1321 ch = (wxChar)event.GetKeyCode();
63e2147c 1322#endif
2f024384 1323
63e2147c 1324 switch (ch)
f6bcfd97 1325 {
63e2147c
RD
1326 case WXK_DELETE:
1327 // delete the character at the cursor
1328 pos = tc->GetInsertionPoint();
1329 if (pos < tc->GetLastPosition())
2f024384 1330 tc->Remove(pos, pos + 1);
63e2147c
RD
1331 break;
1332
1333 case WXK_BACK:
1334 // delete the character before the cursor
1335 pos = tc->GetInsertionPoint();
1336 if (pos > 0)
2f024384 1337 tc->Remove(pos - 1, pos);
63e2147c
RD
1338 break;
1339
1340 default:
1341 tc->WriteText(ch);
1342 break;
f6bcfd97 1343 }
b54ba671 1344}
2c9a89e0 1345
c78b3acd 1346void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
0b7e6e7d 1347 WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
2796cce3
RD
1348{
1349#if defined(__WXMOTIF__) || defined(__WXGTK__)
1350 // wxMotif needs a little extra help...
6fc0f38f 1351 size_t pos = (size_t)( Text()->GetInsertionPoint() );
b54ba671 1352 wxString s( Text()->GetValue() );
8dd8f875 1353 s = s.Left(pos) + wxT("\n") + s.Mid(pos);
b54ba671
VZ
1354 Text()->SetValue(s);
1355 Text()->SetInsertionPoint( pos );
2796cce3
RD
1356#else
1357 // the other ports can handle a Return key press
1358 //
1359 event.Skip();
1360#endif
1361}
1362
c4608a8a
VZ
1363void wxGridCellTextEditor::SetParameters(const wxString& params)
1364{
1365 if ( !params )
1366 {
1367 // reset to default
1368 m_maxChars = 0;
1369 }
1370 else
1371 {
1372 long tmp;
c2f5b920 1373 if ( params.ToLong(&tmp) )
c4608a8a 1374 {
c2f5b920 1375 m_maxChars = (size_t)tmp;
c4608a8a
VZ
1376 }
1377 else
1378 {
c2f5b920 1379 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str() );
c4608a8a
VZ
1380 }
1381 }
1382}
1383
73145b0e
JS
1384// return the value in the text control
1385wxString wxGridCellTextEditor::GetValue() const
1386{
2f024384 1387 return Text()->GetValue();
73145b0e
JS
1388}
1389
816be743
VZ
1390// ----------------------------------------------------------------------------
1391// wxGridCellNumberEditor
1392// ----------------------------------------------------------------------------
1393
1394wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
1395{
1396 m_min = min;
1397 m_max = max;
1398}
1399
1400void wxGridCellNumberEditor::Create(wxWindow* parent,
1401 wxWindowID id,
1402 wxEvtHandler* evtHandler)
1403{
0e871ad0 1404#if wxUSE_SPINCTRL
816be743
VZ
1405 if ( HasRange() )
1406 {
1407 // create a spin ctrl
ca65c044 1408 m_control = new wxSpinCtrl(parent, wxID_ANY, wxEmptyString,
816be743
VZ
1409 wxDefaultPosition, wxDefaultSize,
1410 wxSP_ARROW_KEYS,
1411 m_min, m_max);
1412
1413 wxGridCellEditor::Create(parent, id, evtHandler);
1414 }
1415 else
0e871ad0 1416#endif
816be743
VZ
1417 {
1418 // just a text control
1419 wxGridCellTextEditor::Create(parent, id, evtHandler);
1420
1421#if wxUSE_VALIDATORS
85bc0351 1422 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
c2f5b920 1423#endif
816be743
VZ
1424 }
1425}
1426
1427void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
1428{
1429 // first get the value
1430 wxGridTableBase *table = grid->GetTable();
1431 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1432 {
1433 m_valueOld = table->GetValueAsLong(row, col);
1434 }
1435 else
1436 {
8a60ff0a 1437 m_valueOld = 0;
a5777624 1438 wxString sValue = table->GetValue(row, col);
0e871ad0 1439 if (! sValue.ToLong(&m_valueOld) && ! sValue.empty())
a5777624
RD
1440 {
1441 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1442 return;
1443 }
816be743
VZ
1444 }
1445
0e871ad0 1446#if wxUSE_SPINCTRL
816be743
VZ
1447 if ( HasRange() )
1448 {
4a64bee4 1449 Spin()->SetValue((int)m_valueOld);
f6bcfd97 1450 Spin()->SetFocus();
816be743
VZ
1451 }
1452 else
0e871ad0 1453#endif
816be743
VZ
1454 {
1455 DoBeginEdit(GetString());
1456 }
1457}
1458
3324d5f5 1459bool wxGridCellNumberEditor::EndEdit(int row, int col,
816be743
VZ
1460 wxGrid* grid)
1461{
8a60ff0a
RD
1462 long value = 0;
1463 wxString text;
816be743 1464
0e871ad0 1465#if wxUSE_SPINCTRL
816be743
VZ
1466 if ( HasRange() )
1467 {
1468 value = Spin()->GetValue();
8a360869
VZ
1469 if ( value == m_valueOld )
1470 return false;
1471
1472 text.Printf(wxT("%ld"), value);
816be743 1473 }
8a360869
VZ
1474 else // using unconstrained input
1475#endif // wxUSE_SPINCTRL
816be743 1476 {
8a360869 1477 const wxString textOld(grid->GetCellValue(row, col));
8a60ff0a 1478 text = Text()->GetValue();
8a360869
VZ
1479 if ( text.empty() )
1480 {
1481 if ( textOld.empty() )
1482 return false;
1483 }
1484 else // non-empty text now (maybe 0)
1485 {
1486 if ( !text.ToLong(&value) )
1487 return false;
816be743 1488
8a360869
VZ
1489 // if value == m_valueOld == 0 but old text was "" and new one is
1490 // "0" something still did change
1491 if ( value == m_valueOld && (value || !textOld.empty()) )
1492 return false;
1493 }
816be743
VZ
1494 }
1495
8a360869
VZ
1496 wxGridTableBase * const table = grid->GetTable();
1497 if ( table->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1498 table->SetValueAsLong(row, col, value);
1499 else
1500 table->SetValue(row, col, text);
1501
1502 return true;
816be743
VZ
1503}
1504
1505void wxGridCellNumberEditor::Reset()
1506{
0e871ad0 1507#if wxUSE_SPINCTRL
816be743
VZ
1508 if ( HasRange() )
1509 {
4a64bee4 1510 Spin()->SetValue((int)m_valueOld);
816be743
VZ
1511 }
1512 else
0e871ad0 1513#endif
816be743
VZ
1514 {
1515 DoReset(GetString());
1516 }
1517}
1518
f6bcfd97
BP
1519bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event)
1520{
1521 if ( wxGridCellEditor::IsAcceptedKey(event) )
1522 {
1523 int keycode = event.GetKeyCode();
63e2147c
RD
1524 if ( (keycode < 128) &&
1525 (wxIsdigit(keycode) || keycode == '+' || keycode == '-'))
f6bcfd97 1526 {
63e2147c 1527 return true;
f6bcfd97
BP
1528 }
1529 }
1530
ca65c044 1531 return false;
f6bcfd97
BP
1532}
1533
816be743
VZ
1534void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
1535{
eb5e42b6 1536 int keycode = event.GetKeyCode();
816be743
VZ
1537 if ( !HasRange() )
1538 {
63e2147c 1539 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-')
816be743
VZ
1540 {
1541 wxGridCellTextEditor::StartingKey(event);
1542
1543 // skip Skip() below
1544 return;
1545 }
1546 }
eb5e42b6
RD
1547#if wxUSE_SPINCTRL
1548 else
1549 {
1550 if ( wxIsdigit(keycode) )
1551 {
1552 wxSpinCtrl* spin = (wxSpinCtrl*)m_control;
1553 spin->SetValue(keycode - '0');
1554 spin->SetSelection(1,1);
1555 return;
1556 }
1557 }
1558#endif
2f024384 1559
816be743
VZ
1560 event.Skip();
1561}
9c4ba614 1562
c4608a8a
VZ
1563void wxGridCellNumberEditor::SetParameters(const wxString& params)
1564{
1565 if ( !params )
1566 {
1567 // reset to default
1568 m_min =
1569 m_max = -1;
1570 }
1571 else
1572 {
1573 long tmp;
1574 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
1575 {
1576 m_min = (int)tmp;
1577
1578 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
1579 {
1580 m_max = (int)tmp;
1581
1582 // skip the error message below
1583 return;
1584 }
1585 }
1586
f6bcfd97 1587 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str());
c4608a8a
VZ
1588 }
1589}
1590
73145b0e
JS
1591// return the value in the spin control if it is there (the text control otherwise)
1592wxString wxGridCellNumberEditor::GetValue() const
1593{
0e871ad0
WS
1594 wxString s;
1595
1596#if wxUSE_SPINCTRL
4db6714b 1597 if ( HasRange() )
0e871ad0
WS
1598 {
1599 long value = Spin()->GetValue();
1600 s.Printf(wxT("%ld"), value);
1601 }
1602 else
1603#endif
1604 {
1605 s = Text()->GetValue();
1606 }
1607
1608 return s;
73145b0e
JS
1609}
1610
816be743
VZ
1611// ----------------------------------------------------------------------------
1612// wxGridCellFloatEditor
1613// ----------------------------------------------------------------------------
1614
f6bcfd97
BP
1615wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
1616{
1617 m_width = width;
1618 m_precision = precision;
1619}
1620
816be743
VZ
1621void wxGridCellFloatEditor::Create(wxWindow* parent,
1622 wxWindowID id,
1623 wxEvtHandler* evtHandler)
1624{
1625 wxGridCellTextEditor::Create(parent, id, evtHandler);
1626
1627#if wxUSE_VALIDATORS
85bc0351 1628 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
c2f5b920 1629#endif
816be743
VZ
1630}
1631
1632void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
1633{
1634 // first get the value
d035e423 1635 wxGridTableBase * const table = grid->GetTable();
816be743
VZ
1636 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1637 {
1638 m_valueOld = table->GetValueAsDouble(row, col);
1639 }
1640 else
1641 {
8a60ff0a 1642 m_valueOld = 0.0;
d035e423
VZ
1643
1644 const wxString value = table->GetValue(row, col);
1645 if ( !value.empty() )
a5777624 1646 {
d035e423
VZ
1647 if ( !value.ToDouble(&m_valueOld) )
1648 {
1649 wxFAIL_MSG( _T("this cell doesn't have float value") );
1650 return;
1651 }
a5777624 1652 }
816be743
VZ
1653 }
1654
1655 DoBeginEdit(GetString());
1656}
1657
d035e423 1658bool wxGridCellFloatEditor::EndEdit(int row, int col, wxGrid* grid)
816be743 1659{
d035e423
VZ
1660 const wxString text(Text()->GetValue()),
1661 textOld(grid->GetCellValue(row, col));
8a60ff0a 1662
d035e423
VZ
1663 double value;
1664 if ( !text.empty() )
816be743 1665 {
d035e423
VZ
1666 if ( !text.ToDouble(&value) )
1667 return false;
1668 }
1669 else // new value is empty string
1670 {
1671 if ( textOld.empty() )
1672 return false; // nothing changed
816be743 1673
d035e423 1674 value = 0.;
816be743 1675 }
2f024384 1676
d035e423
VZ
1677 // the test for empty strings ensures that we don't skip the value setting
1678 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1679 if ( wxIsSameDouble(value, m_valueOld) && !text.empty() && !textOld.empty() )
1680 return false; // nothing changed
1681
1682 wxGridTableBase * const table = grid->GetTable();
1683
1684 if ( table->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1685 table->SetValueAsDouble(row, col, value);
1686 else
1687 table->SetValue(row, col, text);
1688
1689 return true;
816be743
VZ
1690}
1691
1692void wxGridCellFloatEditor::Reset()
1693{
1694 DoReset(GetString());
1695}
1696
1697void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
1698{
12a3f227 1699 int keycode = event.GetKeyCode();
3fe73755
SN
1700 char tmpbuf[2];
1701 tmpbuf[0] = (char) keycode;
1702 tmpbuf[1] = '\0';
42841dfc 1703 wxString strbuf(tmpbuf, *wxConvCurrent);
2f024384 1704
902725ee 1705#if wxUSE_INTL
42841dfc 1706 bool is_decimal_point = ( strbuf ==
63e2147c 1707 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) );
5335e9c4
MW
1708#else
1709 bool is_decimal_point = ( strbuf == _T(".") );
1710#endif
2f024384 1711
63e2147c
RD
1712 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-'
1713 || is_decimal_point )
816be743
VZ
1714 {
1715 wxGridCellTextEditor::StartingKey(event);
1716
1717 // skip Skip() below
1718 return;
1719 }
1720
1721 event.Skip();
1722}
1723
f6bcfd97
BP
1724void wxGridCellFloatEditor::SetParameters(const wxString& params)
1725{
1726 if ( !params )
1727 {
1728 // reset to default
1729 m_width =
1730 m_precision = -1;
1731 }
1732 else
1733 {
1734 long tmp;
1735 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
1736 {
1737 m_width = (int)tmp;
1738
1739 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
1740 {
1741 m_precision = (int)tmp;
1742
1743 // skip the error message below
1744 return;
1745 }
1746 }
1747
1748 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str());
1749 }
1750}
1751
1752wxString wxGridCellFloatEditor::GetString() const
1753{
1754 wxString fmt;
fe4cb4f5 1755 if ( m_precision == -1 && m_width != -1)
f6bcfd97
BP
1756 {
1757 // default precision
ec53826c 1758 fmt.Printf(_T("%%%d.f"), m_width);
f6bcfd97 1759 }
fe4cb4f5
JS
1760 else if ( m_precision != -1 && m_width == -1)
1761 {
1762 // default width
1763 fmt.Printf(_T("%%.%df"), m_precision);
1764 }
1765 else if ( m_precision != -1 && m_width != -1 )
f6bcfd97 1766 {
ec53826c 1767 fmt.Printf(_T("%%%d.%df"), m_width, m_precision);
f6bcfd97 1768 }
fe4cb4f5
JS
1769 else
1770 {
1771 // default width/precision
1772 fmt = _T("%f");
1773 }
f6bcfd97
BP
1774
1775 return wxString::Format(fmt, m_valueOld);
1776}
1777
1778bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
1779{
1780 if ( wxGridCellEditor::IsAcceptedKey(event) )
1781 {
7b34da9b
VZ
1782 const int keycode = event.GetKeyCode();
1783 if ( isascii(keycode) )
1784 {
1785 char tmpbuf[2];
1786 tmpbuf[0] = (char) keycode;
1787 tmpbuf[1] = '\0';
1788 wxString strbuf(tmpbuf, *wxConvCurrent);
2f024384 1789
902725ee 1790#if wxUSE_INTL
7b34da9b
VZ
1791 const wxString decimalPoint =
1792 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
5335e9c4 1793#else
7b34da9b 1794 const wxString decimalPoint(_T('.'));
5335e9c4 1795#endif
2f024384 1796
7b34da9b
VZ
1797 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1798 if ( wxIsdigit(keycode) ||
1799 tolower(keycode) == 'e' ||
1800 keycode == decimalPoint ||
1801 keycode == '+' ||
1802 keycode == '-' )
1803 {
1804 return true;
1805 }
2f024384 1806 }
f6bcfd97
BP
1807 }
1808
ca65c044 1809 return false;
f6bcfd97
BP
1810}
1811
3a8c693a
VZ
1812#endif // wxUSE_TEXTCTRL
1813
1814#if wxUSE_CHECKBOX
1815
508011ce
VZ
1816// ----------------------------------------------------------------------------
1817// wxGridCellBoolEditor
1818// ----------------------------------------------------------------------------
1819
9c71a138 1820// the default values for GetValue()
dab61ed7 1821wxString wxGridCellBoolEditor::ms_stringValues[2] = { _T(""), _T("1") };
9c71a138 1822
508011ce
VZ
1823void wxGridCellBoolEditor::Create(wxWindow* parent,
1824 wxWindowID id,
1825 wxEvtHandler* evtHandler)
1826{
1827 m_control = new wxCheckBox(parent, id, wxEmptyString,
1828 wxDefaultPosition, wxDefaultSize,
1829 wxNO_BORDER);
1830
1831 wxGridCellEditor::Create(parent, id, evtHandler);
1832}
1833
1834void wxGridCellBoolEditor::SetSize(const wxRect& r)
1835{
ca65c044 1836 bool resize = false;
b94ae1ea
VZ
1837 wxSize size = m_control->GetSize();
1838 wxCoord minSize = wxMin(r.width, r.height);
1839
1840 // check if the checkbox is not too big/small for this cell
1841 wxSize sizeBest = m_control->GetBestSize();
1842 if ( !(size == sizeBest) )
1843 {
1844 // reset to default size if it had been made smaller
1845 size = sizeBest;
1846
ca65c044 1847 resize = true;
b94ae1ea
VZ
1848 }
1849
1850 if ( size.x >= minSize || size.y >= minSize )
1851 {
1852 // leave 1 pixel margin
1853 size.x = size.y = minSize - 2;
1854
ca65c044 1855 resize = true;
b94ae1ea
VZ
1856 }
1857
1858 if ( resize )
1859 {
1860 m_control->SetSize(size);
1861 }
1862
508011ce 1863 // position it in the centre of the rectangle (TODO: support alignment?)
508011ce 1864
b94ae1ea 1865#if defined(__WXGTK__) || defined (__WXMOTIF__)
508011ce
VZ
1866 // the checkbox without label still has some space to the right in wxGTK,
1867 // so shift it to the right
b94ae1ea
VZ
1868 size.x -= 8;
1869#elif defined(__WXMSW__)
a95e38c0
VZ
1870 // here too, but in other way
1871 size.x += 1;
b94ae1ea
VZ
1872 size.y -= 2;
1873#endif
508011ce 1874
1bd71df9
JS
1875 int hAlign = wxALIGN_CENTRE;
1876 int vAlign = wxALIGN_CENTRE;
1877 if (GetCellAttr())
1878 GetCellAttr()->GetAlignment(& hAlign, & vAlign);
52d6f640 1879
1bd71df9
JS
1880 int x = 0, y = 0;
1881 if (hAlign == wxALIGN_LEFT)
1882 {
1883 x = r.x + 2;
2f024384 1884
1bd71df9
JS
1885#ifdef __WXMSW__
1886 x += 2;
52d6f640 1887#endif
2f024384
DS
1888
1889 y = r.y + r.height / 2 - size.y / 2;
1bd71df9
JS
1890 }
1891 else if (hAlign == wxALIGN_RIGHT)
1892 {
1893 x = r.x + r.width - size.x - 2;
2f024384 1894 y = r.y + r.height / 2 - size.y / 2;
1bd71df9
JS
1895 }
1896 else if (hAlign == wxALIGN_CENTRE)
1897 {
2f024384
DS
1898 x = r.x + r.width / 2 - size.x / 2;
1899 y = r.y + r.height / 2 - size.y / 2;
1bd71df9 1900 }
52d6f640 1901
1bd71df9 1902 m_control->Move(x, y);
508011ce
VZ
1903}
1904
1905void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
1906{
99306db2
VZ
1907 m_control->Show(show);
1908
189d0213 1909 if ( show )
508011ce 1910 {
189d0213
VZ
1911 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
1912 CBox()->SetBackgroundColour(colBg);
508011ce 1913 }
508011ce
VZ
1914}
1915
1916void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
1917{
1918 wxASSERT_MSG(m_control,
c2f5b920 1919 wxT("The wxGridCellEditor must be created first!"));
508011ce 1920
28a77bc4 1921 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
2f024384 1922 {
f2d76237 1923 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
2f024384 1924 }
f2d76237 1925 else
695a3263
MB
1926 {
1927 wxString cellval( grid->GetTable()->GetValue(row, col) );
8497bbf5
VZ
1928
1929 if ( cellval == ms_stringValues[false] )
1930 m_startValue = false;
1931 else if ( cellval == ms_stringValues[true] )
1932 m_startValue = true;
1933 else
1934 {
1935 // do not try to be smart here and convert it to true or false
1936 // because we'll still overwrite it with something different and
1937 // this risks to be very surprising for the user code, let them
1938 // know about it
1939 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1940 }
695a3263 1941 }
2f024384 1942
508011ce
VZ
1943 CBox()->SetValue(m_startValue);
1944 CBox()->SetFocus();
1945}
1946
1947bool wxGridCellBoolEditor::EndEdit(int row, int col,
508011ce
VZ
1948 wxGrid* grid)
1949{
1950 wxASSERT_MSG(m_control,
c2f5b920 1951 wxT("The wxGridCellEditor must be created first!"));
508011ce 1952
ca65c044 1953 bool changed = false;
508011ce
VZ
1954 bool value = CBox()->GetValue();
1955 if ( value != m_startValue )
ca65c044 1956 changed = true;
508011ce
VZ
1957
1958 if ( changed )
1959 {
9c71a138
VZ
1960 wxGridTableBase * const table = grid->GetTable();
1961 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
1962 table->SetValueAsBool(row, col, value);
f2d76237 1963 else
9c71a138 1964 table->SetValue(row, col, GetValue());
508011ce
VZ
1965 }
1966
1967 return changed;
1968}
1969
1970void wxGridCellBoolEditor::Reset()
1971{
1972 wxASSERT_MSG(m_control,
c2f5b920 1973 wxT("The wxGridCellEditor must be created first!"));
508011ce
VZ
1974
1975 CBox()->SetValue(m_startValue);
1976}
1977
e195a54c 1978void wxGridCellBoolEditor::StartingClick()
508011ce 1979{
e195a54c 1980 CBox()->SetValue(!CBox()->GetValue());
508011ce
VZ
1981}
1982
f6bcfd97
BP
1983bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event)
1984{
1985 if ( wxGridCellEditor::IsAcceptedKey(event) )
1986 {
1987 int keycode = event.GetKeyCode();
1988 switch ( keycode )
1989 {
f6bcfd97
BP
1990 case WXK_SPACE:
1991 case '+':
1992 case '-':
ca65c044 1993 return true;
f6bcfd97
BP
1994 }
1995 }
1996
ca65c044 1997 return false;
f6bcfd97 1998}
04418332 1999
63e2147c
RD
2000void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event)
2001{
2002 int keycode = event.GetKeyCode();
2003 switch ( keycode )
2004 {
2005 case WXK_SPACE:
2006 CBox()->SetValue(!CBox()->GetValue());
2007 break;
902725ee 2008
63e2147c
RD
2009 case '+':
2010 CBox()->SetValue(true);
2011 break;
902725ee 2012
63e2147c
RD
2013 case '-':
2014 CBox()->SetValue(false);
2015 break;
2016 }
2017}
2018
73145b0e
JS
2019wxString wxGridCellBoolEditor::GetValue() const
2020{
9c71a138
VZ
2021 return ms_stringValues[CBox()->GetValue()];
2022}
2023
2024/* static */ void
2025wxGridCellBoolEditor::UseStringValues(const wxString& valueTrue,
2026 const wxString& valueFalse)
2027{
2028 ms_stringValues[false] = valueFalse;
2029 ms_stringValues[true] = valueTrue;
2030}
2031
2032/* static */ bool
2033wxGridCellBoolEditor::IsTrueValue(const wxString& value)
2034{
2035 return value == ms_stringValues[true];
73145b0e 2036}
f6bcfd97 2037
3a8c693a
VZ
2038#endif // wxUSE_CHECKBOX
2039
2040#if wxUSE_COMBOBOX
2041
4ee5fc9c
VZ
2042// ----------------------------------------------------------------------------
2043// wxGridCellChoiceEditor
2044// ----------------------------------------------------------------------------
2045
7db33cc3
MB
2046wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices,
2047 bool allowOthers)
2048 : m_choices(choices),
2049 m_allowOthers(allowOthers) { }
2050
4ee5fc9c 2051wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
f6bcfd97 2052 const wxString choices[],
4ee5fc9c
VZ
2053 bool allowOthers)
2054 : m_allowOthers(allowOthers)
2055{
c4608a8a 2056 if ( count )
4ee5fc9c 2057 {
c4608a8a
VZ
2058 m_choices.Alloc(count);
2059 for ( size_t n = 0; n < count; n++ )
2060 {
2061 m_choices.Add(choices[n]);
2062 }
4ee5fc9c
VZ
2063 }
2064}
2065
c4608a8a
VZ
2066wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
2067{
2068 wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
2069 editor->m_allowOthers = m_allowOthers;
2070 editor->m_choices = m_choices;
2071
2072 return editor;
2073}
2074
4ee5fc9c
VZ
2075void wxGridCellChoiceEditor::Create(wxWindow* parent,
2076 wxWindowID id,
2077 wxEvtHandler* evtHandler)
2078{
7999b830
VZ
2079 int style = wxTE_PROCESS_ENTER |
2080 wxTE_PROCESS_TAB |
2081 wxBORDER_NONE;
2082
2083 if ( !m_allowOthers )
2f26ad28 2084 style |= wxCB_READONLY;
4ee5fc9c
VZ
2085 m_control = new wxComboBox(parent, id, wxEmptyString,
2086 wxDefaultPosition, wxDefaultSize,
7999b830
VZ
2087 m_choices,
2088 style);
4ee5fc9c 2089
4ee5fc9c
VZ
2090 wxGridCellEditor::Create(parent, id, evtHandler);
2091}
2092
a5777624
RD
2093void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
2094 wxGridCellAttr * attr)
4ee5fc9c
VZ
2095{
2096 // as we fill the entire client area, don't do anything here to minimize
2097 // flicker
a5777624
RD
2098
2099 // TODO: It doesn't actually fill the client area since the height of a
c2f5b920
DS
2100 // combo always defaults to the standard. Until someone has time to
2101 // figure out the right rectangle to paint, just do it the normal way.
a5777624 2102 wxGridCellEditor::PaintBackground(rectCell, attr);
4ee5fc9c
VZ
2103}
2104
2105void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
2106{
2107 wxASSERT_MSG(m_control,
c2f5b920 2108 wxT("The wxGridCellEditor must be created first!"));
4ee5fc9c 2109
08dd04d0
JS
2110 wxGridCellEditorEvtHandler* evtHandler = NULL;
2111 if (m_control)
2112 evtHandler = wxDynamicCast(m_control->GetEventHandler(), wxGridCellEditorEvtHandler);
2113
2114 // Don't immediately end if we get a kill focus event within BeginEdit
2115 if (evtHandler)
2116 evtHandler->SetInSetFocus(true);
2117
4ee5fc9c
VZ
2118 m_startValue = grid->GetTable()->GetValue(row, col);
2119
b6484591 2120 Reset(); // this updates combo box to correspond to m_startValue
2f024384 2121
4ee5fc9c 2122 Combo()->SetFocus();
08dd04d0
JS
2123
2124 if (evtHandler)
46cbb21e
JS
2125 {
2126 // When dropping down the menu, a kill focus event
2127 // happens after this point, so we can't reset the flag yet.
2128#if !defined(__WXGTK20__)
08dd04d0 2129 evtHandler->SetInSetFocus(false);
46cbb21e
JS
2130#endif
2131 }
4ee5fc9c
VZ
2132}
2133
28a77bc4 2134bool wxGridCellChoiceEditor::EndEdit(int row, int col,
4ee5fc9c
VZ
2135 wxGrid* grid)
2136{
2137 wxString value = Combo()->GetValue();
faffacec
VZ
2138 if ( value == m_startValue )
2139 return false;
4ee5fc9c 2140
faffacec 2141 grid->GetTable()->SetValue(row, col, value);
4ee5fc9c 2142
faffacec 2143 return true;
4ee5fc9c
VZ
2144}
2145
2146void wxGridCellChoiceEditor::Reset()
2147{
b6484591
VZ
2148 if (m_allowOthers)
2149 {
2150 Combo()->SetValue(m_startValue);
2151 Combo()->SetInsertionPointEnd();
2152 }
2153 else // the combobox is read-only
2154 {
2155 // find the right position, or default to the first if not found
2156 int pos = Combo()->FindString(m_startValue);
2157 if (pos == wxNOT_FOUND)
2158 pos = 0;
2159 Combo()->SetSelection(pos);
2160 }
4ee5fc9c
VZ
2161}
2162
c4608a8a
VZ
2163void wxGridCellChoiceEditor::SetParameters(const wxString& params)
2164{
2165 if ( !params )
2166 {
2167 // what can we do?
2168 return;
2169 }
2170
2171 m_choices.Empty();
2172
2173 wxStringTokenizer tk(params, _T(','));
2174 while ( tk.HasMoreTokens() )
2175 {
2176 m_choices.Add(tk.GetNextToken());
2177 }
2178}
2179
73145b0e
JS
2180// return the value in the text control
2181wxString wxGridCellChoiceEditor::GetValue() const
2182{
2183 return Combo()->GetValue();
2184}
04418332 2185
3a8c693a
VZ
2186#endif // wxUSE_COMBOBOX
2187
508011ce
VZ
2188// ----------------------------------------------------------------------------
2189// wxGridCellEditorEvtHandler
2190// ----------------------------------------------------------------------------
2796cce3 2191
140954fd
VZ
2192void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent& event)
2193{
08dd04d0
JS
2194 // Don't disable the cell if we're just starting to edit it
2195 if (m_inSetFocus)
2196 return;
2197
140954fd
VZ
2198 // accept changes
2199 m_grid->DisableCellEditControl();
2200
2201 event.Skip();
2202}
2203
2796cce3
RD
2204void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
2205{
12a3f227 2206 switch ( event.GetKeyCode() )
2796cce3
RD
2207 {
2208 case WXK_ESCAPE:
2209 m_editor->Reset();
b54ba671 2210 m_grid->DisableCellEditControl();
2796cce3
RD
2211 break;
2212
2c9a89e0 2213 case WXK_TAB:
b51c3f27 2214 m_grid->GetEventHandler()->ProcessEvent( event );
9b4aede2
RD
2215 break;
2216
2796cce3 2217 case WXK_RETURN:
faec5a43
SN
2218 case WXK_NUMPAD_ENTER:
2219 if (!m_grid->GetEventHandler()->ProcessEvent(event))
2796cce3
RD
2220 m_editor->HandleReturn(event);
2221 break;
2222
2796cce3
RD
2223 default:
2224 event.Skip();
2f024384 2225 break;
2796cce3
RD
2226 }
2227}
2228
fb0de762
RD
2229void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
2230{
7db713ae
JS
2231 int row = m_grid->GetGridCursorRow();
2232 int col = m_grid->GetGridCursorCol();
2233 wxRect rect = m_grid->CellToRect( row, col );
2234 int cw, ch;
2235 m_grid->GetGridWindow()->GetClientSize( &cw, &ch );
2f024384 2236
7db713ae
JS
2237 // if cell width is smaller than grid client area, cell is wholly visible
2238 bool wholeCellVisible = (rect.GetWidth() < cw);
2239
12a3f227 2240 switch ( event.GetKeyCode() )
fb0de762
RD
2241 {
2242 case WXK_ESCAPE:
2243 case WXK_TAB:
2244 case WXK_RETURN:
a4f7bf58 2245 case WXK_NUMPAD_ENTER:
fb0de762
RD
2246 break;
2247
7db713ae
JS
2248 case WXK_HOME:
2249 {
2f024384 2250 if ( wholeCellVisible )
7db713ae
JS
2251 {
2252 // no special processing needed...
2253 event.Skip();
2254 break;
2255 }
2256
2257 // do special processing for partly visible cell...
2258
2259 // get the widths of all cells previous to this one
2260 int colXPos = 0;
faa94f3e 2261 for ( int i = 0; i < col; i++ )
7db713ae
JS
2262 {
2263 colXPos += m_grid->GetColSize(i);
2264 }
2265
2266 int xUnit = 1, yUnit = 1;
2267 m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit);
2268 if (col != 0)
2269 {
2f024384 2270 m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL));
7db713ae
JS
2271 }
2272 else
2273 {
2f024384 2274 m_grid->Scroll(colXPos / xUnit, m_grid->GetScrollPos(wxVERTICAL));
7db713ae
JS
2275 }
2276 event.Skip();
2277 break;
2278 }
c2f5b920 2279
7db713ae
JS
2280 case WXK_END:
2281 {
2f024384 2282 if ( wholeCellVisible )
7db713ae
JS
2283 {
2284 // no special processing needed...
2285 event.Skip();
2286 break;
2287 }
2288
2289 // do special processing for partly visible cell...
2290
2291 int textWidth = 0;
2292 wxString value = m_grid->GetCellValue(row, col);
2293 if ( wxEmptyString != value )
2294 {
2295 // get width of cell CONTENTS (text)
2296 int y;
2297 wxFont font = m_grid->GetCellFont(row, col);
2298 m_grid->GetTextExtent(value, &textWidth, &y, NULL, NULL, &font);
2f024384 2299
7db713ae
JS
2300 // try to RIGHT align the text by scrolling
2301 int client_right = m_grid->GetGridWindow()->GetClientSize().GetWidth();
2f024384 2302
7db713ae
JS
2303 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2304 // otherwise the last part of the cell content might be hidden below the scroll bar
2305 // FIXME: maybe there is a more suitable correction?
2f024384 2306 textWidth -= (client_right - (m_grid->GetScrollLineX() * 2));
7db713ae
JS
2307 if ( textWidth < 0 )
2308 {
2309 textWidth = 0;
2310 }
2311 }
2312
2313 // get the widths of all cells previous to this one
2314 int colXPos = 0;
faa94f3e 2315 for ( int i = 0; i < col; i++ )
7db713ae
JS
2316 {
2317 colXPos += m_grid->GetColSize(i);
2318 }
2f024384 2319
7db713ae
JS
2320 // and add the (modified) text width of the cell contents
2321 // as we'd like to see the last part of the cell contents
2322 colXPos += textWidth;
2323
2324 int xUnit = 1, yUnit = 1;
2325 m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit);
ccdee36f 2326 m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL));
7db713ae
JS
2327 event.Skip();
2328 break;
2329 }
2330
fb0de762
RD
2331 default:
2332 event.Skip();
c2f5b920 2333 break;
fb0de762
RD
2334 }
2335}
2336
c4608a8a
VZ
2337// ----------------------------------------------------------------------------
2338// wxGridCellWorker is an (almost) empty common base class for
2339// wxGridCellRenderer and wxGridCellEditor managing ref counting
2340// ----------------------------------------------------------------------------
2341
2342void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
2343{
2344 // nothing to do
2345}
2346
2347wxGridCellWorker::~wxGridCellWorker()
2348{
2349}
2350
508011ce
VZ
2351// ============================================================================
2352// renderer classes
2353// ============================================================================
2354
ab79958a
VZ
2355// ----------------------------------------------------------------------------
2356// wxGridCellRenderer
2357// ----------------------------------------------------------------------------
2358
2359void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 2360 wxGridCellAttr& attr,
ab79958a
VZ
2361 wxDC& dc,
2362 const wxRect& rect,
c78b3acd 2363 int WXUNUSED(row), int WXUNUSED(col),
ab79958a
VZ
2364 bool isSelected)
2365{
04ee05f9 2366 dc.SetBackgroundMode( wxBRUSHSTYLE_SOLID );
ab79958a 2367
ff72f628 2368 wxColour clr;
4db6714b 2369 if ( grid.IsEnabled() )
ab79958a 2370 {
ec157c8f
WS
2371 if ( isSelected )
2372 {
760be3f7
VS
2373 if ( grid.HasFocus() )
2374 clr = grid.GetSelectionBackground();
2375 else
2376 clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW);
ec157c8f
WS
2377 }
2378 else
2379 {
ff72f628 2380 clr = attr.GetBackgroundColour();
ec157c8f 2381 }
52d6f640 2382 }
ff72f628 2383 else // grey out fields if the grid is disabled
ab79958a 2384 {
ff72f628 2385 clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
ab79958a
VZ
2386 }
2387
ff72f628 2388 dc.SetBrush(clr);
ab79958a 2389 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
2390 dc.DrawRectangle(rect);
2391}
2392
508011ce
VZ
2393// ----------------------------------------------------------------------------
2394// wxGridCellStringRenderer
2395// ----------------------------------------------------------------------------
2396
fbfb8bcc
VZ
2397void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid,
2398 const wxGridCellAttr& attr,
816be743
VZ
2399 wxDC& dc,
2400 bool isSelected)
ab79958a 2401{
04ee05f9 2402 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
ab79958a 2403
283b7808
VZ
2404 // TODO some special colours for attr.IsReadOnly() case?
2405
04418332 2406 // different coloured text when the grid is disabled
4db6714b 2407 if ( grid.IsEnabled() )
ab79958a 2408 {
c2f5b920
DS
2409 if ( isSelected )
2410 {
760be3f7
VS
2411 wxColour clr;
2412 if ( grid.HasFocus() )
2413 clr = grid.GetSelectionBackground();
2414 else
2415 clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW);
2416 dc.SetTextBackground( clr );
c2f5b920
DS
2417 dc.SetTextForeground( grid.GetSelectionForeground() );
2418 }
2419 else
2420 {
2421 dc.SetTextBackground( attr.GetBackgroundColour() );
2422 dc.SetTextForeground( attr.GetTextColour() );
2423 }
ab79958a
VZ
2424 }
2425 else
2426 {
c2f5b920
DS
2427 dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
2428 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
ab79958a 2429 }
816be743 2430
2796cce3 2431 dc.SetFont( attr.GetFont() );
816be743
VZ
2432}
2433
fbfb8bcc 2434wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr,
65e4e78e
VZ
2435 wxDC& dc,
2436 const wxString& text)
2437{
f6bcfd97 2438 wxCoord x = 0, y = 0, max_x = 0;
65e4e78e 2439 dc.SetFont(attr.GetFont());
f6bcfd97
BP
2440 wxStringTokenizer tk(text, _T('\n'));
2441 while ( tk.HasMoreTokens() )
2442 {
2443 dc.GetTextExtent(tk.GetNextToken(), &x, &y);
2444 max_x = wxMax(max_x, x);
2445 }
2446
2447 y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
65e4e78e 2448
f6bcfd97 2449 return wxSize(max_x, y);
65e4e78e
VZ
2450}
2451
2452wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
2453 wxGridCellAttr& attr,
2454 wxDC& dc,
2455 int row, int col)
2456{
2457 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
2458}
2459
816be743
VZ
2460void wxGridCellStringRenderer::Draw(wxGrid& grid,
2461 wxGridCellAttr& attr,
2462 wxDC& dc,
2463 const wxRect& rectCell,
2464 int row, int col,
2465 bool isSelected)
2466{
27f35b66 2467 wxRect rect = rectCell;
dc1f566f
SN
2468 rect.Inflate(-1);
2469
2470 // erase only this cells background, overflow cells should have been erased
2471 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
2472
2473 int hAlign, vAlign;
2474 attr.GetAlignment(&hAlign, &vAlign);
27f35b66 2475
39b80349
SN
2476 int overflowCols = 0;
2477
27f35b66
SN
2478 if (attr.GetOverflow())
2479 {
2480 int cols = grid.GetNumberCols();
2481 int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
2482 int cell_rows, cell_cols;
2f024384 2483 attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <= 0
27f35b66
SN
2484 if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
2485 {
2486 int i, c_cols, c_rows;
2487 for (i = col+cell_cols; i < cols; i++)
2488 {
ca65c044 2489 bool is_empty = true;
2f024384 2490 for (int j=row; j < row + cell_rows; j++)
27f35b66 2491 {
39b80349 2492 // check w/ anchor cell for multicell block
ef4d6ce8 2493 grid.GetCellSize(j, i, &c_rows, &c_cols);
2f024384
DS
2494 if (c_rows > 0)
2495 c_rows = 0;
2496 if (!grid.GetTable()->IsEmptyCell(j + c_rows, i))
39b80349 2497 {
ca65c044 2498 is_empty = false;
ef4d6ce8 2499 break;
39b80349 2500 }
27f35b66 2501 }
2f024384 2502
39b80349 2503 if (is_empty)
c2f5b920 2504 {
39b80349 2505 rect.width += grid.GetColSize(i);
c2f5b920 2506 }
dc1f566f
SN
2507 else
2508 {
2509 i--;
2510 break;
2511 }
2f024384 2512
4db6714b
KH
2513 if (rect.width >= best_width)
2514 break;
2b5f62a0 2515 }
2f024384 2516
39b80349 2517 overflowCols = i - col - cell_cols + 1;
4db6714b
KH
2518 if (overflowCols >= cols)
2519 overflowCols = cols - 1;
39b80349 2520 }
27f35b66 2521
dc1f566f
SN
2522 if (overflowCols > 0) // redraw overflow cells w/ proper hilight
2523 {
39b80349 2524 hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
2b5f62a0 2525 wxRect clip = rect;
39b80349 2526 clip.x += rectCell.width;
dc1f566f 2527 // draw each overflow cell individually
c2f5b920 2528 int col_end = col + cell_cols + overflowCols;
dc1f566f 2529 if (col_end >= grid.GetNumberCols())
2b5f62a0 2530 col_end = grid.GetNumberCols() - 1;
c2f5b920 2531 for (int i = col + cell_cols; i <= col_end; i++)
dc1f566f 2532 {
dc1f566f
SN
2533 clip.width = grid.GetColSize(i) - 1;
2534 dc.DestroyClippingRegion();
2535 dc.SetClippingRegion(clip);
39b80349
SN
2536
2537 SetTextColoursAndFont(grid, attr, dc,
2b5f62a0 2538 grid.IsInSelection(row,i));
39b80349 2539
dc1f566f 2540 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
2b5f62a0 2541 rect, hAlign, vAlign);
39b80349 2542 clip.x += grid.GetColSize(i) - 1;
dc1f566f 2543 }
ab79958a 2544
39b80349 2545 rect = rectCell;
2b5f62a0 2546 rect.Inflate(-1);
dc1f566f 2547 rect.width++;
39b80349 2548 dc.DestroyClippingRegion();
dc1f566f 2549 }
39b80349 2550 }
ab79958a 2551
dc1f566f
SN
2552 // now we only have to draw the text
2553 SetTextColoursAndFont(grid, attr, dc, isSelected);
39b80349 2554
ab79958a
VZ
2555 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
2556 rect, hAlign, vAlign);
2557}
2558
65e4e78e
VZ
2559// ----------------------------------------------------------------------------
2560// wxGridCellNumberRenderer
2561// ----------------------------------------------------------------------------
2562
fbfb8bcc 2563wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
2564{
2565 wxGridTableBase *table = grid.GetTable();
2566 wxString text;
2567 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
2568 {
2569 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
2570 }
9c4ba614
VZ
2571 else
2572 {
2573 text = table->GetValue(row, col);
2574 }
65e4e78e
VZ
2575
2576 return text;
2577}
2578
816be743
VZ
2579void wxGridCellNumberRenderer::Draw(wxGrid& grid,
2580 wxGridCellAttr& attr,
2581 wxDC& dc,
2582 const wxRect& rectCell,
2583 int row, int col,
2584 bool isSelected)
2585{
2586 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
2587
2588 SetTextColoursAndFont(grid, attr, dc, isSelected);
2589
2590 // draw the text right aligned by default
2591 int hAlign, vAlign;
2592 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 2593 hAlign = wxALIGN_RIGHT;
816be743
VZ
2594
2595 wxRect rect = rectCell;
2596 rect.Inflate(-1);
2597
65e4e78e
VZ
2598 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
2599}
816be743 2600
65e4e78e
VZ
2601wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
2602 wxGridCellAttr& attr,
2603 wxDC& dc,
2604 int row, int col)
2605{
2606 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
2607}
2608
2609// ----------------------------------------------------------------------------
2610// wxGridCellFloatRenderer
2611// ----------------------------------------------------------------------------
2612
2613wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
2614{
2615 SetWidth(width);
2616 SetPrecision(precision);
2617}
2618
e72b4213
VZ
2619wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
2620{
2621 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
2622 renderer->m_width = m_width;
2623 renderer->m_precision = m_precision;
2624 renderer->m_format = m_format;
2625
2626 return renderer;
2627}
2628
fbfb8bcc 2629wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
2630{
2631 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
2632
2633 bool hasDouble;
2634 double val;
65e4e78e
VZ
2635 wxString text;
2636 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
2637 {
0b190b0f 2638 val = table->GetValueAsDouble(row, col);
ca65c044 2639 hasDouble = true;
65e4e78e 2640 }
9c4ba614
VZ
2641 else
2642 {
2643 text = table->GetValue(row, col);
0b190b0f 2644 hasDouble = text.ToDouble(&val);
9c4ba614 2645 }
65e4e78e 2646
0b190b0f
VZ
2647 if ( hasDouble )
2648 {
2649 if ( !m_format )
2650 {
2651 if ( m_width == -1 )
2652 {
19d7140e
VZ
2653 if ( m_precision == -1 )
2654 {
2b5f62a0
VZ
2655 // default width/precision
2656 m_format = _T("%f");
2657 }
19d7140e
VZ
2658 else
2659 {
2660 m_format.Printf(_T("%%.%df"), m_precision);
2661 }
0b190b0f
VZ
2662 }
2663 else if ( m_precision == -1 )
2664 {
2665 // default precision
2666 m_format.Printf(_T("%%%d.f"), m_width);
2667 }
2668 else
2669 {
2670 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
2671 }
2672 }
2673
2674 text.Printf(m_format, val);
19d7140e 2675
0b190b0f
VZ
2676 }
2677 //else: text already contains the string
2678
65e4e78e
VZ
2679 return text;
2680}
2681
816be743
VZ
2682void wxGridCellFloatRenderer::Draw(wxGrid& grid,
2683 wxGridCellAttr& attr,
2684 wxDC& dc,
2685 const wxRect& rectCell,
2686 int row, int col,
2687 bool isSelected)
2688{
2689 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
2690
2691 SetTextColoursAndFont(grid, attr, dc, isSelected);
2692
2693 // draw the text right aligned by default
2694 int hAlign, vAlign;
2695 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 2696 hAlign = wxALIGN_RIGHT;
816be743
VZ
2697
2698 wxRect rect = rectCell;
2699 rect.Inflate(-1);
2700
65e4e78e
VZ
2701 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
2702}
816be743 2703
65e4e78e
VZ
2704wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
2705 wxGridCellAttr& attr,
2706 wxDC& dc,
2707 int row, int col)
2708{
2709 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
2710}
2711
0b190b0f
VZ
2712void wxGridCellFloatRenderer::SetParameters(const wxString& params)
2713{
0b190b0f
VZ
2714 if ( !params )
2715 {
2716 // reset to defaults
2717 SetWidth(-1);
2718 SetPrecision(-1);
2719 }
2720 else
2721 {
2722 wxString tmp = params.BeforeFirst(_T(','));
ec157c8f 2723 if ( !tmp.empty() )
0b190b0f
VZ
2724 {
2725 long width;
19d7140e 2726 if ( tmp.ToLong(&width) )
0b190b0f 2727 {
19d7140e 2728 SetWidth((int)width);
0b190b0f
VZ
2729 }
2730 else
2731 {
19d7140e
VZ
2732 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
2733 }
19d7140e 2734 }
2f024384 2735
7448de8d
WS
2736 tmp = params.AfterFirst(_T(','));
2737 if ( !tmp.empty() )
2738 {
2739 long precision;
19d7140e 2740 if ( tmp.ToLong(&precision) )
7448de8d 2741 {
19d7140e 2742 SetPrecision((int)precision);
7448de8d
WS
2743 }
2744 else
2745 {
19d7140e 2746 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
7448de8d 2747 }
0b190b0f
VZ
2748 }
2749 }
2750}
2751
508011ce
VZ
2752// ----------------------------------------------------------------------------
2753// wxGridCellBoolRenderer
2754// ----------------------------------------------------------------------------
2755
65e4e78e 2756wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 2757
b94ae1ea
VZ
2758// FIXME these checkbox size calculations are really ugly...
2759
65e4e78e 2760// between checkmark and box
a95e38c0 2761static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 2762
65e4e78e
VZ
2763wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
2764 wxGridCellAttr& WXUNUSED(attr),
2765 wxDC& WXUNUSED(dc),
2766 int WXUNUSED(row),
2767 int WXUNUSED(col))
2768{
2769 // compute it only once (no locks for MT safeness in GUI thread...)
2770 if ( !ms_sizeCheckMark.x )
297da4ba 2771 {
65e4e78e 2772 // get checkbox size
ca65c044 2773 wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString);
297da4ba 2774 wxSize size = checkbox->GetBestSize();
2f024384 2775 wxCoord checkSize = size.y + 2 * wxGRID_CHECKMARK_MARGIN;
297da4ba 2776
2f26ad28 2777#if defined(__WXMOTIF__)
65e4e78e 2778 checkSize -= size.y / 2;
297da4ba
VZ
2779#endif
2780
2781 delete checkbox;
65e4e78e
VZ
2782
2783 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
2784 }
2785
65e4e78e
VZ
2786 return ms_sizeCheckMark;
2787}
2788
2789void wxGridCellBoolRenderer::Draw(wxGrid& grid,
2790 wxGridCellAttr& attr,
2791 wxDC& dc,
2792 const wxRect& rect,
2793 int row, int col,
2794 bool isSelected)
2795{
2796 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
2797
297da4ba 2798 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 2799 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
2800
2801 // don't draw outside the cell
2802 wxCoord minSize = wxMin(rect.width, rect.height);
2803 if ( size.x >= minSize || size.y >= minSize )
2804 {
2805 // and even leave (at least) 1 pixel margin
2f26ad28 2806 size.x = size.y = minSize;
b94ae1ea
VZ
2807 }
2808
2809 // draw a border around checkmark
1bd71df9 2810 int vAlign, hAlign;
c2f5b920 2811 attr.GetAlignment(&hAlign, &vAlign);
52d6f640 2812
a95e38c0 2813 wxRect rectBorder;
1bd71df9
JS
2814 if (hAlign == wxALIGN_CENTRE)
2815 {
2f024384
DS
2816 rectBorder.x = rect.x + rect.width / 2 - size.x / 2;
2817 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9
JS
2818 rectBorder.width = size.x;
2819 rectBorder.height = size.y;
2820 }
2821 else if (hAlign == wxALIGN_LEFT)
2822 {
2823 rectBorder.x = rect.x + 2;
2f024384 2824 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9 2825 rectBorder.width = size.x;
52d6f640 2826 rectBorder.height = size.y;
1bd71df9
JS
2827 }
2828 else if (hAlign == wxALIGN_RIGHT)
2829 {
2830 rectBorder.x = rect.x + rect.width - size.x - 2;
2f024384 2831 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9 2832 rectBorder.width = size.x;
52d6f640 2833 rectBorder.height = size.y;
1bd71df9 2834 }
b94ae1ea 2835
f2d76237 2836 bool value;
b94ae1ea 2837 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
c2f5b920 2838 {
f2d76237 2839 value = grid.GetTable()->GetValueAsBool(row, col);
c2f5b920 2840 }
f2d76237 2841 else
695a3263
MB
2842 {
2843 wxString cellval( grid.GetTable()->GetValue(row, col) );
9c71a138 2844 value = wxGridCellBoolEditor::IsTrueValue(cellval);
695a3263 2845 }
f2d76237 2846
2f26ad28
RR
2847 int flags = 0;
2848 if (value)
76c66f19
VZ
2849 flags |= wxCONTROL_CHECKED;
2850
2f26ad28 2851 wxRendererNative::Get().DrawCheckBox( &grid, dc, rectBorder, flags );
508011ce
VZ
2852}
2853
2796cce3
RD
2854// ----------------------------------------------------------------------------
2855// wxGridCellAttr
2856// ----------------------------------------------------------------------------
2857
1df4050d
VZ
2858void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
2859{
2860 m_nRef = 1;
2861
2862 m_isReadOnly = Unset;
2863
2864 m_renderer = NULL;
2865 m_editor = NULL;
2866
2867 m_attrkind = wxGridCellAttr::Cell;
2868
27f35b66 2869 m_sizeRows = m_sizeCols = 1;
b63fce94 2870 m_overflow = UnsetOverflow;
27f35b66 2871
1df4050d
VZ
2872 SetDefAttr(attrDefault);
2873}
2874
39bcce60 2875wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246 2876{
1df4050d
VZ
2877 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
2878
a68c1246
VZ
2879 if ( HasTextColour() )
2880 attr->SetTextColour(GetTextColour());
2881 if ( HasBackgroundColour() )
2882 attr->SetBackgroundColour(GetBackgroundColour());
2883 if ( HasFont() )
2884 attr->SetFont(GetFont());
2885 if ( HasAlignment() )
2886 attr->SetAlignment(m_hAlign, m_vAlign);
2887
27f35b66
SN
2888 attr->SetSize( m_sizeRows, m_sizeCols );
2889
a68c1246
VZ
2890 if ( m_renderer )
2891 {
2892 attr->SetRenderer(m_renderer);
39bcce60 2893 m_renderer->IncRef();
a68c1246
VZ
2894 }
2895 if ( m_editor )
2896 {
2897 attr->SetEditor(m_editor);
39bcce60 2898 m_editor->IncRef();
a68c1246
VZ
2899 }
2900
2901 if ( IsReadOnly() )
2902 attr->SetReadOnly();
2903
dfd7c082 2904 attr->SetOverflow( m_overflow == Overflow );
19d7140e
VZ
2905 attr->SetKind( m_attrkind );
2906
a68c1246
VZ
2907 return attr;
2908}
2909
19d7140e
VZ
2910void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
2911{
2912 if ( !HasTextColour() && mergefrom->HasTextColour() )
2913 SetTextColour(mergefrom->GetTextColour());
2914 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
2915 SetBackgroundColour(mergefrom->GetBackgroundColour());
2916 if ( !HasFont() && mergefrom->HasFont() )
2917 SetFont(mergefrom->GetFont());
4db6714b
KH
2918 if ( !HasAlignment() && mergefrom->HasAlignment() )
2919 {
19d7140e
VZ
2920 int hAlign, vAlign;
2921 mergefrom->GetAlignment( &hAlign, &vAlign);
2922 SetAlignment(hAlign, vAlign);
2923 }
3100c3db
RD
2924 if ( !HasSize() && mergefrom->HasSize() )
2925 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
27f35b66 2926
19d7140e
VZ
2927 // Directly access member functions as GetRender/Editor don't just return
2928 // m_renderer/m_editor
2929 //
2930 // Maybe add support for merge of Render and Editor?
2931 if (!HasRenderer() && mergefrom->HasRenderer() )
bf7945ce 2932 {
19d7140e
VZ
2933 m_renderer = mergefrom->m_renderer;
2934 m_renderer->IncRef();
2935 }
2936 if ( !HasEditor() && mergefrom->HasEditor() )
2937 {
2938 m_editor = mergefrom->m_editor;
2939 m_editor->IncRef();
2940 }
2f024384 2941 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
19d7140e
VZ
2942 SetReadOnly(mergefrom->IsReadOnly());
2943
2f024384 2944 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
ff699386 2945 SetOverflow(mergefrom->GetOverflow());
ef5df12b 2946
19d7140e
VZ
2947 SetDefAttr(mergefrom->m_defGridAttr);
2948}
2949
27f35b66
SN
2950void wxGridCellAttr::SetSize(int num_rows, int num_cols)
2951{
2952 // The size of a cell is normally 1,1
2953
2954 // If this cell is larger (2,2) then this is the top left cell
2955 // the other cells that will be covered (lower right cells) must be
2956 // set to negative or zero values such that
2957 // row + num_rows of the covered cell points to the larger cell (this cell)
2958 // same goes for the col + num_cols.
2959
2960 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2961
2f024384
DS
2962 wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) ||
2963 !((num_rows <= 0) && (num_cols > 0)) ||
2964 !((num_rows == 0) && (num_cols == 0))),
27f35b66
SN
2965 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2966
2967 m_sizeRows = num_rows;
2968 m_sizeCols = num_cols;
2969}
2970
2796cce3
RD
2971const wxColour& wxGridCellAttr::GetTextColour() const
2972{
2973 if (HasTextColour())
508011ce 2974 {
2796cce3 2975 return m_colText;
508011ce 2976 }
0926b2fc 2977 else if (m_defGridAttr && m_defGridAttr != this)
508011ce 2978 {
2796cce3 2979 return m_defGridAttr->GetTextColour();
508011ce
VZ
2980 }
2981 else
2982 {
2796cce3
RD
2983 wxFAIL_MSG(wxT("Missing default cell attribute"));
2984 return wxNullColour;
2985 }
2986}
2987
2796cce3
RD
2988const wxColour& wxGridCellAttr::GetBackgroundColour() const
2989{
2990 if (HasBackgroundColour())
2f024384 2991 {
2796cce3 2992 return m_colBack;
2f024384 2993 }
0926b2fc 2994 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 2995 {
2796cce3 2996 return m_defGridAttr->GetBackgroundColour();
2f024384 2997 }
508011ce
VZ
2998 else
2999 {
2796cce3
RD
3000 wxFAIL_MSG(wxT("Missing default cell attribute"));
3001 return wxNullColour;
3002 }
3003}
3004
2796cce3
RD
3005const wxFont& wxGridCellAttr::GetFont() const
3006{
3007 if (HasFont())
2f024384 3008 {
2796cce3 3009 return m_font;
2f024384 3010 }
0926b2fc 3011 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 3012 {
2796cce3 3013 return m_defGridAttr->GetFont();
2f024384 3014 }
508011ce
VZ
3015 else
3016 {
2796cce3
RD
3017 wxFAIL_MSG(wxT("Missing default cell attribute"));
3018 return wxNullFont;
3019 }
3020}
3021
2796cce3
RD
3022void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
3023{
508011ce
VZ
3024 if (HasAlignment())
3025 {
4db6714b
KH
3026 if ( hAlign )
3027 *hAlign = m_hAlign;
3028 if ( vAlign )
3029 *vAlign = m_vAlign;
2796cce3 3030 }
0926b2fc 3031 else if (m_defGridAttr && m_defGridAttr != this)
c2f5b920 3032 {
2796cce3 3033 m_defGridAttr->GetAlignment(hAlign, vAlign);
c2f5b920 3034 }
508011ce
VZ
3035 else
3036 {
2796cce3
RD
3037 wxFAIL_MSG(wxT("Missing default cell attribute"));
3038 }
3039}
3040
27f35b66
SN
3041void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
3042{
4db6714b
KH
3043 if ( num_rows )
3044 *num_rows = m_sizeRows;
3045 if ( num_cols )
3046 *num_cols = m_sizeCols;
27f35b66 3047}
2796cce3 3048
f2d76237 3049// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
3050// which attribute to use. If a non-default attr object has one then it is
3051// used, otherwise the default editor or renderer is fetched from the grid and
3052// used. It should be the default for the data type of the cell. If it is
3053// NULL (because the table has a type that the grid does not have in its
c2f5b920 3054// registry), then the grid's default editor or renderer is used.
28a77bc4 3055
ef316e23 3056wxGridCellRenderer* wxGridCellAttr::GetRenderer(const wxGrid* grid, int row, int col) const
28a77bc4 3057{
c2f5b920 3058 wxGridCellRenderer *renderer = NULL;
28a77bc4 3059
3cf883a2 3060 if ( m_renderer && this != m_defGridAttr )
0b190b0f 3061 {
3cf883a2
VZ
3062 // use the cells renderer if it has one
3063 renderer = m_renderer;
3064 renderer->IncRef();
0b190b0f 3065 }
2f024384 3066 else // no non-default cell renderer
0b190b0f 3067 {
3cf883a2
VZ
3068 // get default renderer for the data type
3069 if ( grid )
3070 {
3071 // GetDefaultRendererForCell() will do IncRef() for us
3072 renderer = grid->GetDefaultRendererForCell(row, col);
3073 }
0b190b0f 3074
c2f5b920 3075 if ( renderer == NULL )
3cf883a2 3076 {
c2f5b920 3077 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
3cf883a2
VZ
3078 {
3079 // if we still don't have one then use the grid default
3080 // (no need for IncRef() here neither)
3081 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
3082 }
3083 else // default grid attr
3084 {
3085 // use m_renderer which we had decided not to use initially
3086 renderer = m_renderer;
3087 if ( renderer )
3088 renderer->IncRef();
3089 }
3090 }
0b190b0f 3091 }
28a77bc4 3092
3cf883a2
VZ
3093 // we're supposed to always find something
3094 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
28a77bc4
RD
3095
3096 return renderer;
2796cce3
RD
3097}
3098
3cf883a2 3099// same as above, except for s/renderer/editor/g
ef316e23 3100wxGridCellEditor* wxGridCellAttr::GetEditor(const wxGrid* grid, int row, int col) const
07296f0b 3101{
c2f5b920 3102 wxGridCellEditor *editor = NULL;
0b190b0f 3103
3cf883a2 3104 if ( m_editor && this != m_defGridAttr )
0b190b0f 3105 {
3cf883a2
VZ
3106 // use the cells editor if it has one
3107 editor = m_editor;
3108 editor->IncRef();
0b190b0f 3109 }
3cf883a2 3110 else // no non default cell editor
0b190b0f 3111 {
3cf883a2
VZ
3112 // get default editor for the data type
3113 if ( grid )
3114 {
3115 // GetDefaultEditorForCell() will do IncRef() for us
3116 editor = grid->GetDefaultEditorForCell(row, col);
3117 }
3cf883a2 3118
c2f5b920 3119 if ( editor == NULL )
3cf883a2 3120 {
c2f5b920 3121 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
3cf883a2
VZ
3122 {
3123 // if we still don't have one then use the grid default
3124 // (no need for IncRef() here neither)
3125 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
3126 }
3127 else // default grid attr
3128 {
3129 // use m_editor which we had decided not to use initially
3130 editor = m_editor;
3131 if ( editor )
3132 editor->IncRef();
3133 }
3134 }
0b190b0f 3135 }
28a77bc4 3136
3cf883a2
VZ
3137 // we're supposed to always find something
3138 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
3139
28a77bc4 3140 return editor;
07296f0b
RD
3141}
3142
b99be8fb 3143// ----------------------------------------------------------------------------
758cbedf 3144// wxGridCellAttrData
b99be8fb
VZ
3145// ----------------------------------------------------------------------------
3146
758cbedf 3147void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb 3148{
96ca74cd
SN
3149 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
3150 // touch attribute's reference counting explicitly, since this
3151 // is managed by class wxGridCellWithAttr
b99be8fb
VZ
3152 int n = FindIndex(row, col);
3153 if ( n == wxNOT_FOUND )
3154 {
a70517e9
VZ
3155 if ( attr )
3156 {
3157 // add the attribute
3158 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
3159 }
3160 //else: nothing to do
b99be8fb 3161 }
6f292345 3162 else // we already have an attribute for this cell
b99be8fb
VZ
3163 {
3164 if ( attr )
3165 {
3166 // change the attribute
96ca74cd 3167 m_attrs[(size_t)n].ChangeAttr(attr);
b99be8fb
VZ
3168 }
3169 else
3170 {
3171 // remove this attribute
3172 m_attrs.RemoveAt((size_t)n);
3173 }
3174 }
b99be8fb
VZ
3175}
3176
758cbedf 3177wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb 3178{
10a4531d 3179 wxGridCellAttr *attr = NULL;
b99be8fb
VZ
3180
3181 int n = FindIndex(row, col);
3182 if ( n != wxNOT_FOUND )
3183 {
2e9a6788
VZ
3184 attr = m_attrs[(size_t)n].attr;
3185 attr->IncRef();
b99be8fb
VZ
3186 }
3187
3188 return attr;
3189}
3190
4d60017a
SN
3191void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
3192{
3193 size_t count = m_attrs.GetCount();
3194 for ( size_t n = 0; n < count; n++ )
3195 {
3196 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
3197 wxCoord row = coords.GetRow();
3198 if ((size_t)row >= pos)
3199 {
3200 if (numRows > 0)
3201 {
3202 // If rows inserted, include row counter where necessary
3203 coords.SetRow(row + numRows);
3204 }
3205 else if (numRows < 0)
3206 {
3207 // If rows deleted ...
3208 if ((size_t)row >= pos - numRows)
3209 {
3210 // ...either decrement row counter (if row still exists)...
3211 coords.SetRow(row + numRows);
3212 }
3213 else
3214 {
3215 // ...or remove the attribute
01dd42b6 3216 m_attrs.RemoveAt(n);
4db6714b
KH
3217 n--;
3218 count--;
d1c0b4f9
VZ
3219 }
3220 }
4d60017a
SN
3221 }
3222 }
3223}
3224
3225void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
3226{
3227 size_t count = m_attrs.GetCount();
3228 for ( size_t n = 0; n < count; n++ )
3229 {
3230 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
3231 wxCoord col = coords.GetCol();
3232 if ( (size_t)col >= pos )
3233 {
3234 if ( numCols > 0 )
3235 {
3236 // If rows inserted, include row counter where necessary
3237 coords.SetCol(col + numCols);
3238 }
3239 else if (numCols < 0)
3240 {
3241 // If rows deleted ...
3242 if ((size_t)col >= pos - numCols)
3243 {
3244 // ...either decrement row counter (if row still exists)...
3245 coords.SetCol(col + numCols);
3246 }
3247 else
3248 {
3249 // ...or remove the attribute
01dd42b6 3250 m_attrs.RemoveAt(n);
2f024384
DS
3251 n--;
3252 count--;
d1c0b4f9
VZ
3253 }
3254 }
4d60017a
SN
3255 }
3256 }
3257}
3258
758cbedf 3259int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
3260{
3261 size_t count = m_attrs.GetCount();
3262 for ( size_t n = 0; n < count; n++ )
3263 {
3264 const wxGridCellCoords& coords = m_attrs[n].coords;
3265 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
3266 {
3267 return n;
3268 }
3269 }
3270
3271 return wxNOT_FOUND;
3272}
3273
758cbedf
VZ
3274// ----------------------------------------------------------------------------
3275// wxGridRowOrColAttrData
3276// ----------------------------------------------------------------------------
3277
3278wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
3279{
b4a980f4 3280 size_t count = m_attrs.GetCount();
758cbedf
VZ
3281 for ( size_t n = 0; n < count; n++ )
3282 {
3283 m_attrs[n]->DecRef();
3284 }
3285}
3286
3287wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
3288{
10a4531d 3289 wxGridCellAttr *attr = NULL;
758cbedf
VZ
3290
3291 int n = m_rowsOrCols.Index(rowOrCol);
3292 if ( n != wxNOT_FOUND )
3293 {
3294 attr = m_attrs[(size_t)n];
3295 attr->IncRef();
3296 }
3297
3298 return attr;
3299}
3300
3301void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
3302{
a95e38c0
VZ
3303 int i = m_rowsOrCols.Index(rowOrCol);
3304 if ( i == wxNOT_FOUND )
758cbedf 3305 {
62d249cb
VZ
3306 if ( attr )
3307 {
96ca74cd
SN
3308 // add the attribute - no need to do anything to reference count
3309 // since we take ownership of the attribute.
62d249cb
VZ
3310 m_rowsOrCols.Add(rowOrCol);
3311 m_attrs.Add(attr);
3312 }
3313 // nothing to remove
758cbedf
VZ
3314 }
3315 else
3316 {
a95e38c0 3317 size_t n = (size_t)i;
d1b021ff
SN
3318 if ( m_attrs[n] == attr )
3319 // nothing to do
54181a33 3320 return;
758cbedf
VZ
3321 if ( attr )
3322 {
96ca74cd
SN
3323 // change the attribute, handling reference count manually,
3324 // taking ownership of the new attribute.
a95e38c0
VZ
3325 m_attrs[n]->DecRef();
3326 m_attrs[n] = attr;
758cbedf
VZ
3327 }
3328 else
3329 {
96ca74cd 3330 // remove this attribute, handling reference count manually
a95e38c0
VZ
3331 m_attrs[n]->DecRef();
3332 m_rowsOrCols.RemoveAt(n);
3333 m_attrs.RemoveAt(n);
758cbedf
VZ
3334 }
3335 }
3336}
3337
4d60017a
SN
3338void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
3339{
3340 size_t count = m_attrs.GetCount();
3341 for ( size_t n = 0; n < count; n++ )
3342 {
3343 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
3344 if ( (size_t)rowOrCol >= pos )
3345 {
3346 if ( numRowsOrCols > 0 )
3347 {
3348 // If rows inserted, include row counter where necessary
3349 rowOrCol += numRowsOrCols;
3350 }
3351 else if ( numRowsOrCols < 0)
3352 {
3353 // If rows deleted, either decrement row counter (if row still exists)
3354 if ((size_t)rowOrCol >= pos - numRowsOrCols)
3355 rowOrCol += numRowsOrCols;
3356 else
3357 {
01dd42b6
VZ
3358 m_rowsOrCols.RemoveAt(n);
3359 m_attrs[n]->DecRef();
3360 m_attrs.RemoveAt(n);
4db6714b
KH
3361 n--;
3362 count--;
d1c0b4f9
VZ
3363 }
3364 }
4d60017a
SN
3365 }
3366 }
3367}
3368
b99be8fb
VZ
3369// ----------------------------------------------------------------------------
3370// wxGridCellAttrProvider
3371// ----------------------------------------------------------------------------
3372
3373wxGridCellAttrProvider::wxGridCellAttrProvider()
3374{
10a4531d 3375 m_data = NULL;
b99be8fb
VZ
3376}
3377
3378wxGridCellAttrProvider::~wxGridCellAttrProvider()
3379{
3380 delete m_data;
3381}
3382
3383void wxGridCellAttrProvider::InitData()
3384{
3385 m_data = new wxGridCellAttrProviderData;
3386}
3387
19d7140e
VZ
3388wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
3389 wxGridCellAttr::wxAttrKind kind ) const
b99be8fb 3390{
10a4531d 3391 wxGridCellAttr *attr = NULL;
758cbedf
VZ
3392 if ( m_data )
3393 {
962a48f6 3394 switch (kind)
758cbedf 3395 {
19d7140e 3396 case (wxGridCellAttr::Any):
962a48f6
DS
3397 // Get cached merge attributes.
3398 // Currently not used as no cache implemented as not mutable
19d7140e 3399 // attr = m_data->m_mergeAttr.GetAttr(row, col);
4db6714b 3400 if (!attr)
19d7140e 3401 {
962a48f6
DS
3402 // Basically implement old version.
3403 // Also check merge cache, so we don't have to re-merge every time..
999836aa
VZ
3404 wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col);
3405 wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row);
3406 wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col);
19d7140e 3407
4db6714b
KH
3408 if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol))
3409 {
2d0c2e79 3410 // Two or more are non NULL
19d7140e
VZ
3411 attr = new wxGridCellAttr;
3412 attr->SetKind(wxGridCellAttr::Merged);
3413
962a48f6 3414 // Order is important..
4db6714b
KH
3415 if (attrcell)
3416 {
19d7140e
VZ
3417 attr->MergeWith(attrcell);
3418 attrcell->DecRef();
3419 }
4db6714b
KH
3420 if (attrcol)
3421 {
19d7140e
VZ
3422 attr->MergeWith(attrcol);
3423 attrcol->DecRef();
3424 }
4db6714b
KH
3425 if (attrrow)
3426 {
19d7140e
VZ
3427 attr->MergeWith(attrrow);
3428 attrrow->DecRef();
3429 }
962a48f6
DS
3430
3431 // store merge attr if cache implemented
19d7140e
VZ
3432 //attr->IncRef();
3433 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3434 }
3435 else
2d0c2e79 3436 {
19d7140e 3437 // one or none is non null return it or null.
4db6714b
KH
3438 if (attrrow)
3439 attr = attrrow;
3440 if (attrcol)
2d0c2e79 3441 {
962a48f6 3442 if (attr)
2d0c2e79
RD
3443 attr->DecRef();
3444 attr = attrcol;
3445 }
4db6714b 3446 if (attrcell)
2d0c2e79 3447 {
4db6714b 3448 if (attr)
2d0c2e79
RD
3449 attr->DecRef();
3450 attr = attrcell;
3451 }
19d7140e
VZ
3452 }
3453 }
2f024384 3454 break;
4db6714b 3455
19d7140e
VZ
3456 case (wxGridCellAttr::Cell):
3457 attr = m_data->m_cellAttrs.GetAttr(row, col);
2f024384 3458 break;
4db6714b 3459
19d7140e 3460 case (wxGridCellAttr::Col):
2d0c2e79 3461 attr = m_data->m_colAttrs.GetAttr(col);
2f024384 3462 break;
4db6714b 3463
19d7140e 3464 case (wxGridCellAttr::Row):
2d0c2e79 3465 attr = m_data->m_rowAttrs.GetAttr(row);
2f024384 3466 break;
4db6714b 3467
19d7140e
VZ
3468 default:
3469 // unused as yet...
3470 // (wxGridCellAttr::Default):
3471 // (wxGridCellAttr::Merged):
2f024384 3472 break;
758cbedf
VZ
3473 }
3474 }
2f024384 3475
758cbedf 3476 return attr;
b99be8fb
VZ
3477}
3478
2e9a6788 3479void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
3480 int row, int col)
3481{
3482 if ( !m_data )
3483 InitData();
3484
758cbedf
VZ
3485 m_data->m_cellAttrs.SetAttr(attr, row, col);
3486}
3487
3488void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
3489{
3490 if ( !m_data )
3491 InitData();
3492
3493 m_data->m_rowAttrs.SetAttr(attr, row);
3494}
3495
3496void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
3497{
3498 if ( !m_data )
3499 InitData();
3500
3501 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
3502}
3503
4d60017a
SN
3504void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
3505{
3506 if ( m_data )
3507 {
3508 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
3509
d1c0b4f9 3510 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
3511 }
3512}
3513
3514void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
3515{
3516 if ( m_data )
3517 {
3518 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
3519
d1c0b4f9 3520 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
3521 }
3522}
3523
f2d76237
RD
3524// ----------------------------------------------------------------------------
3525// wxGridTypeRegistry
3526// ----------------------------------------------------------------------------
3527
3528wxGridTypeRegistry::~wxGridTypeRegistry()
3529{
b4a980f4 3530 size_t count = m_typeinfo.GetCount();
b94ae1ea 3531 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
3532 delete m_typeinfo[i];
3533}
3534
f2d76237
RD
3535void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
3536 wxGridCellRenderer* renderer,
3537 wxGridCellEditor* editor)
3538{
f2d76237
RD
3539 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
3540
3541 // is it already registered?
c4608a8a 3542 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
3543 if ( loc != wxNOT_FOUND )
3544 {
f2d76237
RD
3545 delete m_typeinfo[loc];
3546 m_typeinfo[loc] = info;
3547 }
39bcce60
VZ
3548 else
3549 {
f2d76237
RD
3550 m_typeinfo.Add(info);
3551 }
3552}
3553
c4608a8a
VZ
3554int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
3555{
3556 size_t count = m_typeinfo.GetCount();
3557 for ( size_t i = 0; i < count; i++ )
3558 {
3559 if ( typeName == m_typeinfo[i]->m_typeName )
3560 {
3561 return i;
3562 }
3563 }
3564
3565 return wxNOT_FOUND;
3566}
3567
f2d76237
RD
3568int wxGridTypeRegistry::FindDataType(const wxString& typeName)
3569{
c4608a8a
VZ
3570 int index = FindRegisteredDataType(typeName);
3571 if ( index == wxNOT_FOUND )
3572 {
3573 // check whether this is one of the standard ones, in which case
3574 // register it "on the fly"
3a8c693a 3575#if wxUSE_TEXTCTRL
c4608a8a
VZ
3576 if ( typeName == wxGRID_VALUE_STRING )
3577 {
3578 RegisterDataType(wxGRID_VALUE_STRING,
3579 new wxGridCellStringRenderer,
3580 new wxGridCellTextEditor);
4db6714b
KH
3581 }
3582 else
3a8c693a
VZ
3583#endif // wxUSE_TEXTCTRL
3584#if wxUSE_CHECKBOX
3585 if ( typeName == wxGRID_VALUE_BOOL )
c4608a8a
VZ
3586 {
3587 RegisterDataType(wxGRID_VALUE_BOOL,
3588 new wxGridCellBoolRenderer,
3589 new wxGridCellBoolEditor);
4db6714b
KH
3590 }
3591 else
3a8c693a
VZ
3592#endif // wxUSE_CHECKBOX
3593#if wxUSE_TEXTCTRL
3594 if ( typeName == wxGRID_VALUE_NUMBER )
c4608a8a
VZ
3595 {
3596 RegisterDataType(wxGRID_VALUE_NUMBER,
3597 new wxGridCellNumberRenderer,
3598 new wxGridCellNumberEditor);
3599 }
3600 else if ( typeName == wxGRID_VALUE_FLOAT )
3601 {
3602 RegisterDataType(wxGRID_VALUE_FLOAT,
3603 new wxGridCellFloatRenderer,
3604 new wxGridCellFloatEditor);
4db6714b
KH
3605 }
3606 else
3a8c693a
VZ
3607#endif // wxUSE_TEXTCTRL
3608#if wxUSE_COMBOBOX
3609 if ( typeName == wxGRID_VALUE_CHOICE )
c4608a8a
VZ
3610 {
3611 RegisterDataType(wxGRID_VALUE_CHOICE,
3612 new wxGridCellStringRenderer,
3613 new wxGridCellChoiceEditor);
4db6714b
KH
3614 }
3615 else
3a8c693a 3616#endif // wxUSE_COMBOBOX
c4608a8a
VZ
3617 {
3618 return wxNOT_FOUND;
3619 }
f2d76237 3620
c4608a8a
VZ
3621 // we get here only if just added the entry for this type, so return
3622 // the last index
3623 index = m_typeinfo.GetCount() - 1;
3624 }
3625
3626 return index;
3627}
3628
3629int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
3630{
3631 int index = FindDataType(typeName);
3632 if ( index == wxNOT_FOUND )
3633 {
3634 // the first part of the typename is the "real" type, anything after ':'
3635 // are the parameters for the renderer
3636 index = FindDataType(typeName.BeforeFirst(_T(':')));
3637 if ( index == wxNOT_FOUND )
3638 {
3639 return wxNOT_FOUND;
f2d76237 3640 }
c4608a8a
VZ
3641
3642 wxGridCellRenderer *renderer = GetRenderer(index);
3643 wxGridCellRenderer *rendererOld = renderer;
3644 renderer = renderer->Clone();
3645 rendererOld->DecRef();
3646
3647 wxGridCellEditor *editor = GetEditor(index);
3648 wxGridCellEditor *editorOld = editor;
3649 editor = editor->Clone();
3650 editorOld->DecRef();
3651
3652 // do it even if there are no parameters to reset them to defaults
3653 wxString params = typeName.AfterFirst(_T(':'));
3654 renderer->SetParameters(params);
3655 editor->SetParameters(params);
3656
3657 // register the new typename
c4608a8a
VZ
3658 RegisterDataType(typeName, renderer, editor);
3659
3660 // we just registered it, it's the last one
3661 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
3662 }
3663
c4608a8a 3664 return index;
f2d76237
RD
3665}
3666
3667wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
3668{
3669 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
faec5a43
SN
3670 if (renderer)
3671 renderer->IncRef();
2f024384 3672
f2d76237
RD
3673 return renderer;
3674}
3675
0b190b0f 3676wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
3677{
3678 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
faec5a43
SN
3679 if (editor)
3680 editor->IncRef();
2f024384 3681
f2d76237
RD
3682 return editor;
3683}
3684
758cbedf
VZ
3685// ----------------------------------------------------------------------------
3686// wxGridTableBase
3687// ----------------------------------------------------------------------------
3688
f85afd4e
MB
3689IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
3690
f85afd4e 3691wxGridTableBase::wxGridTableBase()
f85afd4e 3692{
10a4531d
VZ
3693 m_view = NULL;
3694 m_attrProvider = NULL;
f85afd4e
MB
3695}
3696
3697wxGridTableBase::~wxGridTableBase()
3698{
b99be8fb
VZ
3699 delete m_attrProvider;
3700}
3701
3702void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
3703{
3704 delete m_attrProvider;
3705 m_attrProvider = attrProvider;
f85afd4e
MB
3706}
3707
f2d76237
RD
3708bool wxGridTableBase::CanHaveAttributes()
3709{
3710 if ( ! GetAttrProvider() )
3711 {
3712 // use the default attr provider by default
3713 SetAttrProvider(new wxGridCellAttrProvider);
3714 }
2f024384 3715
ca65c044 3716 return true;
f2d76237
RD
3717}
3718
19d7140e 3719wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
b99be8fb
VZ
3720{
3721 if ( m_attrProvider )
19d7140e 3722 return m_attrProvider->GetAttr(row, col, kind);
b99be8fb 3723 else
10a4531d 3724 return NULL;
b99be8fb
VZ
3725}
3726
758cbedf 3727void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
3728{
3729 if ( m_attrProvider )
3730 {
6f292345
VZ
3731 if ( attr )
3732 attr->SetKind(wxGridCellAttr::Cell);
b99be8fb
VZ
3733 m_attrProvider->SetAttr(attr, row, col);
3734 }
3735 else
3736 {
3737 // as we take ownership of the pointer and don't store it, we must
3738 // free it now
39bcce60 3739 wxSafeDecRef(attr);
b99be8fb
VZ
3740 }
3741}
3742
758cbedf
VZ
3743void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
3744{
3745 if ( m_attrProvider )
3746 {
19d7140e 3747 attr->SetKind(wxGridCellAttr::Row);
758cbedf
VZ
3748 m_attrProvider->SetRowAttr(attr, row);
3749 }
3750 else
3751 {
3752 // as we take ownership of the pointer and don't store it, we must
3753 // free it now
39bcce60 3754 wxSafeDecRef(attr);
758cbedf
VZ
3755 }
3756}
3757
3758void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
3759{
3760 if ( m_attrProvider )
3761 {
19d7140e 3762 attr->SetKind(wxGridCellAttr::Col);
758cbedf
VZ
3763 m_attrProvider->SetColAttr(attr, col);
3764 }
3765 else
3766 {
3767 // as we take ownership of the pointer and don't store it, we must
3768 // free it now
39bcce60 3769 wxSafeDecRef(attr);
758cbedf
VZ
3770 }
3771}
3772
aa5e1f75
SN
3773bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
3774 size_t WXUNUSED(numRows) )
f85afd4e 3775{
f6bcfd97 3776 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
8f177c8e 3777
ca65c044 3778 return false;
f85afd4e
MB
3779}
3780
aa5e1f75 3781bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 3782{
f6bcfd97 3783 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
8f177c8e 3784
ca65c044 3785 return false;
f85afd4e
MB
3786}
3787
aa5e1f75
SN
3788bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
3789 size_t WXUNUSED(numRows) )
f85afd4e 3790{
f6bcfd97 3791 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
8f177c8e 3792
ca65c044 3793 return false;
f85afd4e
MB
3794}
3795
aa5e1f75
SN
3796bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
3797 size_t WXUNUSED(numCols) )
f85afd4e 3798{
f6bcfd97 3799 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
8f177c8e 3800
ca65c044 3801 return false;
f85afd4e
MB
3802}
3803
aa5e1f75 3804bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 3805{
f6bcfd97 3806 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
8f177c8e 3807
ca65c044 3808 return false;
f85afd4e
MB
3809}
3810
aa5e1f75
SN
3811bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
3812 size_t WXUNUSED(numCols) )
f85afd4e 3813{
f6bcfd97 3814 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
8f177c8e 3815
ca65c044 3816 return false;
f85afd4e
MB
3817}
3818
f85afd4e
MB
3819wxString wxGridTableBase::GetRowLabelValue( int row )
3820{
3821 wxString s;
93763ad5 3822
2f024384
DS
3823 // RD: Starting the rows at zero confuses users,
3824 // no matter how much it makes sense to us geeks.
3825 s << row + 1;
3826
f85afd4e
MB
3827 return s;
3828}
3829
3830wxString wxGridTableBase::GetColLabelValue( int col )
3831{
3832 // default col labels are:
3833 // cols 0 to 25 : A-Z
3834 // cols 26 to 675 : AA-ZZ
3835 // etc.
3836
3837 wxString s;
3838 unsigned int i, n;
3839 for ( n = 1; ; n++ )
3840 {
2f024384
DS
3841 s += (wxChar) (_T('A') + (wxChar)(col % 26));
3842 col = col / 26 - 1;
4db6714b
KH
3843 if ( col < 0 )
3844 break;
f85afd4e
MB
3845 }
3846
3847 // reverse the string...
3848 wxString s2;
3d59537f 3849 for ( i = 0; i < n; i++ )
f85afd4e 3850 {
2f024384 3851 s2 += s[n - i - 1];
f85afd4e
MB
3852 }
3853
3854 return s2;
3855}
3856
f2d76237
RD
3857wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
3858{
816be743 3859 return wxGRID_VALUE_STRING;
f2d76237
RD
3860}
3861
3862bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
3863 const wxString& typeName )
3864{
816be743 3865 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
3866}
3867
3868bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
3869{
3870 return CanGetValueAs(row, col, typeName);
3871}
3872
3873long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
3874{
3875 return 0;
3876}
3877
3878double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
3879{
3880 return 0.0;
3881}
3882
3883bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
3884{
ca65c044 3885 return false;
f2d76237
RD
3886}
3887
3888void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
3889 long WXUNUSED(value) )
3890{
3891}
3892
3893void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
3894 double WXUNUSED(value) )
3895{
3896}
3897
3898void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
3899 bool WXUNUSED(value) )
3900{
3901}
3902
f2d76237
RD
3903void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3904 const wxString& WXUNUSED(typeName) )
3905{
3906 return NULL;
3907}
3908
3909void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3910 const wxString& WXUNUSED(typeName),
3911 void* WXUNUSED(value) )
3912{
3913}
3914
f85afd4e
MB
3915//////////////////////////////////////////////////////////////////////
3916//
3917// Message class for the grid table to send requests and notifications
3918// to the grid view
3919//
3920
3921wxGridTableMessage::wxGridTableMessage()
3922{
10a4531d 3923 m_table = NULL;
f85afd4e
MB
3924 m_id = -1;
3925 m_comInt1 = -1;
3926 m_comInt2 = -1;
3927}
3928
3929wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
3930 int commandInt1, int commandInt2 )
3931{
3932 m_table = table;
3933 m_id = id;
3934 m_comInt1 = commandInt1;
3935 m_comInt2 = commandInt2;
3936}
3937
f85afd4e
MB
3938//////////////////////////////////////////////////////////////////////
3939//
3940// A basic grid table for string data. An object of this class will
3941// created by wxGrid if you don't specify an alternative table class.
3942//
3943
223d09f6 3944WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
3945
3946IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
3947
3948wxGridStringTable::wxGridStringTable()
3949 : wxGridTableBase()
3950{
3951}
3952
3953wxGridStringTable::wxGridStringTable( int numRows, int numCols )
3954 : wxGridTableBase()
3955{
f85afd4e
MB
3956 m_data.Alloc( numRows );
3957
3958 wxArrayString sa;
3959 sa.Alloc( numCols );
27f35b66 3960 sa.Add( wxEmptyString, numCols );
8f177c8e 3961
27f35b66 3962 m_data.Add( sa, numRows );
f85afd4e
MB
3963}
3964
3965wxGridStringTable::~wxGridStringTable()
3966{
3967}
3968
e32352cf 3969int wxGridStringTable::GetNumberRows()
f85afd4e
MB
3970{
3971 return m_data.GetCount();
3972}
3973
e32352cf 3974int wxGridStringTable::GetNumberCols()
f85afd4e
MB
3975{
3976 if ( m_data.GetCount() > 0 )
3977 return m_data[0].GetCount();
3978 else
3979 return 0;
3980}
3981
3982wxString wxGridStringTable::GetValue( int row, int col )
3983{
3e13956a
RD
3984 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3985 wxEmptyString,
3986 _T("invalid row or column index in wxGridStringTable") );
af547d51 3987
f85afd4e
MB
3988 return m_data[row][col];
3989}
3990
f2d76237 3991void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 3992{
3e13956a
RD
3993 wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()),
3994 _T("invalid row or column index in wxGridStringTable") );
af547d51 3995
f2d76237 3996 m_data[row][col] = value;
f85afd4e
MB
3997}
3998
f85afd4e
MB
3999void wxGridStringTable::Clear()
4000{
4001 int row, col;
4002 int numRows, numCols;
8f177c8e 4003
f85afd4e
MB
4004 numRows = m_data.GetCount();
4005 if ( numRows > 0 )
4006 {
4007 numCols = m_data[0].GetCount();
4008
3d59537f 4009 for ( row = 0; row < numRows; row++ )
f85afd4e 4010 {
3d59537f 4011 for ( col = 0; col < numCols; col++ )
f85afd4e
MB
4012 {
4013 m_data[row][col] = wxEmptyString;
4014 }
4015 }
4016 }
4017}
4018
f85afd4e
MB
4019bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
4020{
f85afd4e 4021 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
4022 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
4023 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 4024
f85afd4e
MB
4025 if ( pos >= curNumRows )
4026 {
4027 return AppendRows( numRows );
4028 }
8f177c8e 4029
f85afd4e
MB
4030 wxArrayString sa;
4031 sa.Alloc( curNumCols );
27f35b66
SN
4032 sa.Add( wxEmptyString, curNumCols );
4033 m_data.Insert( sa, pos, numRows );
2f024384 4034
f85afd4e
MB
4035 if ( GetView() )
4036 {
4037 wxGridTableMessage msg( this,
4038 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
4039 pos,
4040 numRows );
8f177c8e 4041
f85afd4e
MB
4042 GetView()->ProcessTableMessage( msg );
4043 }
4044
ca65c044 4045 return true;
f85afd4e
MB
4046}
4047
4048bool wxGridStringTable::AppendRows( size_t numRows )
4049{
f85afd4e 4050 size_t curNumRows = m_data.GetCount();
4db6714b
KH
4051 size_t curNumCols = ( curNumRows > 0
4052 ? m_data[0].GetCount()
4053 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 4054
f85afd4e
MB
4055 wxArrayString sa;
4056 if ( curNumCols > 0 )
4057 {
4058 sa.Alloc( curNumCols );
27f35b66 4059 sa.Add( wxEmptyString, curNumCols );
f85afd4e 4060 }
8f177c8e 4061
27f35b66 4062 m_data.Add( sa, numRows );
f85afd4e
MB
4063
4064 if ( GetView() )
4065 {
4066 wxGridTableMessage msg( this,
4067 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
4068 numRows );
8f177c8e 4069
f85afd4e
MB
4070 GetView()->ProcessTableMessage( msg );
4071 }
4072
ca65c044 4073 return true;
f85afd4e
MB
4074}
4075
4076bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
4077{
f85afd4e 4078 size_t curNumRows = m_data.GetCount();
8f177c8e 4079
f85afd4e
MB
4080 if ( pos >= curNumRows )
4081 {
e91d2033
VZ
4082 wxFAIL_MSG( wxString::Format
4083 (
4084 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
4085 (unsigned long)pos,
4086 (unsigned long)numRows,
4087 (unsigned long)curNumRows
4088 ) );
4089
ca65c044 4090 return false;
f85afd4e
MB
4091 }
4092
4093 if ( numRows > curNumRows - pos )
4094 {
4095 numRows = curNumRows - pos;
4096 }
8f177c8e 4097
f85afd4e
MB
4098 if ( numRows >= curNumRows )
4099 {
d57ad377 4100 m_data.Clear();
f85afd4e
MB
4101 }
4102 else
4103 {
27f35b66 4104 m_data.RemoveAt( pos, numRows );
f85afd4e 4105 }
4db6714b 4106
f85afd4e
MB
4107 if ( GetView() )
4108 {
4109 wxGridTableMessage msg( this,
4110 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
4111 pos,
4112 numRows );
8f177c8e 4113
f85afd4e
MB
4114 GetView()->ProcessTableMessage( msg );
4115 }
4116
ca65c044 4117 return true;
f85afd4e
MB
4118}
4119
4120bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
4121{
4122 size_t row, col;
4123
4124 size_t curNumRows = m_data.GetCount();
4db6714b
KH
4125 size_t curNumCols = ( curNumRows > 0
4126 ? m_data[0].GetCount()
4127 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 4128
f85afd4e
MB
4129 if ( pos >= curNumCols )
4130 {
4131 return AppendCols( numCols );
4132 }
4133
d4175745
VZ
4134 if ( !m_colLabels.IsEmpty() )
4135 {
4136 m_colLabels.Insert( wxEmptyString, pos, numCols );
4137
4138 size_t i;
4139 for ( i = pos; i < pos + numCols; i++ )
4140 m_colLabels[i] = wxGridTableBase::GetColLabelValue( i );
4141 }
4142
3d59537f 4143 for ( row = 0; row < curNumRows; row++ )
f85afd4e 4144 {
3d59537f 4145 for ( col = pos; col < pos + numCols; col++ )
f85afd4e
MB
4146 {
4147 m_data[row].Insert( wxEmptyString, col );
4148 }
4149 }
4db6714b 4150
f85afd4e
MB
4151 if ( GetView() )
4152 {
4153 wxGridTableMessage msg( this,
4154 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
4155 pos,
4156 numCols );
8f177c8e 4157
f85afd4e
MB
4158 GetView()->ProcessTableMessage( msg );
4159 }
4160
ca65c044 4161 return true;
f85afd4e
MB
4162}
4163
4164bool wxGridStringTable::AppendCols( size_t numCols )
4165{
27f35b66 4166 size_t row;
f85afd4e
MB
4167
4168 size_t curNumRows = m_data.GetCount();
2f024384 4169
3d59537f 4170 for ( row = 0; row < curNumRows; row++ )
f85afd4e 4171 {
27f35b66 4172 m_data[row].Add( wxEmptyString, numCols );
f85afd4e
MB
4173 }
4174
4175 if ( GetView() )
4176 {
4177 wxGridTableMessage msg( this,
4178 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
4179 numCols );
8f177c8e 4180
f85afd4e
MB
4181 GetView()->ProcessTableMessage( msg );
4182 }
4183
ca65c044 4184 return true;
f85afd4e
MB
4185}
4186
4187bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
4188{
27f35b66 4189 size_t row;
f85afd4e
MB
4190
4191 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
4192 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
4193 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 4194
f85afd4e
MB
4195 if ( pos >= curNumCols )
4196 {
e91d2033
VZ
4197 wxFAIL_MSG( wxString::Format
4198 (
4199 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4200 (unsigned long)pos,
4201 (unsigned long)numCols,
4202 (unsigned long)curNumCols
4203 ) );
ca65c044 4204 return false;
f85afd4e
MB
4205 }
4206
d4175745
VZ
4207 int colID;
4208 if ( GetView() )
4209 colID = GetView()->GetColAt( pos );
4210 else
4211 colID = pos;
4212
4213 if ( numCols > curNumCols - colID )
4214 {
4215 numCols = curNumCols - colID;
4216 }
4217
4218 if ( !m_colLabels.IsEmpty() )
f85afd4e 4219 {
b2df5ddf
VZ
4220 // m_colLabels stores just as many elements as it needs, e.g. if only
4221 // the label of the first column had been set it would have only one
4222 // element and not numCols, so account for it
4223 int nToRm = m_colLabels.size() - colID;
4224 if ( nToRm > 0 )
4225 m_colLabels.RemoveAt( colID, nToRm );
f85afd4e
MB
4226 }
4227
3d59537f 4228 for ( row = 0; row < curNumRows; row++ )
f85afd4e
MB
4229 {
4230 if ( numCols >= curNumCols )
4231 {
dcdce64e 4232 m_data[row].Clear();
f85afd4e
MB
4233 }
4234 else
4235 {
d4175745 4236 m_data[row].RemoveAt( colID, numCols );
f85afd4e
MB
4237 }
4238 }
4db6714b 4239
f85afd4e
MB
4240 if ( GetView() )
4241 {
4242 wxGridTableMessage msg( this,
4243 wxGRIDTABLE_NOTIFY_COLS_DELETED,
4244 pos,
4245 numCols );
8f177c8e 4246
f85afd4e
MB
4247 GetView()->ProcessTableMessage( msg );
4248 }
4249
ca65c044 4250 return true;
f85afd4e
MB
4251}
4252
4253wxString wxGridStringTable::GetRowLabelValue( int row )
4254{
4255 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
4256 {
4257 // using default label
4258 //
4259 return wxGridTableBase::GetRowLabelValue( row );
4260 }
4261 else
4262 {
2f024384 4263 return m_rowLabels[row];
f85afd4e
MB
4264 }
4265}
4266
4267wxString wxGridStringTable::GetColLabelValue( int col )
4268{
4269 if ( col > (int)(m_colLabels.GetCount()) - 1 )
4270 {
4271 // using default label
4272 //
4273 return wxGridTableBase::GetColLabelValue( col );
4274 }
4275 else
4276 {
2f024384 4277 return m_colLabels[col];
f85afd4e
MB
4278 }
4279}
4280
4281void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
4282{
4283 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
4284 {
4285 int n = m_rowLabels.GetCount();
4286 int i;
2f024384 4287
3d59537f 4288 for ( i = n; i <= row; i++ )
f85afd4e
MB
4289 {
4290 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
4291 }
4292 }
4293
4294 m_rowLabels[row] = value;
4295}
4296
4297void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
4298{
4299 if ( col > (int)(m_colLabels.GetCount()) - 1 )
4300 {
4301 int n = m_colLabels.GetCount();
4302 int i;
2f024384 4303
3d59537f 4304 for ( i = n; i <= col; i++ )
f85afd4e
MB
4305 {
4306 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
4307 }
4308 }
4309
4310 m_colLabels[col] = value;
4311}
4312
4313
f85afd4e 4314//////////////////////////////////////////////////////////////////////
2d66e025
MB
4315//////////////////////////////////////////////////////////////////////
4316
86033c4b
VZ
4317BEGIN_EVENT_TABLE(wxGridSubwindow, wxWindow)
4318 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost)
4319END_EVENT_TABLE()
4320
4321void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
4322{
4323 m_owner->CancelMouseCapture();
4324}
4325
86033c4b 4326BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxGridSubwindow )
2d66e025 4327 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
a9339fe2 4328 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel )
2d66e025 4329 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
2d66e025
MB
4330END_EVENT_TABLE()
4331
aa5e1f75 4332void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
4333{
4334 wxPaintDC dc(this);
4335
4336 // NO - don't do this because it will set both the x and y origin
4337 // coords to match the parent scrolled window and we just want to
4338 // set the y coord - MB
4339 //
4340 // m_owner->PrepareDC( dc );
60ff3b99 4341
790ad94f 4342 int x, y;
2d66e025 4343 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
615b7e6a
RR
4344 wxPoint pt = dc.GetDeviceOrigin();
4345 dc.SetDeviceOrigin( pt.x, pt.y-y );
60ff3b99 4346
d10f4bf9 4347 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
a9339fe2 4348 m_owner->DrawRowLabels( dc, rows );
2d66e025
MB
4349}
4350
2d66e025
MB
4351void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
4352{
4353 m_owner->ProcessRowLabelMouseEvent( event );
4354}
4355
b51c3f27
RD
4356void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
4357{
b5e9cbb9
FM
4358 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
4359 event.Skip();
b51c3f27
RD
4360}
4361
2d66e025
MB
4362//////////////////////////////////////////////////////////////////////
4363
86033c4b 4364BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxGridSubwindow )
2d66e025 4365 EVT_PAINT( wxGridColLabelWindow::OnPaint )
a9339fe2 4366 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel )
2d66e025 4367 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
2d66e025
MB
4368END_EVENT_TABLE()
4369
aa5e1f75 4370void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
4371{
4372 wxPaintDC dc(this);
4373
4374 // NO - don't do this because it will set both the x and y origin
4375 // coords to match the parent scrolled window and we just want to
4376 // set the x coord - MB
4377 //
4378 // m_owner->PrepareDC( dc );
60ff3b99 4379
790ad94f 4380 int x, y;
2d66e025 4381 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
615b7e6a
RR
4382 wxPoint pt = dc.GetDeviceOrigin();
4383 if (GetLayoutDirection() == wxLayout_RightToLeft)
4384 dc.SetDeviceOrigin( pt.x+x, pt.y );
4385 else
4386 dc.SetDeviceOrigin( pt.x-x, pt.y );
2d66e025 4387
d10f4bf9 4388 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
a9339fe2 4389 m_owner->DrawColLabels( dc, cols );
2d66e025
MB
4390}
4391
2d66e025
MB
4392void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
4393{
4394 m_owner->ProcessColLabelMouseEvent( event );
4395}
4396
b51c3f27
RD
4397void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
4398{
b5e9cbb9
FM
4399 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
4400 event.Skip();
b51c3f27
RD
4401}
4402
2d66e025
MB
4403//////////////////////////////////////////////////////////////////////
4404
86033c4b 4405BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxGridSubwindow )
a9339fe2 4406 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel )
2d66e025 4407 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
a9339fe2 4408 EVT_PAINT( wxGridCornerLabelWindow::OnPaint )
2d66e025
MB
4409END_EVENT_TABLE()
4410
d2fdd8d2
RR
4411void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
4412{
4413 wxPaintDC dc(this);
b99be8fb 4414
ff72f628 4415 m_owner->DrawCornerLabel(dc);
d2fdd8d2
RR
4416}
4417
2d66e025
MB
4418void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
4419{
4420 m_owner->ProcessCornerLabelMouseEvent( event );
4421}
4422
b51c3f27
RD
4423void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
4424{
b5e9cbb9
FM
4425 if (!m_owner->GetEventHandler()->ProcessEvent(event))
4426 event.Skip();
b51c3f27
RD
4427}
4428
f85afd4e
MB
4429//////////////////////////////////////////////////////////////////////
4430
86033c4b 4431BEGIN_EVENT_TABLE( wxGridWindow, wxGridSubwindow )
2d66e025 4432 EVT_PAINT( wxGridWindow::OnPaint )
a9339fe2 4433 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel )
2d66e025
MB
4434 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
4435 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 4436 EVT_KEY_UP( wxGridWindow::OnKeyUp )
a9339fe2 4437 EVT_CHAR( wxGridWindow::OnChar )
80acaf25
JS
4438 EVT_SET_FOCUS( wxGridWindow::OnFocus )
4439 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
2796cce3 4440 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
4441END_EVENT_TABLE()
4442
2d66e025
MB
4443void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
4444{
4445 wxPaintDC dc( this );
4446 m_owner->PrepareDC( dc );
796df70a 4447 wxRegion reg = GetUpdateRegion();
ccdee36f
DS
4448 wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg );
4449 m_owner->DrawGridCellArea( dc, dirtyCells );
2f024384 4450
9f7aee01
VZ
4451 m_owner->DrawGridSpace( dc );
4452
796df70a 4453 m_owner->DrawAllGridLines( dc, reg );
2f024384 4454
ccdee36f 4455 m_owner->DrawHighlight( dc, dirtyCells );
2d66e025
MB
4456}
4457
2d66e025
MB
4458void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
4459{
59ddac01 4460 wxWindow::ScrollWindow( dx, dy, rect );
ad805b9e
VZ
4461 m_owner->GetGridRowLabelWindow()->ScrollWindow( 0, dy, rect );
4462 m_owner->GetGridColLabelWindow()->ScrollWindow( dx, 0, rect );
2d66e025
MB
4463}
4464
2d66e025
MB
4465void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
4466{
33e9fc54
RD
4467 if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
4468 SetFocus();
902725ee 4469
2d66e025
MB
4470 m_owner->ProcessGridCellMouseEvent( event );
4471}
4472
b51c3f27
RD
4473void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
4474{
b5e9cbb9
FM
4475 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
4476 event.Skip();
b51c3f27 4477}
2d66e025 4478
f6bcfd97 4479// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
4480// cursor must be in the cell edit control to get key events
4481//
4482void wxGridWindow::OnKeyDown( wxKeyEvent& event )
4483{
4db6714b
KH
4484 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4485 event.Skip();
2d66e025 4486}
f85afd4e 4487
f6bcfd97
BP
4488void wxGridWindow::OnKeyUp( wxKeyEvent& event )
4489{
4db6714b
KH
4490 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4491 event.Skip();
f6bcfd97 4492}
7c8a8ad5 4493
63e2147c
RD
4494void wxGridWindow::OnChar( wxKeyEvent& event )
4495{
4db6714b
KH
4496 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4497 event.Skip();
63e2147c
RD
4498}
4499
aa5e1f75 4500void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 4501{
8dd4f536 4502}
025562fe 4503
80acaf25
JS
4504void wxGridWindow::OnFocus(wxFocusEvent& event)
4505{
760be3f7
VS
4506 // and if we have any selection, it has to be repainted, because it
4507 // uses different colour when the grid is not focused:
4508 if ( m_owner->IsSelection() )
4509 {
4510 Refresh();
4511 }
4512 else
4513 {
4514 // NB: Note that this code is in "else" branch only because the other
4515 // branch refreshes everything and so there's no point in calling
4516 // Refresh() again, *not* because it should only be done if
4517 // !IsSelection(). If the above code is ever optimized to refresh
4518 // only selected area, this needs to be moved out of the "else"
4519 // branch so that it's always executed.
4520
4521 // current cell cursor {dis,re}appears on focus change:
4522 const wxGridCellCoords cursorCoords(m_owner->GetGridCursorRow(),
4523 m_owner->GetGridCursorCol());
4524 const wxRect cursor =
4525 m_owner->BlockToDeviceRect(cursorCoords, cursorCoords);
4526 Refresh(true, &cursor);
4527 }
4528
80acaf25
JS
4529 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4530 event.Skip();
4531}
2d66e025 4532
d4175745 4533#define internalXToCol(x) XToCol(x, true)
bec70262 4534#define internalYToRow(y) YToRow(y, true)
ccdee36f 4535
33188aa4 4536/////////////////////////////////////////////////////////////////////
07296f0b 4537
b0a877ec 4538#if wxUSE_EXTENDED_RTTI
73c36334
JS
4539WX_DEFINE_FLAGS( wxGridStyle )
4540
3ff066a4 4541wxBEGIN_FLAGS( wxGridStyle )
73c36334
JS
4542 // new style border flags, we put them first to
4543 // use them for streaming out
3ff066a4
SC
4544 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
4545 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
4546 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
4547 wxFLAGS_MEMBER(wxBORDER_RAISED)
4548 wxFLAGS_MEMBER(wxBORDER_STATIC)
4549 wxFLAGS_MEMBER(wxBORDER_NONE)
ca65c044 4550
73c36334 4551 // old style border flags
3ff066a4
SC
4552 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
4553 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
4554 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
4555 wxFLAGS_MEMBER(wxRAISED_BORDER)
4556 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 4557 wxFLAGS_MEMBER(wxBORDER)
73c36334
JS
4558
4559 // standard window styles
3ff066a4
SC
4560 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
4561 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
4562 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
4563 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 4564 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
ccdee36f 4565 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB)
3ff066a4
SC
4566 wxFLAGS_MEMBER(wxVSCROLL)
4567 wxFLAGS_MEMBER(wxHSCROLL)
73c36334 4568
3ff066a4 4569wxEND_FLAGS( wxGridStyle )
73c36334 4570
b0a877ec
SC
4571IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
4572
3ff066a4
SC
4573wxBEGIN_PROPERTIES_TABLE(wxGrid)
4574 wxHIDE_PROPERTY( Children )
af498247 4575 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 4576wxEND_PROPERTIES_TABLE()
b0a877ec 4577
3ff066a4
SC
4578wxBEGIN_HANDLERS_TABLE(wxGrid)
4579wxEND_HANDLERS_TABLE()
b0a877ec 4580
ca65c044 4581wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
b0a877ec
SC
4582
4583/*
ccdee36f 4584 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
b0a877ec
SC
4585*/
4586#else
2d66e025 4587IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
b0a877ec 4588#endif
2d66e025
MB
4589
4590BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
f85afd4e
MB
4591 EVT_PAINT( wxGrid::OnPaint )
4592 EVT_SIZE( wxGrid::OnSize )
f85afd4e 4593 EVT_KEY_DOWN( wxGrid::OnKeyDown )
f6bcfd97 4594 EVT_KEY_UP( wxGrid::OnKeyUp )
63e2147c 4595 EVT_CHAR ( wxGrid::OnChar )
2796cce3 4596 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
f85afd4e 4597END_EVENT_TABLE()
8f177c8e 4598
b0a877ec
SC
4599bool wxGrid::Create(wxWindow *parent, wxWindowID id,
4600 const wxPoint& pos, const wxSize& size,
4601 long style, const wxString& name)
4602{
4603 if (!wxScrolledWindow::Create(parent, id, pos, size,
c2f5b920 4604 style | wxWANTS_CHARS, name))
ca65c044 4605 return false;
b0a877ec 4606
2f024384
DS
4607 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
4608 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE);
b0a877ec 4609
2f024384 4610 Create();
170acdc9 4611 SetInitialSize(size);
72b0a1de 4612 SetScrollRate(m_scrollLineX, m_scrollLineY);
b93aafab 4613 CalcDimensions();
b0a877ec 4614
ca65c044 4615 return true;
b0a877ec
SC
4616}
4617
58dd5b3b
MB
4618wxGrid::~wxGrid()
4619{
e882d288
VZ
4620 if ( m_winCapture )
4621 m_winCapture->ReleaseMouse();
4622
b09b3048
VZ
4623 // Ensure that the editor control is destroyed before the grid is,
4624 // otherwise we crash later when the editor tries to do something with the
4625 // half destroyed grid
4626 HideCellEditControl();
4627
606b005f
JS
4628 // Must do this or ~wxScrollHelper will pop the wrong event handler
4629 SetTargetWindow(this);
0a976765 4630 ClearAttrCache();
39bcce60 4631 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
4632
4633#ifdef DEBUG_ATTR_CACHE
4634 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
4635 wxPrintf(_T("wxGrid attribute cache statistics: "
4636 "total: %u, hits: %u (%u%%)\n"),
4637 total, gs_nAttrCacheHits,
4638 total ? (gs_nAttrCacheHits*100) / total : 0);
4639#endif
4640
86020f7e
VZ
4641 // if we own the table, just delete it, otherwise at least don't leave it
4642 // with dangling view pointer
4643 if ( m_ownTable )
2796cce3 4644 delete m_table;
ecc7aa82 4645 else if ( m_table && m_table->GetView() == this )
86020f7e 4646 m_table->SetView(NULL);
f2d76237
RD
4647
4648 delete m_typeRegistry;
b5808881 4649 delete m_selection;
58dd5b3b
MB
4650}
4651
58dd5b3b
MB
4652//
4653// ----- internal init and update functions
4654//
4655
9950649c
RD
4656// NOTE: If using the default visual attributes works everywhere then this can
4657// be removed as well as the #else cases below.
4658#define _USE_VISATTR 0
4659
58dd5b3b 4660void wxGrid::Create()
f0102d2a 4661{
3d59537f
DS
4662 // create the type registry
4663 m_typeRegistry = new wxGridTypeRegistry;
2c9a89e0 4664
ca65c044 4665 m_cellEditCtrlEnabled = false;
4634a5d6 4666
ccd970b1 4667 m_defaultCellAttr = new wxGridCellAttr();
f2d76237
RD
4668
4669 // Set default cell attributes
ccd970b1 4670 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
19d7140e 4671 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 4672 m_defaultCellAttr->SetFont(GetFont());
4c7277db 4673 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
9950649c
RD
4674 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
4675 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
4676
4677#if _USE_VISATTR
4678 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
4679 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
4680
4681 m_defaultCellAttr->SetTextColour(gva.colFg);
4682 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
ca65c044 4683
9950649c 4684#else
f2d76237 4685 m_defaultCellAttr->SetTextColour(
a756f210 4686 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
f2d76237 4687 m_defaultCellAttr->SetBackgroundColour(
a756f210 4688 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
9950649c 4689#endif
2796cce3 4690
4634a5d6
MB
4691 m_numRows = 0;
4692 m_numCols = 0;
4693 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 4694
f2d76237 4695 // subwindow components that make up the wxGrid
ad805b9e
VZ
4696 m_rowLabelWin = new wxGridRowLabelWindow(this);
4697 CreateColumnWindow();
4698 m_cornerLabelWin = new wxGridCornerLabelWindow(this);
4699 m_gridWin = new wxGridWindow( this );
2d66e025
MB
4700
4701 SetTargetWindow( m_gridWin );
6f36917b 4702
9950649c
RD
4703#if _USE_VISATTR
4704 wxColour gfg = gva.colFg;
4705 wxColour gbg = gva.colBg;
4706 wxColour lfg = lva.colFg;
4707 wxColour lbg = lva.colBg;
4708#else
4709 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4710 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
4711 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4712 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
4713#endif
2f024384 4714
fa47d7a7
VS
4715 m_cornerLabelWin->SetOwnForegroundColour(lfg);
4716 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
4717 m_rowLabelWin->SetOwnForegroundColour(lfg);
4718 m_rowLabelWin->SetOwnBackgroundColour(lbg);
ad805b9e
VZ
4719 m_colWindow->SetOwnForegroundColour(lfg);
4720 m_colWindow->SetOwnBackgroundColour(lbg);
fa47d7a7
VS
4721
4722 m_gridWin->SetOwnForegroundColour(gfg);
4723 m_gridWin->SetOwnBackgroundColour(gbg);
ca65c044 4724
ad805b9e
VZ
4725 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4726 m_labelTextColour = m_rowLabelWin->GetForegroundColour();
4727
4728 // now that we have the grid window, use its font to compute the default
4729 // row height
4730 m_defaultRowHeight = m_gridWin->GetCharHeight();
4731#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4732 m_defaultRowHeight += 8;
4733#else
4734 m_defaultRowHeight += 4;
4735#endif
4736
4737}
4738
4739void wxGrid::CreateColumnWindow()
4740{
4741 if ( m_useNativeHeader )
4742 {
4743 m_colWindow = new wxGridHeaderCtrl(this);
4744 m_colLabelHeight = m_colWindow->GetBestSize().y;
4745 }
4746 else // draw labels ourselves
4747 {
4748 m_colWindow = new wxGridColLabelWindow(this);
4749 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4750 }
2d66e025 4751}
f85afd4e 4752
b5808881 4753bool wxGrid::CreateGrid( int numRows, int numCols,
6b992f7d 4754 wxGridSelectionModes selmode )
2d66e025 4755{
f6bcfd97 4756 wxCHECK_MSG( !m_created,
ca65c044 4757 false,
f6bcfd97
BP
4758 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4759
6b992f7d 4760 return SetTable(new wxGridStringTable(numRows, numCols), true, selmode);
2796cce3
RD
4761}
4762
6b992f7d 4763void wxGrid::SetSelectionMode(wxGridSelectionModes selmode)
f1567cdd 4764{
6f36917b
VZ
4765 wxCHECK_RET( m_created,
4766 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4767
4768 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
4769}
4770
aa5b8857
SN
4771wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4772{
6b992f7d 4773 wxCHECK_MSG( m_created, wxGridSelectCells,
aa5b8857
SN
4774 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4775
4776 return m_selection->GetSelectionMode();
4777}
4778
6b992f7d
VZ
4779bool
4780wxGrid::SetTable(wxGridTableBase *table,
4781 bool takeOwnership,
4782 wxGrid::wxGridSelectionModes selmode )
2796cce3 4783{
a7dde52f 4784 bool checkSelection = false;
2796cce3
RD
4785 if ( m_created )
4786 {
3e13956a 4787 // stop all processing
ca65c044 4788 m_created = false;
86c7378f 4789
c71b2126 4790 if (m_table)
86c7378f 4791 {
a7dde52f
SN
4792 m_table->SetView(0);
4793 if( m_ownTable )
4794 delete m_table;
5b061713 4795 m_table = NULL;
86c7378f 4796 }
2f024384 4797
3e13956a 4798 delete m_selection;
5b061713 4799 m_selection = NULL;
a7dde52f
SN
4800
4801 m_ownTable = false;
2f024384
DS
4802 m_numRows = 0;
4803 m_numCols = 0;
a7dde52f
SN
4804 checkSelection = true;
4805
4806 // kill row and column size arrays
4807 m_colWidths.Empty();
4808 m_colRights.Empty();
4809 m_rowHeights.Empty();
4810 m_rowBottoms.Empty();
233a54f6 4811 }
2f024384 4812
233a54f6 4813 if (table)
2796cce3
RD
4814 {
4815 m_numRows = table->GetNumberRows();
4816 m_numCols = table->GetNumberCols();
4817
ad805b9e 4818 if ( m_useNativeHeader )
3039ade9 4819 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e 4820
2796cce3
RD
4821 m_table = table;
4822 m_table->SetView( this );
8fc856de 4823 m_ownTable = takeOwnership;
043d16b2 4824 m_selection = new wxGridSelection( this, selmode );
a7dde52f
SN
4825 if (checkSelection)
4826 {
4827 // If the newly set table is smaller than the
4828 // original one current cell and selection regions
4829 // might be invalid,
8a3e536c 4830 m_selectedBlockCorner = wxGridNoCellCoords;
c71b2126 4831 m_currentCellCoords =
a7dde52f
SN
4832 wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()),
4833 wxMin(m_numCols, m_currentCellCoords.GetCol()));
8a3e536c
VZ
4834 if (m_selectedBlockTopLeft.GetRow() >= m_numRows ||
4835 m_selectedBlockTopLeft.GetCol() >= m_numCols)
a7dde52f 4836 {
8a3e536c
VZ
4837 m_selectedBlockTopLeft = wxGridNoCellCoords;
4838 m_selectedBlockBottomRight = wxGridNoCellCoords;
a7dde52f
SN
4839 }
4840 else
8a3e536c 4841 m_selectedBlockBottomRight =
a7dde52f 4842 wxGridCellCoords(wxMin(m_numRows,
8a3e536c 4843 m_selectedBlockBottomRight.GetRow()),
a7dde52f 4844 wxMin(m_numCols,
8a3e536c 4845 m_selectedBlockBottomRight.GetCol()));
a7dde52f 4846 }
6f36917b
VZ
4847 CalcDimensions();
4848
ca65c044 4849 m_created = true;
2d66e025 4850 }
f85afd4e 4851
2d66e025 4852 return m_created;
f85afd4e
MB
4853}
4854
ad805b9e 4855void wxGrid::Init()
f9549841
VZ
4856{
4857 m_created = false;
4858
4859 m_cornerLabelWin = NULL;
4860 m_rowLabelWin = NULL;
ad805b9e 4861 m_colWindow = NULL;
f9549841
VZ
4862 m_gridWin = NULL;
4863
4864 m_table = NULL;
4865 m_ownTable = false;
4866
4867 m_selection = NULL;
4868 m_defaultCellAttr = NULL;
4869 m_typeRegistry = NULL;
4870 m_winCapture = NULL;
f9549841 4871
f85afd4e
MB
4872 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4873 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4874
0a976765
VZ
4875 // init attr cache
4876 m_attrCache.row = -1;
2b5f62a0
VZ
4877 m_attrCache.col = -1;
4878 m_attrCache.attr = NULL;
0a976765 4879
ff72f628 4880 m_labelFont = GetFont();
52d6f640 4881 m_labelFont.SetWeight( wxBOLD );
8f177c8e 4882
73145b0e 4883 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4c7277db 4884 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 4885
4c7277db 4886 m_colLabelHorizAlign = wxALIGN_CENTRE;
73145b0e 4887 m_colLabelVertAlign = wxALIGN_CENTRE;
d43851f7 4888 m_colLabelTextOrientation = wxHORIZONTAL;
f85afd4e 4889
f85afd4e 4890 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
ad805b9e 4891 m_defaultRowHeight = 0; // this will be initialized after creation
1f1ce288 4892
b8d24d4e
RG
4893 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4894 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4895
73145b0e 4896 m_gridLineColour = wxColour( 192,192,192 );
ca65c044 4897 m_gridLinesEnabled = true;
9f7aee01
VZ
4898 m_gridLinesClipHorz =
4899 m_gridLinesClipVert = true;
73145b0e 4900 m_cellHighlightColour = *wxBLACK;
bf7945ce 4901 m_cellHighlightPenWidth = 2;
d2520c85 4902 m_cellHighlightROPenWidth = 1;
8f177c8e 4903
d4175745
VZ
4904 m_canDragColMove = false;
4905
58dd5b3b 4906 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
10a4531d 4907 m_winCapture = NULL;
ca65c044
WS
4908 m_canDragRowSize = true;
4909 m_canDragColSize = true;
4910 m_canDragGridSize = true;
79dbea21 4911 m_canDragCell = false;
f85afd4e
MB
4912 m_dragLastPos = -1;
4913 m_dragRowOrCol = -1;
ca65c044 4914 m_isDragging = false;
07296f0b 4915 m_startDragPos = wxDefaultPosition;
ad805b9e 4916
11393d29
VZ
4917 m_sortCol = wxNOT_FOUND;
4918 m_sortIsAscending = true;
4919
ad805b9e 4920 m_useNativeHeader =
71cf399f 4921 m_nativeColumnLabels = false;
f85afd4e 4922
ca65c044 4923 m_waitForSlowClick = false;
025562fe 4924
f85afd4e
MB
4925 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4926 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4927
4928 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 4929
ad805b9e
VZ
4930 m_selectedBlockTopLeft =
4931 m_selectedBlockBottomRight =
4932 m_selectedBlockCorner = wxGridNoCellCoords;
b524b5c6 4933
d43851f7
JS
4934 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4935 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 4936
ca65c044 4937 m_editable = true; // default for whole grid
f85afd4e 4938
ca65c044 4939 m_inOnKeyDown = false;
2d66e025 4940 m_batchCount = 0;
3c79cf49 4941
266e8367 4942 m_extraWidth =
526dbb95 4943 m_extraHeight = 0;
608754c4
JS
4944
4945 m_scrollLineX = GRID_SCROLL_LINE_X;
4946 m_scrollLineY = GRID_SCROLL_LINE_Y;
7c1cb261
VZ
4947}
4948
4949// ----------------------------------------------------------------------------
4950// the idea is to call these functions only when necessary because they create
4951// quite big arrays which eat memory mostly unnecessary - in particular, if
4952// default widths/heights are used for all rows/columns, we may not use these
4953// arrays at all
4954//
0ed3b812
VZ
4955// with some extra code, it should be possible to only store the widths/heights
4956// different from default ones (resulting in space savings for huge grids) but
4957// this is not done currently
7c1cb261
VZ
4958// ----------------------------------------------------------------------------
4959
4960void wxGrid::InitRowHeights()
4961{
4962 m_rowHeights.Empty();
4963 m_rowBottoms.Empty();
4964
4965 m_rowHeights.Alloc( m_numRows );
4966 m_rowBottoms.Alloc( m_numRows );
4967
27f35b66
SN
4968 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4969
0ed3b812 4970 int rowBottom = 0;
3d59537f 4971 for ( int i = 0; i < m_numRows; i++ )
7c1cb261 4972 {
7c1cb261
VZ
4973 rowBottom += m_defaultRowHeight;
4974 m_rowBottoms.Add( rowBottom );
4975 }
4976}
4977
4978void wxGrid::InitColWidths()
4979{
4980 m_colWidths.Empty();
4981 m_colRights.Empty();
4982
4983 m_colWidths.Alloc( m_numCols );
4984 m_colRights.Alloc( m_numCols );
27f35b66
SN
4985
4986 m_colWidths.Add( m_defaultColWidth, m_numCols );
4987
3d59537f 4988 for ( int i = 0; i < m_numCols; i++ )
7c1cb261 4989 {
e822d1bd 4990 int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
7c1cb261
VZ
4991 m_colRights.Add( colRight );
4992 }
4993}
4994
4995int wxGrid::GetColWidth(int col) const
4996{
4997 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4998}
4999
5000int wxGrid::GetColLeft(int col) const
5001{
d4175745 5002 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
7c1cb261
VZ
5003 : m_colRights[col] - m_colWidths[col];
5004}
5005
5006int wxGrid::GetColRight(int col) const
5007{
d4175745 5008 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
7c1cb261
VZ
5009 : m_colRights[col];
5010}
5011
5012int wxGrid::GetRowHeight(int row) const
5013{
5014 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
5015}
2d66e025 5016
7c1cb261
VZ
5017int wxGrid::GetRowTop(int row) const
5018{
5019 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
5020 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
5021}
5022
7c1cb261
VZ
5023int wxGrid::GetRowBottom(int row) const
5024{
5025 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
5026 : m_rowBottoms[row];
5027}
f85afd4e
MB
5028
5029void wxGrid::CalcDimensions()
5030{
0ed3b812
VZ
5031 // compute the size of the scrollable area
5032 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
5033 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
60ff3b99 5034
0ed3b812
VZ
5035 w += m_extraWidth;
5036 h += m_extraHeight;
faec5a43 5037
73145b0e 5038 // take into account editor if shown
4db6714b 5039 if ( IsCellEditControlShown() )
73145b0e 5040 {
3d59537f
DS
5041 int w2, h2;
5042 int r = m_currentCellCoords.GetRow();
5043 int c = m_currentCellCoords.GetCol();
5044 int x = GetColLeft(c);
5045 int y = GetRowTop(r);
5046
5047 // how big is the editor
5048 wxGridCellAttr* attr = GetCellAttr(r, c);
5049 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
5050 editor->GetControl()->GetSize(&w2, &h2);
5051 w2 += x;
5052 h2 += y;
5053 if ( w2 > w )
5054 w = w2;
5055 if ( h2 > h )
5056 h = h2;
5057 editor->DecRef();
5058 attr->DecRef();
73145b0e
JS
5059 }
5060
faec5a43
SN
5061 // preserve (more or less) the previous position
5062 int x, y;
5063 GetViewStart( &x, &y );
97a9929e 5064
c92ed9f7 5065 // ensure the position is valid for the new scroll ranges
7b519e5e 5066 if ( x >= w )
c92ed9f7 5067 x = wxMax( w - 1, 0 );
7b519e5e 5068 if ( y >= h )
c92ed9f7 5069 y = wxMax( h - 1, 0 );
faec5a43 5070
ace8d849 5071 // update the virtual size and refresh the scrollbars to reflect it
f1ff7df0
VZ
5072 m_gridWin->SetVirtualSize(w, h);
5073 Scroll(x, y);
ace8d849 5074 AdjustScrollbars();
12314291
VZ
5075
5076 // if our OnSize() hadn't been called (it would if we have scrollbars), we
5077 // still must reposition the children
5078 CalcWindowSizes();
f85afd4e
MB
5079}
5080
69367c56
VZ
5081wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size)
5082{
5083 wxSize sizeGridWin(size);
5084 sizeGridWin.x -= m_rowLabelWidth;
5085 sizeGridWin.y -= m_colLabelHeight;
5086
5087 return sizeGridWin;
5088}
5089
7807d81c
MB
5090void wxGrid::CalcWindowSizes()
5091{
b0a877ec
SC
5092 // escape if the window is has not been fully created yet
5093
5094 if ( m_cornerLabelWin == NULL )
2f024384 5095 return;
b0a877ec 5096
7807d81c
MB
5097 int cw, ch;
5098 GetClientSize( &cw, &ch );
b99be8fb 5099
c1841ac2
VZ
5100 // the grid may be too small to have enough space for the labels yet, don't
5101 // size the windows to negative sizes in this case
5102 int gw = cw - m_rowLabelWidth;
5103 int gh = ch - m_colLabelHeight;
5104 if (gw < 0)
5105 gw = 0;
5106 if (gh < 0)
5107 gh = 0;
5108
6d308072 5109 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
7807d81c
MB
5110 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
5111
ad805b9e
VZ
5112 if ( m_colWindow && m_colWindow->IsShown() )
5113 m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight );
7807d81c 5114
6d308072 5115 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
c1841ac2 5116 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh );
7807d81c 5117
6d308072 5118 if ( m_gridWin && m_gridWin->IsShown() )
c1841ac2 5119 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh );
7807d81c
MB
5120}
5121
3d59537f
DS
5122// this is called when the grid table sends a message
5123// to indicate that it has been redimensioned
f85afd4e
MB
5124//
5125bool wxGrid::Redimension( wxGridTableMessage& msg )
5126{
5127 int i;
ca65c044 5128 bool result = false;
8f177c8e 5129
a6794685
SN
5130 // Clear the attribute cache as the attribute might refer to a different
5131 // cell than stored in the cache after adding/removing rows/columns.
5132 ClearAttrCache();
2f024384 5133
7e48d7d9
SN
5134 // By the same reasoning, the editor should be dismissed if columns are
5135 // added or removed. And for consistency, it should IMHO always be
5136 // removed, not only if the cell "underneath" it actually changes.
5137 // For now, I intentionally do not save the editor's content as the
5138 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 5139 HideCellEditControl();
2f024384 5140
f85afd4e
MB
5141 switch ( msg.GetId() )
5142 {
5143 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5144 {
5145 size_t pos = msg.GetCommandInt();
5146 int numRows = msg.GetCommandInt2();
f6bcfd97 5147
f85afd4e 5148 m_numRows += numRows;
2d66e025 5149
f6bcfd97
BP
5150 if ( !m_rowHeights.IsEmpty() )
5151 {
27f35b66
SN
5152 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
5153 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
5154
5155 int bottom = 0;
2f024384
DS
5156 if ( pos > 0 )
5157 bottom = m_rowBottoms[pos - 1];
60ff3b99 5158
3d59537f 5159 for ( i = pos; i < m_numRows; i++ )
f6bcfd97
BP
5160 {
5161 bottom += m_rowHeights[i];
5162 m_rowBottoms[i] = bottom;
5163 }
5164 }
2f024384 5165
f6bcfd97
BP
5166 if ( m_currentCellCoords == wxGridNoCellCoords )
5167 {
5168 // if we have just inserted cols into an empty grid the current
5169 // cell will be undefined...
5170 //
5171 SetCurrentCell( 0, 0 );
5172 }
3f3dc2ef
VZ
5173
5174 if ( m_selection )
5175 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
5176 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
5177 if (attrProvider)
5178 attrProvider->UpdateAttrRows( pos, numRows );
5179
5180 if ( !GetBatchCount() )
2d66e025 5181 {
f6bcfd97
BP
5182 CalcDimensions();
5183 m_rowLabelWin->Refresh();
2d66e025 5184 }
f85afd4e 5185 }
ca65c044 5186 result = true;
f6bcfd97 5187 break;
f85afd4e
MB
5188
5189 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5190 {
5191 int numRows = msg.GetCommandInt();
2d66e025 5192 int oldNumRows = m_numRows;
f85afd4e 5193 m_numRows += numRows;
2d66e025 5194
f6bcfd97
BP
5195 if ( !m_rowHeights.IsEmpty() )
5196 {
27f35b66
SN
5197 m_rowHeights.Add( m_defaultRowHeight, numRows );
5198 m_rowBottoms.Add( 0, numRows );
60ff3b99 5199
f6bcfd97 5200 int bottom = 0;
2f024384
DS
5201 if ( oldNumRows > 0 )
5202 bottom = m_rowBottoms[oldNumRows - 1];
f6bcfd97 5203
3d59537f 5204 for ( i = oldNumRows; i < m_numRows; i++ )
f6bcfd97
BP
5205 {
5206 bottom += m_rowHeights[i];
5207 m_rowBottoms[i] = bottom;
5208 }
5209 }
2f024384 5210
f6bcfd97
BP
5211 if ( m_currentCellCoords == wxGridNoCellCoords )
5212 {
5213 // if we have just inserted cols into an empty grid the current
5214 // cell will be undefined...
5215 //
5216 SetCurrentCell( 0, 0 );
5217 }
2f024384 5218
f6bcfd97 5219 if ( !GetBatchCount() )
2d66e025 5220 {
f6bcfd97
BP
5221 CalcDimensions();
5222 m_rowLabelWin->Refresh();
2d66e025 5223 }
f85afd4e 5224 }
ca65c044 5225 result = true;
f6bcfd97 5226 break;
f85afd4e
MB
5227
5228 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5229 {
5230 size_t pos = msg.GetCommandInt();
5231 int numRows = msg.GetCommandInt2();
f85afd4e
MB
5232 m_numRows -= numRows;
5233
f6bcfd97 5234 if ( !m_rowHeights.IsEmpty() )
f85afd4e 5235 {
27f35b66
SN
5236 m_rowHeights.RemoveAt( pos, numRows );
5237 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
5238
5239 int h = 0;
3d59537f 5240 for ( i = 0; i < m_numRows; i++ )
2d66e025
MB
5241 {
5242 h += m_rowHeights[i];
5243 m_rowBottoms[i] = h;
5244 }
f85afd4e 5245 }
3d59537f 5246
f6bcfd97
BP
5247 if ( !m_numRows )
5248 {
5249 m_currentCellCoords = wxGridNoCellCoords;
5250 }
5251 else
5252 {
5253 if ( m_currentCellCoords.GetRow() >= m_numRows )
5254 m_currentCellCoords.Set( 0, 0 );
5255 }
3f3dc2ef
VZ
5256
5257 if ( m_selection )
5258 m_selection->UpdateRows( pos, -((int)numRows) );
f6bcfd97 5259 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
5260 if (attrProvider)
5261 {
f6bcfd97 5262 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
3d59537f 5263
84912ef8
RD
5264// ifdef'd out following patch from Paul Gammans
5265#if 0
3ca6a5f0 5266 // No need to touch column attributes, unless we
f6bcfd97
BP
5267 // removed _all_ rows, in this case, we remove
5268 // all column attributes.
5269 // I hate to do this here, but the
5270 // needed data is not available inside UpdateAttrRows.
5271 if ( !GetNumberRows() )
5272 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 5273#endif
f6bcfd97 5274 }
ccdee36f 5275
f6bcfd97
BP
5276 if ( !GetBatchCount() )
5277 {
5278 CalcDimensions();
5279 m_rowLabelWin->Refresh();
5280 }
f85afd4e 5281 }
ca65c044 5282 result = true;
f6bcfd97 5283 break;
f85afd4e
MB
5284
5285 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5286 {
5287 size_t pos = msg.GetCommandInt();
5288 int numCols = msg.GetCommandInt2();
f85afd4e 5289 m_numCols += numCols;
2d66e025 5290
ad805b9e 5291 if ( m_useNativeHeader )
3039ade9 5292 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e 5293
d4175745
VZ
5294 if ( !m_colAt.IsEmpty() )
5295 {
5296 //Shift the column IDs
5297 int i;
5298 for ( i = 0; i < m_numCols - numCols; i++ )
5299 {
5300 if ( m_colAt[i] >= (int)pos )
5301 m_colAt[i] += numCols;
5302 }
5303
5304 m_colAt.Insert( pos, pos, numCols );
5305
5306 //Set the new columns' positions
5307 for ( i = pos + 1; i < (int)pos + numCols; i++ )
5308 {
5309 m_colAt[i] = i;
5310 }
5311 }
5312
f6bcfd97
BP
5313 if ( !m_colWidths.IsEmpty() )
5314 {
27f35b66
SN
5315 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
5316 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
5317
5318 int right = 0;
2f024384 5319 if ( pos > 0 )
d4175745 5320 right = m_colRights[GetColAt( pos - 1 )];
60ff3b99 5321
d4175745
VZ
5322 int colPos;
5323 for ( colPos = pos; colPos < m_numCols; colPos++ )
f6bcfd97 5324 {
d4175745
VZ
5325 i = GetColAt( colPos );
5326
f6bcfd97
BP
5327 right += m_colWidths[i];
5328 m_colRights[i] = right;
5329 }
5330 }
2f024384 5331
f6bcfd97 5332 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 5333 {
f6bcfd97
BP
5334 // if we have just inserted cols into an empty grid the current
5335 // cell will be undefined...
5336 //
5337 SetCurrentCell( 0, 0 );
2d66e025 5338 }
3f3dc2ef
VZ
5339
5340 if ( m_selection )
5341 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
5342 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
5343 if (attrProvider)
5344 attrProvider->UpdateAttrCols( pos, numCols );
5345 if ( !GetBatchCount() )
5346 {
5347 CalcDimensions();
ad805b9e 5348 m_colWindow->Refresh();
f6bcfd97 5349 }
f85afd4e 5350 }
ca65c044 5351 result = true;
f6bcfd97 5352 break;
f85afd4e
MB
5353
5354 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5355 {
5356 int numCols = msg.GetCommandInt();
2d66e025 5357 int oldNumCols = m_numCols;
f85afd4e 5358 m_numCols += numCols;
ad805b9e 5359 if ( m_useNativeHeader )
3039ade9 5360 GetGridColHeader()->SetColumnCount(m_numCols);
d4175745
VZ
5361
5362 if ( !m_colAt.IsEmpty() )
5363 {
5364 m_colAt.Add( 0, numCols );
5365
5366 //Set the new columns' positions
5367 int i;
5368 for ( i = oldNumCols; i < m_numCols; i++ )
5369 {
5370 m_colAt[i] = i;
5371 }
5372 }
5373
f6bcfd97
BP
5374 if ( !m_colWidths.IsEmpty() )
5375 {
27f35b66
SN
5376 m_colWidths.Add( m_defaultColWidth, numCols );
5377 m_colRights.Add( 0, numCols );
2d66e025 5378
f6bcfd97 5379 int right = 0;
2f024384 5380 if ( oldNumCols > 0 )
d4175745 5381 right = m_colRights[GetColAt( oldNumCols - 1 )];
60ff3b99 5382
d4175745
VZ
5383 int colPos;
5384 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
f6bcfd97 5385 {
d4175745
VZ
5386 i = GetColAt( colPos );
5387
f6bcfd97
BP
5388 right += m_colWidths[i];
5389 m_colRights[i] = right;
5390 }
5391 }
2f024384 5392
f6bcfd97 5393 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 5394 {
f6bcfd97
BP
5395 // if we have just inserted cols into an empty grid the current
5396 // cell will be undefined...
5397 //
5398 SetCurrentCell( 0, 0 );
5399 }
5400 if ( !GetBatchCount() )
5401 {
5402 CalcDimensions();
ad805b9e 5403 m_colWindow->Refresh();
2d66e025 5404 }
f85afd4e 5405 }
ca65c044 5406 result = true;
f6bcfd97 5407 break;
f85afd4e
MB
5408
5409 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5410 {
5411 size_t pos = msg.GetCommandInt();
5412 int numCols = msg.GetCommandInt2();
f85afd4e 5413 m_numCols -= numCols;
ad805b9e 5414 if ( m_useNativeHeader )
3039ade9 5415 GetGridColHeader()->SetColumnCount(m_numCols);
f85afd4e 5416
d4175745
VZ
5417 if ( !m_colAt.IsEmpty() )
5418 {
5419 int colID = GetColAt( pos );
5420
5421 m_colAt.RemoveAt( pos, numCols );
5422
5423 //Shift the column IDs
5424 int colPos;
5425 for ( colPos = 0; colPos < m_numCols; colPos++ )
5426 {
5427 if ( m_colAt[colPos] > colID )
5428 m_colAt[colPos] -= numCols;
5429 }
5430 }
5431
f6bcfd97 5432 if ( !m_colWidths.IsEmpty() )
f85afd4e 5433 {
27f35b66
SN
5434 m_colWidths.RemoveAt( pos, numCols );
5435 m_colRights.RemoveAt( pos, numCols );
2d66e025
MB
5436
5437 int w = 0;
d4175745
VZ
5438 int colPos;
5439 for ( colPos = 0; colPos < m_numCols; colPos++ )
2d66e025 5440 {
d4175745
VZ
5441 i = GetColAt( colPos );
5442
2d66e025
MB
5443 w += m_colWidths[i];
5444 m_colRights[i] = w;
5445 }
f85afd4e 5446 }
2f024384 5447
f6bcfd97
BP
5448 if ( !m_numCols )
5449 {
5450 m_currentCellCoords = wxGridNoCellCoords;
5451 }
5452 else
5453 {
5454 if ( m_currentCellCoords.GetCol() >= m_numCols )
5455 m_currentCellCoords.Set( 0, 0 );
5456 }
3f3dc2ef
VZ
5457
5458 if ( m_selection )
5459 m_selection->UpdateCols( pos, -((int)numCols) );
f6bcfd97 5460 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
5461 if (attrProvider)
5462 {
f6bcfd97 5463 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
ccdee36f 5464
84912ef8
RD
5465// ifdef'd out following patch from Paul Gammans
5466#if 0
f6bcfd97
BP
5467 // No need to touch row attributes, unless we
5468 // removed _all_ columns, in this case, we remove
5469 // all row attributes.
5470 // I hate to do this here, but the
5471 // needed data is not available inside UpdateAttrCols.
5472 if ( !GetNumberCols() )
5473 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 5474#endif
f6bcfd97 5475 }
ccdee36f 5476
f6bcfd97
BP
5477 if ( !GetBatchCount() )
5478 {
5479 CalcDimensions();
ad805b9e 5480 m_colWindow->Refresh();
f6bcfd97 5481 }
f85afd4e 5482 }
ca65c044 5483 result = true;
f6bcfd97 5484 break;
f85afd4e
MB
5485 }
5486
f6bcfd97
BP
5487 if (result && !GetBatchCount() )
5488 m_gridWin->Refresh();
2f024384 5489
f6bcfd97 5490 return result;
f85afd4e
MB
5491}
5492
ef316e23 5493wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
f85afd4e 5494{
2d66e025
MB
5495 wxRegionIterator iter( reg );
5496 wxRect r;
f85afd4e 5497
275c4ae4
RD
5498 wxArrayInt rowlabels;
5499
2d66e025
MB
5500 int top, bottom;
5501 while ( iter )
f85afd4e 5502 {
2d66e025 5503 r = iter.GetRect();
f85afd4e 5504
2d66e025
MB
5505 // TODO: remove this when we can...
5506 // There is a bug in wxMotif that gives garbage update
5507 // rectangles if you jump-scroll a long way by clicking the
5508 // scrollbar with middle button. This is a work-around
5509 //
5510#if defined(__WXMOTIF__)
5511 int cw, ch;
5512 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5513 if ( r.GetTop() > ch )
5514 r.SetTop( 0 );
2d66e025
MB
5515 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5516#endif
f85afd4e 5517
2d66e025
MB
5518 // logical bounds of update region
5519 //
5520 int dummy;
5521 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
5522 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
5523
5524 // find the row labels within these bounds
5525 //
5526 int row;
3d59537f 5527 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 5528 {
7c1cb261
VZ
5529 if ( GetRowBottom(row) < top )
5530 continue;
2d66e025 5531
6d55126d 5532 if ( GetRowTop(row) > bottom )
7c1cb261 5533 break;
60ff3b99 5534
d10f4bf9 5535 rowlabels.Add( row );
2d66e025 5536 }
60ff3b99 5537
60d8e886 5538 ++iter;
f85afd4e 5539 }
d10f4bf9
VZ
5540
5541 return rowlabels;
f85afd4e
MB
5542}
5543
ef316e23 5544wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
f85afd4e 5545{
2d66e025
MB
5546 wxRegionIterator iter( reg );
5547 wxRect r;
f85afd4e 5548
d10f4bf9 5549 wxArrayInt colLabels;
f85afd4e 5550
2d66e025
MB
5551 int left, right;
5552 while ( iter )
f85afd4e 5553 {
2d66e025 5554 r = iter.GetRect();
f85afd4e 5555
2d66e025
MB
5556 // TODO: remove this when we can...
5557 // There is a bug in wxMotif that gives garbage update
5558 // rectangles if you jump-scroll a long way by clicking the
5559 // scrollbar with middle button. This is a work-around
5560 //
5561#if defined(__WXMOTIF__)
5562 int cw, ch;
5563 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5564 if ( r.GetLeft() > cw )
5565 r.SetLeft( 0 );
2d66e025
MB
5566 r.SetRight( wxMin( r.GetRight(), cw ) );
5567#endif
5568
5569 // logical bounds of update region
5570 //
5571 int dummy;
5572 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
5573 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
5574
5575 // find the cells within these bounds
5576 //
5577 int col;
d4175745
VZ
5578 int colPos;
5579 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 5580 {
d4175745
VZ
5581 col = GetColAt( colPos );
5582
7c1cb261
VZ
5583 if ( GetColRight(col) < left )
5584 continue;
60ff3b99 5585
7c1cb261
VZ
5586 if ( GetColLeft(col) > right )
5587 break;
2d66e025 5588
d10f4bf9 5589 colLabels.Add( col );
2d66e025 5590 }
60ff3b99 5591
60d8e886 5592 ++iter;
f85afd4e 5593 }
2f024384 5594
d10f4bf9 5595 return colLabels;
f85afd4e
MB
5596}
5597
ef316e23 5598wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
f85afd4e 5599{
2d66e025
MB
5600 wxRegionIterator iter( reg );
5601 wxRect r;
f85afd4e 5602
d10f4bf9 5603 wxGridCellCoordsArray cellsExposed;
f85afd4e 5604
2d66e025
MB
5605 int left, top, right, bottom;
5606 while ( iter )
5607 {
5608 r = iter.GetRect();
f85afd4e 5609
2d66e025
MB
5610 // TODO: remove this when we can...
5611 // There is a bug in wxMotif that gives garbage update
5612 // rectangles if you jump-scroll a long way by clicking the
5613 // scrollbar with middle button. This is a work-around
5614 //
5615#if defined(__WXMOTIF__)
f85afd4e 5616 int cw, ch;
2d66e025
MB
5617 m_gridWin->GetClientSize( &cw, &ch );
5618 if ( r.GetTop() > ch ) r.SetTop( 0 );
5619 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5620 r.SetRight( wxMin( r.GetRight(), cw ) );
5621 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5622#endif
8f177c8e 5623
2d66e025
MB
5624 // logical bounds of update region
5625 //
5626 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5627 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 5628
2d66e025 5629 // find the cells within these bounds
cd4f6f5f
VZ
5630 wxArrayInt cols;
5631 for ( int row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 5632 {
7c1cb261
VZ
5633 if ( GetRowBottom(row) <= top )
5634 continue;
f85afd4e 5635
7c1cb261
VZ
5636 if ( GetRowTop(row) > bottom )
5637 break;
60ff3b99 5638
cd4f6f5f
VZ
5639 // add all dirty cells in this row: notice that the columns which
5640 // are dirty don't depend on the row so we compute them only once
5641 // for the first dirty row and then reuse for all the next ones
5642 if ( cols.empty() )
2d66e025 5643 {
cd4f6f5f
VZ
5644 // do determine the dirty columns
5645 for ( int pos = XToPos(left); pos <= XToPos(right); pos++ )
5646 cols.push_back(GetColAt(pos));
d4175745 5647
cd4f6f5f
VZ
5648 // if there are no dirty columns at all, nothing to do
5649 if ( cols.empty() )
7c1cb261 5650 break;
2d66e025 5651 }
cd4f6f5f
VZ
5652
5653 const size_t count = cols.size();
5654 for ( size_t n = 0; n < count; n++ )
5655 cellsExposed.Add(wxGridCellCoords(row, cols[n]));
2d66e025 5656 }
60ff3b99 5657
60d8e886 5658 ++iter;
f85afd4e 5659 }
d10f4bf9
VZ
5660
5661 return cellsExposed;
f85afd4e
MB
5662}
5663
5664
2d66e025 5665void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5666{
2d66e025
MB
5667 int x, y, row;
5668 wxPoint pos( event.GetPosition() );
5669 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5670
2d66e025 5671 if ( event.Dragging() )
f85afd4e 5672 {
426b2d87
JS
5673 if (!m_isDragging)
5674 {
ca65c044 5675 m_isDragging = true;
426b2d87
JS
5676 m_rowLabelWin->CaptureMouse();
5677 }
8f177c8e 5678
2d66e025 5679 if ( event.LeftIsDown() )
f85afd4e 5680 {
962a48f6 5681 switch ( m_cursorMode )
f85afd4e 5682 {
f85afd4e
MB
5683 case WXGRID_CURSOR_RESIZE_ROW:
5684 {
2d66e025
MB
5685 int cw, ch, left, dummy;
5686 m_gridWin->GetClientSize( &cw, &ch );
5687 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 5688
2d66e025
MB
5689 wxClientDC dc( m_gridWin );
5690 PrepareDC( dc );
af547d51
VZ
5691 y = wxMax( y,
5692 GetRowTop(m_dragRowOrCol) +
5693 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 5694 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
5695 if ( m_dragLastPos >= 0 )
5696 {
2d66e025 5697 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 5698 }
2d66e025
MB
5699 dc.DrawLine( left, y, left+cw, y );
5700 m_dragLastPos = y;
f85afd4e
MB
5701 }
5702 break;
5703
5704 case WXGRID_CURSOR_SELECT_ROW:
902725ee 5705 {
e32352cf 5706 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 5707 {
3f3dc2ef 5708 if ( m_selection )
8b5f6d9d 5709 m_selection->SelectRow(row, event);
f85afd4e 5710 }
902725ee
WS
5711 }
5712 break;
e2b42eeb
VZ
5713
5714 // default label to suppress warnings about "enumeration value
5715 // 'xxx' not handled in switch
5716 default:
5717 break;
f85afd4e
MB
5718 }
5719 }
5720 return;
5721 }
5722
426b2d87
JS
5723 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5724 return;
8f177c8e 5725
426b2d87
JS
5726 if (m_isDragging)
5727 {
ccdee36f
DS
5728 if (m_rowLabelWin->HasCapture())
5729 m_rowLabelWin->ReleaseMouse();
ca65c044 5730 m_isDragging = false;
426b2d87 5731 }
60ff3b99 5732
6d004f67
MB
5733 // ------------ Entering or leaving the window
5734 //
5735 if ( event.Entering() || event.Leaving() )
5736 {
e2b42eeb 5737 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
5738 }
5739
2d66e025 5740 // ------------ Left button pressed
f85afd4e 5741 //
6d004f67 5742 else if ( event.LeftDown() )
f85afd4e 5743 {
2d66e025
MB
5744 // don't send a label click event for a hit on the
5745 // edge of the row label - this is probably the user
5746 // wanting to resize the row
5747 //
5748 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 5749 {
2d66e025 5750 row = YToRow(y);
2f024384 5751 if ( row >= 0 &&
b54ba671 5752 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 5753 {
2b01fd49 5754 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 5755 ClearSelection();
64e15340 5756 if ( m_selection )
3f3dc2ef
VZ
5757 {
5758 if ( event.ShiftDown() )
5759 {
8b5f6d9d
VZ
5760 m_selection->SelectBlock
5761 (
5762 m_currentCellCoords.GetRow(), 0,
5763 row, GetNumberCols() - 1,
5764 event
5765 );
3f3dc2ef
VZ
5766 }
5767 else
5768 {
8b5f6d9d 5769 m_selection->SelectRow(row, event);
3f3dc2ef
VZ
5770 }
5771 }
5772
e2b42eeb 5773 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 5774 }
2d66e025
MB
5775 }
5776 else
5777 {
5778 // starting to drag-resize a row
6e8524b1
MB
5779 if ( CanDragRowSize() )
5780 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
5781 }
5782 }
f85afd4e 5783
2d66e025
MB
5784 // ------------ Left double click
5785 //
5786 else if (event.LeftDClick() )
5787 {
4e115ed2 5788 row = YToEdgeOfRow(y);
d43851f7 5789 if ( row < 0 )
2d66e025
MB
5790 {
5791 row = YToRow(y);
a967f048 5792 if ( row >=0 &&
ca65c044
WS
5793 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5794 {
a967f048 5795 // no default action at the moment
ca65c044 5796 }
f85afd4e 5797 }
d43851f7
JS
5798 else
5799 {
5800 // adjust row height depending on label text
5801 AutoSizeRowLabelSize( row );
5802
ad805b9e 5803 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
ccdee36f 5804 m_dragLastPos = -1;
d43851f7 5805 }
f85afd4e 5806 }
60ff3b99 5807
2d66e025 5808 // ------------ Left button released
f85afd4e 5809 //
2d66e025 5810 else if ( event.LeftUp() )
f85afd4e 5811 {
2d66e025 5812 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 5813 {
6d004f67 5814 DoEndDragResizeRow();
60ff3b99 5815
6d004f67
MB
5816 // Note: we are ending the event *after* doing
5817 // default processing in this case
5818 //
b54ba671 5819 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 5820 }
f85afd4e 5821
e2b42eeb
VZ
5822 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5823 m_dragLastPos = -1;
2d66e025 5824 }
f85afd4e 5825
2d66e025
MB
5826 // ------------ Right button down
5827 //
5828 else if ( event.RightDown() )
5829 {
5830 row = YToRow(y);
ef5df12b 5831 if ( row >=0 &&
ca65c044 5832 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
5833 {
5834 // no default action at the moment
f85afd4e
MB
5835 }
5836 }
60ff3b99 5837
2d66e025 5838 // ------------ Right double click
f85afd4e 5839 //
2d66e025
MB
5840 else if ( event.RightDClick() )
5841 {
5842 row = YToRow(y);
a967f048 5843 if ( row >= 0 &&
ca65c044 5844 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
5845 {
5846 // no default action at the moment
5847 }
5848 }
60ff3b99 5849
2d66e025 5850 // ------------ No buttons down and mouse moving
f85afd4e 5851 //
2d66e025 5852 else if ( event.Moving() )
f85afd4e 5853 {
2d66e025
MB
5854 m_dragRowOrCol = YToEdgeOfRow( y );
5855 if ( m_dragRowOrCol >= 0 )
8f177c8e 5856 {
2d66e025 5857 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 5858 {
e2b42eeb 5859 // don't capture the mouse yet
6e8524b1 5860 if ( CanDragRowSize() )
ca65c044 5861 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
8f177c8e 5862 }
2d66e025 5863 }
6d004f67 5864 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5865 {
ca65c044 5866 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
8f177c8e 5867 }
f85afd4e 5868 }
2d66e025
MB
5869}
5870
11393d29
VZ
5871void wxGrid::UpdateColumnSortingIndicator(int col)
5872{
5873 wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" );
5874
5875 if ( m_useNativeHeader )
3039ade9 5876 GetGridColHeader()->UpdateColumn(col);
11393d29
VZ
5877 else if ( m_nativeColumnLabels )
5878 m_colWindow->Refresh();
5879 //else: sorting indicator display not yet implemented in grid version
5880}
5881
5882void wxGrid::SetSortingColumn(int col, bool ascending)
5883{
5884 if ( col == m_sortCol )
5885 {
5886 // we are already using this column for sorting (or not sorting at all)
5887 // but we might still change the sorting order, check for it
5888 if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending )
5889 {
5890 m_sortIsAscending = ascending;
5891
5892 UpdateColumnSortingIndicator(m_sortCol);
5893 }
5894 }
5895 else // we're changing the column used for sorting
5896 {
5897 const int sortColOld = m_sortCol;
5898
5899 // change it before updating the column as we want GetSortingColumn()
5900 // to return the correct new value
5901 m_sortCol = col;
5902
5903 if ( sortColOld != wxNOT_FOUND )
5904 UpdateColumnSortingIndicator(sortColOld);
5905
5906 if ( m_sortCol != wxNOT_FOUND )
5907 {
5908 m_sortIsAscending = ascending;
5909 UpdateColumnSortingIndicator(m_sortCol);
5910 }
5911 }
5912}
5913
5914void wxGrid::DoColHeaderClick(int col)
5915{
5916 // we consider that the grid was resorted if this event is processed and
5917 // not vetoed
5918 if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 )
5919 {
5920 SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true);
5921 Refresh();
5922 }
5923}
5924
ad805b9e
VZ
5925void wxGrid::DoStartResizeCol(int col)
5926{
5927 m_dragRowOrCol = col;
5928 m_dragLastPos = -1;
5929 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol));
5930}
5931
5932void wxGrid::DoUpdateResizeCol(int x)
5933{
5934 int cw, ch, dummy, top;
5935 m_gridWin->GetClientSize( &cw, &ch );
5936 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5937
5938 wxClientDC dc( m_gridWin );
5939 PrepareDC( dc );
5940
5941 x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol));
5942 dc.SetLogicalFunction(wxINVERT);
5943 if ( m_dragLastPos >= 0 )
5944 {
5945 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
5946 }
5947 dc.DrawLine( x, top, x, top + ch );
5948 m_dragLastPos = x;
5949}
5950
5951void wxGrid::DoUpdateResizeColWidth(int w)
5952{
5953 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w);
5954}
5955
2d66e025
MB
5956void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5957{
11393d29 5958 int x, y;
2d66e025
MB
5959 wxPoint pos( event.GetPosition() );
5960 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5961
11393d29 5962 int col = XToCol(x);
2d66e025 5963 if ( event.Dragging() )
f85afd4e 5964 {
fe77cf60
JS
5965 if (!m_isDragging)
5966 {
ca65c044 5967 m_isDragging = true;
ad805b9e 5968 GetColLabelWindow()->CaptureMouse();
d4175745 5969
11393d29
VZ
5970 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 )
5971 DoStartMoveCol(col);
fe77cf60 5972 }
8f177c8e 5973
2d66e025 5974 if ( event.LeftIsDown() )
8f177c8e 5975 {
962a48f6 5976 switch ( m_cursorMode )
8f177c8e 5977 {
2d66e025 5978 case WXGRID_CURSOR_RESIZE_COL:
ad805b9e 5979 DoUpdateResizeCol(x);
2d66e025 5980 break;
f85afd4e 5981
2d66e025 5982 case WXGRID_CURSOR_SELECT_COL:
902725ee 5983 {
11393d29 5984 if ( col != -1 )
aa5e1f75 5985 {
3f3dc2ef 5986 if ( m_selection )
8b5f6d9d 5987 m_selection->SelectCol(col, event);
2d66e025 5988 }
902725ee
WS
5989 }
5990 break;
e2b42eeb 5991
d4175745
VZ
5992 case WXGRID_CURSOR_MOVE_COL:
5993 {
cd68daf5
VZ
5994 int posNew = XToPos(x);
5995 int colNew = GetColAt(posNew);
d4175745 5996
cd68daf5 5997 // determine the position of the drop marker
d4175745 5998 int markerX;
cd68daf5
VZ
5999 if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) )
6000 markerX = GetColRight(colNew);
d4175745 6001 else
cd68daf5 6002 markerX = GetColLeft(colNew);
d4175745
VZ
6003
6004 if ( markerX != m_dragLastPos )
6005 {
ad805b9e 6006 wxClientDC dc( GetColLabelWindow() );
1eab9659 6007 DoPrepareDC(dc);
d4175745
VZ
6008
6009 int cw, ch;
ad805b9e 6010 GetColLabelWindow()->GetClientSize( &cw, &ch );
d4175745
VZ
6011
6012 markerX++;
6013
6014 //Clean up the last indicator
6015 if ( m_dragLastPos >= 0 )
6016 {
ad805b9e 6017 wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
d4175745
VZ
6018 dc.SetPen(pen);
6019 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
6020 dc.SetPen(wxNullPen);
6021
6022 if ( XToCol( m_dragLastPos ) != -1 )
6023 DrawColLabel( dc, XToCol( m_dragLastPos ) );
6024 }
6025
87c819f9 6026 const wxColour *color;
d4175745 6027 //Moving to the same place? Don't draw a marker
cd68daf5 6028 if ( colNew == m_dragRowOrCol )
87c819f9
VZ
6029 color = wxLIGHT_GREY;
6030 else
6031 color = wxBLUE;
d4175745
VZ
6032
6033 //Draw the marker
87c819f9 6034 wxPen pen( *color, 2 );
d4175745
VZ
6035 dc.SetPen(pen);
6036
6037 dc.DrawLine( markerX, 0, markerX, ch );
6038
6039 dc.SetPen(wxNullPen);
6040
6041 m_dragLastPos = markerX - 1;
6042 }
6043 }
6044 break;
6045
e2b42eeb
VZ
6046 // default label to suppress warnings about "enumeration value
6047 // 'xxx' not handled in switch
6048 default:
6049 break;
2d66e025 6050 }
f85afd4e 6051 }
2d66e025 6052 return;
f85afd4e 6053 }
2d66e025 6054
fe77cf60
JS
6055 if ( m_isDragging && (event.Entering() || event.Leaving()) )
6056 return;
2d66e025 6057
fe77cf60
JS
6058 if (m_isDragging)
6059 {
ad805b9e
VZ
6060 if (GetColLabelWindow()->HasCapture())
6061 GetColLabelWindow()->ReleaseMouse();
ca65c044 6062 m_isDragging = false;
fe77cf60 6063 }
60ff3b99 6064
6d004f67
MB
6065 // ------------ Entering or leaving the window
6066 //
6067 if ( event.Entering() || event.Leaving() )
6068 {
ad805b9e 6069 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
6d004f67
MB
6070 }
6071
2d66e025 6072 // ------------ Left button pressed
f85afd4e 6073 //
6d004f67 6074 else if ( event.LeftDown() )
f85afd4e 6075 {
2d66e025
MB
6076 // don't send a label click event for a hit on the
6077 // edge of the col label - this is probably the user
6078 // wanting to resize the col
6079 //
6080 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 6081 {
2f024384 6082 if ( col >= 0 &&
b54ba671 6083 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 6084 {
d4175745 6085 if ( m_canDragColMove )
3f3dc2ef 6086 {
d4175745 6087 //Show button as pressed
ad805b9e 6088 wxClientDC dc( GetColLabelWindow() );
d4175745
VZ
6089 int colLeft = GetColLeft( col );
6090 int colRight = GetColRight( col ) - 1;
ad805b9e 6091 dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
d4175745
VZ
6092 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
6093 dc.DrawLine( colLeft, 1, colRight, 1 );
6094
ad805b9e 6095 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow());
d4175745
VZ
6096 }
6097 else
6098 {
6099 if ( !event.ShiftDown() && !event.CmdDown() )
6100 ClearSelection();
6101 if ( m_selection )
3f3dc2ef 6102 {
d4175745
VZ
6103 if ( event.ShiftDown() )
6104 {
8b5f6d9d
VZ
6105 m_selection->SelectBlock
6106 (
6107 0, m_currentCellCoords.GetCol(),
6108 GetNumberRows() - 1, col,
6109 event
6110 );
d4175745
VZ
6111 }
6112 else
6113 {
8b5f6d9d 6114 m_selection->SelectCol(col, event);
d4175745 6115 }
3f3dc2ef 6116 }
3f3dc2ef 6117
ad805b9e 6118 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow());
d4175745 6119 }
f85afd4e 6120 }
2d66e025
MB
6121 }
6122 else
6123 {
6124 // starting to drag-resize a col
6125 //
6e8524b1 6126 if ( CanDragColSize() )
ad805b9e 6127 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow());
2d66e025
MB
6128 }
6129 }
f85afd4e 6130
2d66e025
MB
6131 // ------------ Left double click
6132 //
6133 if ( event.LeftDClick() )
6134 {
11393d29
VZ
6135 const int colEdge = XToEdgeOfCol(x);
6136 if ( colEdge == -1 )
2d66e025 6137 {
a967f048
RG
6138 if ( col >= 0 &&
6139 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
6140 {
ca65c044 6141 // no default action at the moment
a967f048 6142 }
2d66e025 6143 }
d43851f7
JS
6144 else
6145 {
6146 // adjust column width depending on label text
11393d29 6147 AutoSizeColLabelSize( colEdge );
d43851f7 6148
ad805b9e 6149 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
ccdee36f 6150 m_dragLastPos = -1;
d43851f7 6151 }
2d66e025 6152 }
60ff3b99 6153
2d66e025
MB
6154 // ------------ Left button released
6155 //
6156 else if ( event.LeftUp() )
6157 {
d4175745 6158 switch ( m_cursorMode )
2d66e025 6159 {
d4175745 6160 case WXGRID_CURSOR_RESIZE_COL:
d4175745 6161 DoEndDragResizeCol();
852b6c3c 6162 break;
d4175745
VZ
6163
6164 case WXGRID_CURSOR_MOVE_COL:
11393d29 6165 if ( m_dragLastPos == -1 || col == m_dragRowOrCol )
cd68daf5 6166 {
11393d29
VZ
6167 // the column didn't actually move anywhere
6168 if ( col != -1 )
6169 DoColHeaderClick(col);
cd68daf5
VZ
6170 m_colWindow->Refresh(); // "unpress" the column
6171 }
6172 else
6173 {
6174 DoEndMoveCol(XToPos(x));
6175 }
852b6c3c
VZ
6176 break;
6177
6178 case WXGRID_CURSOR_SELECT_COL:
6179 case WXGRID_CURSOR_SELECT_CELL:
6180 case WXGRID_CURSOR_RESIZE_ROW:
6181 case WXGRID_CURSOR_SELECT_ROW:
11393d29
VZ
6182 if ( col != -1 )
6183 DoColHeaderClick(col);
852b6c3c 6184 break;
2d66e025 6185 }
f85afd4e 6186
ad805b9e 6187 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
ccdee36f 6188 m_dragLastPos = -1;
60ff3b99
VZ
6189 }
6190
2d66e025
MB
6191 // ------------ Right button down
6192 //
6193 else if ( event.RightDown() )
6194 {
a967f048 6195 if ( col >= 0 &&
ca65c044 6196 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
6197 {
6198 // no default action at the moment
f85afd4e
MB
6199 }
6200 }
60ff3b99 6201
2d66e025 6202 // ------------ Right double click
f85afd4e 6203 //
2d66e025
MB
6204 else if ( event.RightDClick() )
6205 {
a967f048 6206 if ( col >= 0 &&
ca65c044 6207 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
6208 {
6209 // no default action at the moment
6210 }
6211 }
60ff3b99 6212
2d66e025 6213 // ------------ No buttons down and mouse moving
f85afd4e 6214 //
2d66e025 6215 else if ( event.Moving() )
f85afd4e 6216 {
2d66e025
MB
6217 m_dragRowOrCol = XToEdgeOfCol( x );
6218 if ( m_dragRowOrCol >= 0 )
f85afd4e 6219 {
2d66e025 6220 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 6221 {
e2b42eeb 6222 // don't capture the cursor yet
6e8524b1 6223 if ( CanDragColSize() )
ad805b9e 6224 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false);
f85afd4e 6225 }
2d66e025 6226 }
6d004f67 6227 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 6228 {
ad805b9e 6229 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false);
8f177c8e 6230 }
f85afd4e
MB
6231 }
6232}
6233
2d66e025 6234void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 6235{
2d66e025 6236 if ( event.LeftDown() )
f85afd4e 6237 {
2d66e025
MB
6238 // indicate corner label by having both row and
6239 // col args == -1
f85afd4e 6240 //
b54ba671 6241 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
6242 {
6243 SelectAll();
6244 }
f85afd4e 6245 }
2d66e025
MB
6246 else if ( event.LeftDClick() )
6247 {
b54ba671 6248 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 6249 }
2d66e025 6250 else if ( event.RightDown() )
f85afd4e 6251 {
b54ba671 6252 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 6253 {
2d66e025
MB
6254 // no default action at the moment
6255 }
6256 }
2d66e025
MB
6257 else if ( event.RightDClick() )
6258 {
b54ba671 6259 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
6260 {
6261 // no default action at the moment
6262 }
6263 }
6264}
f85afd4e 6265
86033c4b
VZ
6266void wxGrid::CancelMouseCapture()
6267{
6268 // cancel operation currently in progress, whatever it is
6269 if ( m_winCapture )
6270 {
6271 m_isDragging = false;
8a3e536c
VZ
6272 m_startDragPos = wxDefaultPosition;
6273
86033c4b
VZ
6274 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
6275 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
6276 m_winCapture = NULL;
6277
6278 // remove traces of whatever we drew on screen
6279 Refresh();
6280 }
6281}
6282
e2b42eeb
VZ
6283void wxGrid::ChangeCursorMode(CursorMode mode,
6284 wxWindow *win,
6285 bool captureMouse)
6286{
6287#ifdef __WXDEBUG__
6288 static const wxChar *cursorModes[] =
6289 {
6290 _T("SELECT_CELL"),
6291 _T("RESIZE_ROW"),
6292 _T("RESIZE_COL"),
6293 _T("SELECT_ROW"),
d4175745
VZ
6294 _T("SELECT_COL"),
6295 _T("MOVE_COL"),
e2b42eeb
VZ
6296 };
6297
181bfffd
VZ
6298 wxLogTrace(_T("grid"),
6299 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
ad805b9e
VZ
6300 win == m_colWindow ? _T("colLabelWin")
6301 : win ? _T("rowLabelWin")
6302 : _T("gridWin"),
e2b42eeb 6303 cursorModes[m_cursorMode], cursorModes[mode]);
2f024384 6304#endif
e2b42eeb 6305
faec5a43
SN
6306 if ( mode == m_cursorMode &&
6307 win == m_winCapture &&
6308 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
6309 return;
6310
6311 if ( !win )
6312 {
6313 // by default use the grid itself
6314 win = m_gridWin;
6315 }
6316
6317 if ( m_winCapture )
6318 {
e882d288 6319 m_winCapture->ReleaseMouse();
10a4531d 6320 m_winCapture = NULL;
e2b42eeb
VZ
6321 }
6322
6323 m_cursorMode = mode;
6324
6325 switch ( m_cursorMode )
6326 {
6327 case WXGRID_CURSOR_RESIZE_ROW:
6328 win->SetCursor( m_rowResizeCursor );
6329 break;
6330
6331 case WXGRID_CURSOR_RESIZE_COL:
6332 win->SetCursor( m_colResizeCursor );
6333 break;
6334
d4175745
VZ
6335 case WXGRID_CURSOR_MOVE_COL:
6336 win->SetCursor( wxCursor(wxCURSOR_HAND) );
6337 break;
6338
e2b42eeb
VZ
6339 default:
6340 win->SetCursor( *wxSTANDARD_CURSOR );
2f024384 6341 break;
e2b42eeb
VZ
6342 }
6343
6344 // we need to capture mouse when resizing
6345 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
6346 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
6347
6348 if ( captureMouse && resize )
6349 {
6350 win->CaptureMouse();
6351 m_winCapture = win;
6352 }
6353}
8f177c8e 6354
8a3e536c
VZ
6355// ----------------------------------------------------------------------------
6356// grid mouse event processing
6357// ----------------------------------------------------------------------------
60ff3b99 6358
8a3e536c
VZ
6359void
6360wxGrid::DoGridCellDrag(wxMouseEvent& event,
6361 const wxGridCellCoords& coords,
6362 bool isFirstDrag)
6363{
6364 if ( coords == wxGridNoCellCoords )
6365 return; // we're outside any valid cell
60ff3b99 6366
8a3e536c
VZ
6367 // Hide the edit control, so it won't interfere with drag-shrinking.
6368 if ( IsCellEditControlShown() )
27f35b66 6369 {
8a3e536c
VZ
6370 HideCellEditControl();
6371 SaveEditControlValue();
27f35b66
SN
6372 }
6373
8a3e536c 6374 switch ( event.GetModifiers() )
2d66e025 6375 {
8a3e536c
VZ
6376 case wxMOD_CMD:
6377 if ( m_selectedBlockCorner == wxGridNoCellCoords)
6378 m_selectedBlockCorner = coords;
6379 UpdateBlockBeingSelected(m_selectedBlockCorner, coords);
6380 break;
07296f0b 6381
8a3e536c
VZ
6382 case wxMOD_NONE:
6383 if ( CanDragCell() )
2d66e025 6384 {
8a3e536c 6385 if ( isFirstDrag )
79dbea21 6386 {
8a3e536c
VZ
6387 if ( m_selectedBlockCorner == wxGridNoCellCoords)
6388 m_selectedBlockCorner = coords;
07296f0b 6389
8a3e536c
VZ
6390 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event);
6391 return;
07296f0b 6392 }
2d66e025 6393 }
25107357 6394
8a3e536c
VZ
6395 UpdateBlockBeingSelected(m_currentCellCoords, coords);
6396 break;
c71b2126 6397
8a3e536c
VZ
6398 default:
6399 // we don't handle the other key modifiers
6400 event.Skip();
6401 }
6402}
8f177c8e 6403
8a3e536c
VZ
6404void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper)
6405{
6406 wxClientDC dc(m_gridWin);
6407 PrepareDC(dc);
6408 dc.SetLogicalFunction(wxINVERT);
e2b42eeb 6409
8a3e536c
VZ
6410 const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
6411 m_gridWin->GetClientSize());
6412
6413 // erase the previously drawn line, if any
6414 if ( m_dragLastPos >= 0 )
6415 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
6416
6417 // we need the vertical position for rows and horizontal for columns here
6418 m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition()));
6419
6420 // don't allow resizing beneath the minimal size
6421 const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) +
6422 oper.GetMinimalLineSize(this, m_dragRowOrCol);
6423 if ( m_dragLastPos < posMin )
6424 m_dragLastPos = posMin;
6425
6426 // and draw it at the new position
6427 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
6428}
6429
6430void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords)
6431{
6432 if ( !m_isDragging )
6433 {
6434 // Don't start doing anything until the mouse has been dragged far
6435 // enough
6436 const wxPoint& pt = event.GetPosition();
6437 if ( m_startDragPos == wxDefaultPosition )
6438 {
6439 m_startDragPos = pt;
6440 return;
6d004f67 6441 }
e2b42eeb 6442
8a3e536c
VZ
6443 if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY &&
6444 abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY )
6445 return;
2d66e025 6446 }
66242c80 6447
8a3e536c
VZ
6448 const bool isFirstDrag = !m_isDragging;
6449 m_isDragging = true;
07296f0b 6450
8a3e536c 6451 switch ( m_cursorMode )
a5777624 6452 {
8a3e536c
VZ
6453 case WXGRID_CURSOR_SELECT_CELL:
6454 DoGridCellDrag(event, coords, isFirstDrag);
6455 break;
6456
6457 case WXGRID_CURSOR_RESIZE_ROW:
6458 DoGridLineDrag(event, wxGridRowOperations());
6459 break;
6460
6461 case WXGRID_CURSOR_RESIZE_COL:
6462 DoGridLineDrag(event, wxGridColumnOperations());
6463 break;
6464
6465 default:
6466 event.Skip();
a5777624 6467 }
e2b42eeb 6468
8a3e536c 6469 if ( isFirstDrag )
f6bcfd97 6470 {
8a3e536c
VZ
6471 m_winCapture = m_gridWin;
6472 m_winCapture->CaptureMouse();
6473 }
6474}
a5777624 6475
8a3e536c
VZ
6476void
6477wxGrid::DoGridCellLeftDown(wxMouseEvent& event,
6478 const wxGridCellCoords& coords,
6479 const wxPoint& pos)
6480{
6481 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) )
6482 {
6483 // event handled by user code, no need to do anything here
6484 return;
a5777624 6485 }
f85afd4e 6486
8a3e536c
VZ
6487 if ( !event.CmdDown() )
6488 ClearSelection();
6489
6490 if ( event.ShiftDown() )
6491 {
6492 if ( m_selection )
6493 {
8b5f6d9d 6494 m_selection->SelectBlock(m_currentCellCoords, coords, event);
8a3e536c
VZ
6495 m_selectedBlockCorner = coords;
6496 }
6497 }
6498 else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
a5777624
RD
6499 {
6500 DisableCellEditControl();
8a3e536c 6501 MakeCellVisible( coords );
a5777624 6502
8a3e536c 6503 if ( event.CmdDown() )
58dd5b3b 6504 {
8a3e536c 6505 if ( m_selection )
1ef49ab5 6506 {
8b5f6d9d 6507 m_selection->ToggleCellSelection(coords, event);
1ef49ab5 6508 }
8b5f6d9d 6509
8a3e536c
VZ
6510 m_selectedBlockTopLeft = wxGridNoCellCoords;
6511 m_selectedBlockBottomRight = wxGridNoCellCoords;
6512 m_selectedBlockCorner = coords;
6513 }
6514 else
6515 {
6516 m_waitForSlowClick = m_currentCellCoords == coords &&
6517 coords != wxGridNoCellCoords;
6518 SetCurrentCell( coords );
58dd5b3b 6519 }
a5777624 6520 }
8a3e536c 6521}
f85afd4e 6522
8a3e536c
VZ
6523void
6524wxGrid::DoGridCellLeftDClick(wxMouseEvent& event,
6525 const wxGridCellCoords& coords,
6526 const wxPoint& pos)
6527{
6528 if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
a5777624 6529 {
8a3e536c 6530 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) )
f85afd4e 6531 {
8a3e536c
VZ
6532 // we want double click to select a cell and start editing
6533 // (i.e. to behave in same way as sequence of two slow clicks):
6534 m_waitForSlowClick = true;
6535 }
6536 }
6537}
932b55d0 6538
8a3e536c
VZ
6539void
6540wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords)
6541{
6542 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6543 {
6544 if (m_winCapture)
6545 {
e882d288 6546 m_winCapture->ReleaseMouse();
8a3e536c
VZ
6547 m_winCapture = NULL;
6548 }
932b55d0 6549
8a3e536c
VZ
6550 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
6551 {
6552 ClearSelection();
6553 EnableCellEditControl();
3f3dc2ef 6554
8a3e536c
VZ
6555 wxGridCellAttr *attr = GetCellAttr(coords);
6556 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6557 editor->StartingClick();
6558 editor->DecRef();
6559 attr->DecRef();
2d66e025 6560
8a3e536c 6561 m_waitForSlowClick = false;
a5777624 6562 }
8a3e536c
VZ
6563 else if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
6564 m_selectedBlockBottomRight != wxGridNoCellCoords )
a5777624 6565 {
8a3e536c
VZ
6566 if ( m_selection )
6567 {
6568 m_selection->SelectBlock( m_selectedBlockTopLeft,
6569 m_selectedBlockBottomRight,
8b5f6d9d 6570 event );
8a3e536c 6571 }
e2b42eeb 6572
8a3e536c
VZ
6573 m_selectedBlockTopLeft = wxGridNoCellCoords;
6574 m_selectedBlockBottomRight = wxGridNoCellCoords;
e2b42eeb 6575
8a3e536c
VZ
6576 // Show the edit control, if it has been hidden for
6577 // drag-shrinking.
6578 ShowCellEditControl();
58dd5b3b 6579 }
8a3e536c
VZ
6580 }
6581 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6582 {
6583 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6584 DoEndDragResizeRow();
f85afd4e 6585
8a3e536c
VZ
6586 // Note: we are ending the event *after* doing
6587 // default processing in this case
6588 //
6589 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
a5777624 6590 }
8a3e536c
VZ
6591 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6592 {
6593 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6594 DoEndDragResizeCol();
8a3e536c
VZ
6595 }
6596
6597 m_dragLastPos = -1;
6598}
6599
6600void
6601wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event),
6602 const wxGridCellCoords& coords,
6603 const wxPoint& pos)
6604{
6605 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
a5777624 6606 {
8a3e536c
VZ
6607 // out of grid cell area
6608 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6609 return;
a5777624 6610 }
2d66e025 6611
8a3e536c
VZ
6612 int dragRow = YToEdgeOfRow( pos.y );
6613 int dragCol = XToEdgeOfCol( pos.x );
6614
6615 // Dragging on the corner of a cell to resize in both
6616 // directions is not implemented yet...
a5777624 6617 //
8a3e536c 6618 if ( dragRow >= 0 && dragCol >= 0 )
a5777624 6619 {
8a3e536c
VZ
6620 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6621 return;
6622 }
6623
6624 if ( dragRow >= 0 )
6625 {
6626 m_dragRowOrCol = dragRow;
6627
6628 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 6629 {
8a3e536c
VZ
6630 if ( CanDragRowSize() && CanDragGridSize() )
6631 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
60ff3b99 6632 }
a5777624 6633 }
ad805b9e
VZ
6634 // When using the native header window we can only resize the columns by
6635 // dragging the dividers in it because we can't make it enter into the
6636 // column resizing mode programmatically
6637 else if ( dragCol >= 0 && !m_useNativeHeader )
8a3e536c
VZ
6638 {
6639 m_dragRowOrCol = dragCol;
a5777624 6640
8a3e536c
VZ
6641 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6642 {
6643 if ( CanDragColSize() && CanDragGridSize() )
6644 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
6645 }
6646 }
6647 else // Neither on a row or col edge
a5777624 6648 {
8a3e536c 6649 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
d57ad377 6650 {
d57ad377 6651 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
d57ad377 6652 }
8a3e536c
VZ
6653 }
6654}
d57ad377 6655
8a3e536c
VZ
6656void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
6657{
6658 const wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
790cc417 6659
8a3e536c
VZ
6660 // coordinates of the cell under mouse
6661 wxGridCellCoords coords = XYToCell(pos);
6d004f67 6662
8a3e536c
VZ
6663 int cell_rows, cell_cols;
6664 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
6665 if ( (cell_rows < 0) || (cell_cols < 0) )
6666 {
6667 coords.SetRow(coords.GetRow() + cell_rows);
6668 coords.SetCol(coords.GetCol() + cell_cols);
6669 }
6d004f67 6670
8a3e536c
VZ
6671 if ( event.Dragging() )
6672 {
6673 if ( event.LeftIsDown() )
6674 DoGridDragEvent(event, coords);
6675 else
6676 event.Skip();
6677 return;
6678 }
6679
6680 m_isDragging = false;
6681 m_startDragPos = wxDefaultPosition;
6682
6683 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6684 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6685 // wxGTK
6686#if 0
6687 if ( event.Entering() || event.Leaving() )
6688 {
6689 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6690 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
6691 }
6692#endif // 0
6693
6694 // deal with various button presses
6695 if ( event.IsButton() )
6696 {
6697 if ( coords != wxGridNoCellCoords )
a5777624 6698 {
8a3e536c 6699 DisableCellEditControl();
e2b42eeb 6700
8a3e536c
VZ
6701 if ( event.LeftDown() )
6702 DoGridCellLeftDown(event, coords, pos);
6703 else if ( event.LeftDClick() )
6704 DoGridCellLeftDClick(event, coords, pos);
6705 else if ( event.RightDown() )
6706 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event);
6707 else if ( event.RightDClick() )
6708 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event);
6d004f67 6709 }
8a3e536c
VZ
6710
6711 // this one should be called even if we're not over any cell
6712 if ( event.LeftUp() )
a5777624 6713 {
8a3e536c 6714 DoGridCellLeftUp(event, coords);
a5777624
RD
6715 }
6716 }
8a3e536c
VZ
6717 else if ( event.Moving() )
6718 {
6719 DoGridMouseMoveEvent(event, coords, pos);
6720 }
6721 else // unknown mouse event?
6722 {
6723 event.Skip();
6724 }
6d004f67
MB
6725}
6726
bec70262 6727void wxGrid::DoEndDragResizeLine(const wxGridOperations& oper)
6d004f67 6728{
bec70262
VZ
6729 if ( m_dragLastPos == -1 )
6730 return;
6d004f67 6731
bec70262 6732 const wxGridOperations& doper = oper.Dual();
6d004f67 6733
bec70262 6734 const wxSize size = m_gridWin->GetClientSize();
e2b42eeb 6735
bec70262 6736 const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0));
ccdee36f 6737
bec70262
VZ
6738 // erase the last line we drew
6739 wxClientDC dc(m_gridWin);
6740 PrepareDC(dc);
6741 dc.SetLogicalFunction(wxINVERT);
6d004f67 6742
bec70262
VZ
6743 const int posLineStart = oper.Select(ptOrigin);
6744 const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size);
6d004f67 6745
bec70262 6746 oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos);
6d004f67 6747
bec70262
VZ
6748 // temporarily hide the edit control before resizing
6749 HideCellEditControl();
6750 SaveEditControlValue();
6751
6752 // do resize the line
6753 const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol);
6754 oper.SetLineSize(this, m_dragRowOrCol,
6755 wxMax(m_dragLastPos - lineStart,
6756 oper.GetMinimalLineSize(this, m_dragRowOrCol)));
6757
ad805b9e
VZ
6758 m_dragLastPos = -1;
6759
bec70262
VZ
6760 // refresh now if we're not frozen
6761 if ( !GetBatchCount() )
6d004f67 6762 {
bec70262
VZ
6763 // we need to refresh everything beyond the resized line in the header
6764 // window
6d004f67 6765
bec70262
VZ
6766 // get the position from which to refresh in the other direction
6767 wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0)));
6768 rect.SetPosition(CalcScrolledPosition(rect.GetPosition()));
6d004f67 6769
bec70262
VZ
6770 // we only need the ordinate (for rows) or abscissa (for columns) here,
6771 // and need to cover the entire window in the other direction
6772 oper.Select(rect) = 0;
6d004f67 6773
bec70262
VZ
6774 wxRect rectHeader(rect.GetPosition(),
6775 oper.MakeSize
6776 (
6777 oper.GetHeaderWindowSize(this),
6778 doper.Select(size) - doper.Select(rect)
6779 ));
6780
6781 oper.GetHeaderWindow(this)->Refresh(true, &rectHeader);
6782
6783
6784 // also refresh the grid window: extend the rectangle
6785 if ( m_table )
6d004f67 6786 {
bec70262 6787 oper.SelectSize(rect) = oper.Select(size);
2f024384 6788
bec70262
VZ
6789 int subtractLines = 0;
6790 const int lineStart = oper.PosToLine(this, posLineStart);
6791 if ( lineStart >= 0 )
27f35b66 6792 {
bec70262
VZ
6793 // ensure that if we have a multi-cell block we redraw all of
6794 // it by increasing the refresh area to cover it entirely if a
6795 // part of it is affected
6796 const int lineEnd = oper.PosToLine(this, posLineEnd, true);
6797 for ( int line = lineStart; line < lineEnd; line++ )
27f35b66 6798 {
bec70262
VZ
6799 int cellLines = oper.Select(
6800 GetCellSize(oper.MakeCoords(m_dragRowOrCol, line)));
6801 if ( cellLines < subtractLines )
6802 subtractLines = cellLines;
27f35b66
SN
6803 }
6804 }
2f024384 6805
bec70262
VZ
6806 int startPos =
6807 oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines);
6808 startPos = doper.CalcScrolledPosition(this, startPos);
6809
6810 doper.Select(rect) = startPos;
6811 doper.SelectSize(rect) = doper.Select(size) - startPos;
e2b42eeb 6812
bec70262
VZ
6813 m_gridWin->Refresh(false, &rect);
6814 }
f85afd4e 6815 }
bec70262
VZ
6816
6817 // show the edit control back again
6818 ShowCellEditControl();
6819}
6820
6821void wxGrid::DoEndDragResizeRow()
6822{
6823 DoEndDragResizeLine(wxGridRowOperations());
6824}
6825
ad805b9e 6826void wxGrid::DoEndDragResizeCol(wxMouseEvent *event)
bec70262
VZ
6827{
6828 DoEndDragResizeLine(wxGridColumnOperations());
ad805b9e
VZ
6829
6830 // Note: we are ending the event *after* doing
6831 // default processing in this case
6832 //
6833 if ( event )
6834 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, *event );
6835 else
6836 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol );
f85afd4e
MB
6837}
6838
cd68daf5 6839void wxGrid::DoStartMoveCol(int col)
d4175745 6840{
cd68daf5
VZ
6841 m_dragRowOrCol = col;
6842}
d4175745 6843
cd68daf5
VZ
6844void wxGrid::DoEndMoveCol(int pos)
6845{
6846 wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" );
6847
6848 if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 )
6849 SetColPos(m_dragRowOrCol, pos);
6850 //else: vetoed by user
d4175745 6851
cd68daf5 6852 m_dragRowOrCol = -1;
d4175745
VZ
6853}
6854
4797b014 6855void wxGrid::RefreshAfterColPosChange()
009c7216 6856{
4797b014
VZ
6857 // recalculate the column rights as the column positions have changed,
6858 // unless we calculate them dynamically because all columns widths are the
6859 // same and it's easy to do
6860 if ( !m_colWidths.empty() )
009c7216 6861 {
4797b014
VZ
6862 int colRight = 0;
6863 for ( int colPos = 0; colPos < m_numCols; colPos++ )
6864 {
6865 int colID = GetColAt( colPos );
6866
6867 colRight += m_colWidths[colID];
6868 m_colRights[colID] = colRight;
6869 }
6870 }
009c7216 6871
4797b014
VZ
6872 // and make the changes visible
6873 if ( m_useNativeHeader )
6874 {
6875 if ( m_colAt.empty() )
3039ade9 6876 GetGridColHeader()->ResetColumnsOrder();
4797b014 6877 else
3039ade9 6878 GetGridColHeader()->SetColumnsOrder(m_colAt);
4797b014
VZ
6879 }
6880 else
6881 {
6882 m_colWindow->Refresh();
009c7216 6883 }
4797b014 6884 m_gridWin->Refresh();
009c7216
VZ
6885}
6886
31ec8b4e
VZ
6887void wxGrid::SetColumnsOrder(const wxArrayInt& order)
6888{
6889 m_colAt = order;
6890
6891 RefreshAfterColPosChange();
6892}
6893
3169a8e8 6894void wxGrid::SetColPos(int idx, int pos)
d4175745 6895{
1bb74626 6896 // we're going to need m_colAt now, initialize it if needed
3169a8e8 6897 if ( m_colAt.empty() )
d4175745 6898 {
3169a8e8
VZ
6899 m_colAt.reserve(m_numCols);
6900 for ( int i = 0; i < m_numCols; i++ )
6901 m_colAt.push_back(i);
d4175745
VZ
6902 }
6903
1bb74626 6904 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
d4175745 6905
4797b014 6906 RefreshAfterColPosChange();
d4175745
VZ
6907}
6908
009c7216
VZ
6909void wxGrid::ResetColPos()
6910{
6911 m_colAt.clear();
da5a641f 6912
4797b014 6913 RefreshAfterColPosChange();
009c7216 6914}
d4175745
VZ
6915
6916void wxGrid::EnableDragColMove( bool enable )
6917{
6918 if ( m_canDragColMove == enable )
6919 return;
6920
009c7216 6921 if ( m_useNativeHeader )
d4175745 6922 {
009c7216 6923 // update all columns to make them [not] reorderable
3039ade9 6924 GetGridColHeader()->SetColumnCount(m_numCols);
009c7216 6925 }
d4175745 6926
009c7216 6927 m_canDragColMove = enable;
d4175745 6928
009c7216
VZ
6929 // we use to call ResetColPos() from here if !enable but this doesn't seem
6930 // right as it would mean there would be no way to "freeze" the current
6931 // columns order by disabling moving them after putting them in the desired
6932 // order, whereas now you can always call ResetColPos() manually if needed
d4175745
VZ
6933}
6934
6935
2d66e025
MB
6936//
6937// ------ interaction with data model
6938//
6939bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 6940{
2d66e025 6941 switch ( msg.GetId() )
17732cec 6942 {
2d66e025
MB
6943 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6944 return GetModelValues();
17732cec 6945
2d66e025
MB
6946 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6947 return SetModelValues();
f85afd4e 6948
2d66e025
MB
6949 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6950 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6951 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6952 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6953 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6954 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6955 return Redimension( msg );
6956
6957 default:
ca65c044 6958 return false;
f85afd4e 6959 }
2d66e025 6960}
f85afd4e 6961
2d66e025 6962// The behaviour of this function depends on the grid table class
5b061713 6963// Clear() function. For the default wxGridStringTable class the
10a4531d 6964// behaviour is to replace all cell contents with wxEmptyString but
2d66e025
MB
6965// not to change the number of rows or cols.
6966//
6967void wxGrid::ClearGrid()
6968{
6969 if ( m_table )
f85afd4e 6970 {
4cfa5de6
RD
6971 if (IsCellEditControlEnabled())
6972 DisableCellEditControl();
6973
2d66e025 6974 m_table->Clear();
5b061713 6975 if (!GetBatchCount())
a9339fe2 6976 m_gridWin->Refresh();
f85afd4e
MB
6977 }
6978}
6979
10a4531d
VZ
6980bool
6981wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t),
6982 int pos, int num, bool WXUNUSED(updateLabels) )
f85afd4e 6983{
10a4531d 6984 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
f85afd4e 6985
10a4531d 6986 if ( !m_table )
ca65c044 6987 return false;
f85afd4e 6988
10a4531d
VZ
6989 if ( IsCellEditControlEnabled() )
6990 DisableCellEditControl();
b7fff980 6991
10a4531d 6992 return (m_table->*funcModify)(pos, num);
2f024384 6993
10a4531d
VZ
6994 // the table will have sent the results of the insert row
6995 // operation to this view object as a grid table message
f85afd4e
MB
6996}
6997
10a4531d
VZ
6998bool
6999wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t),
7000 int num, bool WXUNUSED(updateLabels))
f85afd4e 7001{
10a4531d 7002 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
2f024384 7003
10a4531d 7004 if ( !m_table )
ca65c044 7005 return false;
f85afd4e 7006
10a4531d 7007 return (m_table->*funcAppend)(num);
f85afd4e
MB
7008}
7009
2d66e025
MB
7010//
7011// ----- event handlers
f85afd4e 7012//
8f177c8e 7013
8a3e536c
VZ
7014// Generate a grid event based on a mouse event and return:
7015// -1 if the event was vetoed
7016// +1 if the event was processed (but not vetoed)
7017// 0 if the event wasn't handled
7018int
7019wxGrid::SendEvent(const wxEventType type,
7020 int row, int col,
7021 wxMouseEvent& mouseEv)
2d66e025 7022{
2f024384 7023 bool claimed, vetoed;
fe7b9ed6 7024
97a9929e
VZ
7025 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
7026 {
7027 int rowOrCol = (row == -1 ? col : row);
7028
7029 wxGridSizeEvent gridEvt( GetId(),
7030 type,
7031 this,
7032 rowOrCol,
7033 mouseEv.GetX() + GetRowLabelSize(),
7034 mouseEv.GetY() + GetColLabelSize(),
8b5f6d9d 7035 mouseEv);
97a9929e
VZ
7036
7037 claimed = GetEventHandler()->ProcessEvent(gridEvt);
7038 vetoed = !gridEvt.IsAllowed();
7039 }
fe7b9ed6 7040 else if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
7041 {
7042 // Right now, it should _never_ end up here!
7043 wxGridRangeSelectEvent gridEvt( GetId(),
7044 type,
7045 this,
8a3e536c
VZ
7046 m_selectedBlockTopLeft,
7047 m_selectedBlockBottomRight,
ca65c044 7048 true,
8b5f6d9d 7049 mouseEv);
97a9929e
VZ
7050
7051 claimed = GetEventHandler()->ProcessEvent(gridEvt);
7052 vetoed = !gridEvt.IsAllowed();
7053 }
2b73a34e
RD
7054 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
7055 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
7056 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
7057 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
7058 {
7059 wxPoint pos = mouseEv.GetPosition();
7060
7061 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
7062 pos.y += GetColLabelSize();
7063 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
7064 pos.x += GetRowLabelSize();
c71b2126 7065
2b73a34e
RD
7066 wxGridEvent gridEvt( GetId(),
7067 type,
7068 this,
7069 row, col,
7070 pos.x,
7071 pos.y,
7072 false,
8b5f6d9d 7073 mouseEv);
2b73a34e
RD
7074 claimed = GetEventHandler()->ProcessEvent(gridEvt);
7075 vetoed = !gridEvt.IsAllowed();
7076 }
fe7b9ed6 7077 else
97a9929e
VZ
7078 {
7079 wxGridEvent gridEvt( GetId(),
7080 type,
7081 this,
7082 row, col,
7083 mouseEv.GetX() + GetRowLabelSize(),
7084 mouseEv.GetY() + GetColLabelSize(),
ca65c044 7085 false,
8b5f6d9d 7086 mouseEv);
97a9929e
VZ
7087 claimed = GetEventHandler()->ProcessEvent(gridEvt);
7088 vetoed = !gridEvt.IsAllowed();
7089 }
7090
7091 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
7092 if (vetoed)
7093 return -1;
7094
97a9929e 7095 return claimed ? 1 : 0;
f85afd4e
MB
7096}
7097
8a3e536c 7098// Generate a grid event of specified type, return value same as above
f85afd4e 7099//
8a3e536c 7100int wxGrid::SendEvent(const wxEventType type, int row, int col)
f85afd4e 7101{
a9339fe2 7102 bool claimed, vetoed;
fe7b9ed6 7103
b54ba671 7104 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 7105 {
2d66e025 7106 int rowOrCol = (row == -1 ? col : row);
f85afd4e 7107
a9339fe2 7108 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
2d66e025 7109
fe7b9ed6
VZ
7110 claimed = GetEventHandler()->ProcessEvent(gridEvt);
7111 vetoed = !gridEvt.IsAllowed();
2d66e025
MB
7112 }
7113 else
7114 {
a9339fe2 7115 wxGridEvent gridEvt( GetId(), type, this, row, col );
8f177c8e 7116
fe7b9ed6
VZ
7117 claimed = GetEventHandler()->ProcessEvent(gridEvt);
7118 vetoed = !gridEvt.IsAllowed();
7119 }
7120
97a9929e 7121 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
7122 if (vetoed)
7123 return -1;
7124
97a9929e 7125 return claimed ? 1 : 0;
f85afd4e
MB
7126}
7127
2d66e025 7128void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 7129{
3d59537f
DS
7130 // needed to prevent zillions of paint events on MSW
7131 wxPaintDC dc(this);
8f177c8e 7132}
f85afd4e 7133
bca7bfc8 7134void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 7135{
ad603bf7
VZ
7136 // Don't do anything if between Begin/EndBatch...
7137 // EndBatch() will do all this on the last nested one anyway.
f9549841 7138 if ( m_created && !GetBatchCount() )
27f35b66 7139 {
bca7bfc8 7140 // Refresh to get correct scrolled position:
4db6714b 7141 wxScrolledWindow::Refresh(eraseb, rect);
27f35b66 7142
27f35b66
SN
7143 if (rect)
7144 {
bca7bfc8
SN
7145 int rect_x, rect_y, rectWidth, rectHeight;
7146 int width_label, width_cell, height_label, height_cell;
7147 int x, y;
7148
2f024384 7149 // Copy rectangle can get scroll offsets..
bca7bfc8
SN
7150 rect_x = rect->GetX();
7151 rect_y = rect->GetY();
7152 rectWidth = rect->GetWidth();
7153 rectHeight = rect->GetHeight();
27f35b66 7154
bca7bfc8 7155 width_label = m_rowLabelWidth - rect_x;
4db6714b
KH
7156 if (width_label > rectWidth)
7157 width_label = rectWidth;
27f35b66 7158
bca7bfc8 7159 height_label = m_colLabelHeight - rect_y;
a9339fe2
DS
7160 if (height_label > rectHeight)
7161 height_label = rectHeight;
27f35b66 7162
bca7bfc8
SN
7163 if (rect_x > m_rowLabelWidth)
7164 {
7165 x = rect_x - m_rowLabelWidth;
7166 width_cell = rectWidth;
7167 }
7168 else
7169 {
7170 x = 0;
7171 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
7172 }
7173
7174 if (rect_y > m_colLabelHeight)
7175 {
7176 y = rect_y - m_colLabelHeight;
7177 height_cell = rectHeight;
7178 }
7179 else
7180 {
7181 y = 0;
7182 height_cell = rectHeight - (m_colLabelHeight - rect_y);
7183 }
7184
7185 // Paint corner label part intersecting rect.
7186 if ( width_label > 0 && height_label > 0 )
7187 {
7188 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
7189 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
7190 }
7191
7192 // Paint col labels part intersecting rect.
7193 if ( width_cell > 0 && height_label > 0 )
7194 {
7195 wxRect anotherrect(x, rect_y, width_cell, height_label);
ad805b9e 7196 m_colWindow->Refresh(eraseb, &anotherrect);
bca7bfc8
SN
7197 }
7198
7199 // Paint row labels part intersecting rect.
7200 if ( width_label > 0 && height_cell > 0 )
7201 {
7202 wxRect anotherrect(rect_x, y, width_label, height_cell);
7203 m_rowLabelWin->Refresh(eraseb, &anotherrect);
7204 }
7205
7206 // Paint cell area part intersecting rect.
7207 if ( width_cell > 0 && height_cell > 0 )
7208 {
7209 wxRect anotherrect(x, y, width_cell, height_cell);
7210 m_gridWin->Refresh(eraseb, &anotherrect);
7211 }
7212 }
7213 else
7214 {
7215 m_cornerLabelWin->Refresh(eraseb, NULL);
ad805b9e 7216 m_colWindow->Refresh(eraseb, NULL);
bca7bfc8
SN
7217 m_rowLabelWin->Refresh(eraseb, NULL);
7218 m_gridWin->Refresh(eraseb, NULL);
7219 }
27f35b66
SN
7220 }
7221}
f85afd4e 7222
c71b2126 7223void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
f85afd4e 7224{
b93aafab
JS
7225 if (m_targetWindow != this) // check whether initialisation has been done
7226 {
f1ff7df0
VZ
7227 // reposition our children windows
7228 CalcWindowSizes();
b93aafab 7229 }
f85afd4e
MB
7230}
7231
2d66e025 7232void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 7233{
2d66e025 7234 if ( m_inOnKeyDown )
f85afd4e 7235 {
2d66e025
MB
7236 // shouldn't be here - we are going round in circles...
7237 //
07296f0b 7238 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
7239 }
7240
ca65c044 7241 m_inOnKeyDown = true;
f85afd4e 7242
2d66e025 7243 // propagate the event up and see if it gets processed
2d66e025
MB
7244 wxWindow *parent = GetParent();
7245 wxKeyEvent keyEvt( event );
7246 keyEvt.SetEventObject( parent );
7247
7248 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 7249 {
bcb614b3
RR
7250 if (GetLayoutDirection() == wxLayout_RightToLeft)
7251 {
7252 if (event.GetKeyCode() == WXK_RIGHT)
7253 event.m_keyCode = WXK_LEFT;
7254 else if (event.GetKeyCode() == WXK_LEFT)
7255 event.m_keyCode = WXK_RIGHT;
7256 }
c71b2126 7257
2d66e025 7258 // try local handlers
12a3f227 7259 switch ( event.GetKeyCode() )
2d66e025
MB
7260 {
7261 case WXK_UP:
7262 if ( event.ControlDown() )
5c8fc7c1 7263 MoveCursorUpBlock( event.ShiftDown() );
2d66e025 7264 else
5c8fc7c1 7265 MoveCursorUp( event.ShiftDown() );
2d66e025 7266 break;
f85afd4e 7267
2d66e025
MB
7268 case WXK_DOWN:
7269 if ( event.ControlDown() )
5c8fc7c1 7270 MoveCursorDownBlock( event.ShiftDown() );
2d66e025 7271 else
5c8fc7c1 7272 MoveCursorDown( event.ShiftDown() );
2d66e025 7273 break;
8f177c8e 7274
2d66e025
MB
7275 case WXK_LEFT:
7276 if ( event.ControlDown() )
5c8fc7c1 7277 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025 7278 else
5c8fc7c1 7279 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
7280 break;
7281
7282 case WXK_RIGHT:
7283 if ( event.ControlDown() )
5c8fc7c1 7284 MoveCursorRightBlock( event.ShiftDown() );
2d66e025 7285 else
5c8fc7c1 7286 MoveCursorRight( event.ShiftDown() );
2d66e025 7287 break;
b99be8fb 7288
2d66e025 7289 case WXK_RETURN:
a4f7bf58 7290 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
7291 if ( event.ControlDown() )
7292 {
7293 event.Skip(); // to let the edit control have the return
7294 }
7295 else
7296 {
f6bcfd97
BP
7297 if ( GetGridCursorRow() < GetNumberRows()-1 )
7298 {
7299 MoveCursorDown( event.ShiftDown() );
7300 }
7301 else
7302 {
7303 // at the bottom of a column
13f6e9e8 7304 DisableCellEditControl();
f6bcfd97 7305 }
58dd5b3b 7306 }
2d66e025
MB
7307 break;
7308
5c8fc7c1 7309 case WXK_ESCAPE:
e32352cf 7310 ClearSelection();
5c8fc7c1
SN
7311 break;
7312
2c9a89e0
RD
7313 case WXK_TAB:
7314 if (event.ShiftDown())
f6bcfd97
BP
7315 {
7316 if ( GetGridCursorCol() > 0 )
7317 {
ca65c044 7318 MoveCursorLeft( false );
f6bcfd97
BP
7319 }
7320 else
7321 {
7322 // at left of grid
13f6e9e8 7323 DisableCellEditControl();
f6bcfd97
BP
7324 }
7325 }
2c9a89e0 7326 else
f6bcfd97 7327 {
ccdee36f 7328 if ( GetGridCursorCol() < GetNumberCols() - 1 )
f6bcfd97 7329 {
ca65c044 7330 MoveCursorRight( false );
f6bcfd97
BP
7331 }
7332 else
7333 {
7334 // at right of grid
13f6e9e8 7335 DisableCellEditControl();
f6bcfd97
BP
7336 }
7337 }
2c9a89e0
RD
7338 break;
7339
2d66e025
MB
7340 case WXK_HOME:
7341 if ( event.ControlDown() )
7342 {
8a3e536c 7343 GoToCell(0, 0);
2d66e025
MB
7344 }
7345 else
7346 {
7347 event.Skip();
7348 }
7349 break;
7350
7351 case WXK_END:
7352 if ( event.ControlDown() )
7353 {
8a3e536c 7354 GoToCell(m_numRows - 1, m_numCols - 1);
2d66e025
MB
7355 }
7356 else
7357 {
7358 event.Skip();
7359 }
7360 break;
7361
faa94f3e 7362 case WXK_PAGEUP:
2d66e025
MB
7363 MovePageUp();
7364 break;
7365
faa94f3e 7366 case WXK_PAGEDOWN:
2d66e025
MB
7367 MovePageDown();
7368 break;
7369
07296f0b 7370 case WXK_SPACE:
32b4e9ec
VZ
7371 // Ctrl-Space selects the current column, Shift-Space -- the
7372 // current row and Ctrl-Shift-Space -- everything
7373 switch ( m_selection ? event.GetModifiers() : wxMOD_NONE )
aa5e1f75 7374 {
32b4e9ec
VZ
7375 case wxMOD_CONTROL:
7376 m_selection->SelectCol(m_currentCellCoords.GetCol());
7377 break;
ccdee36f 7378
32b4e9ec
VZ
7379 case wxMOD_SHIFT:
7380 m_selection->SelectRow(m_currentCellCoords.GetRow());
7381 break;
7382
7383 case wxMOD_CONTROL | wxMOD_SHIFT:
7384 m_selection->SelectBlock(0, 0,
7385 m_numRows - 1, m_numCols - 1);
7386 break;
7387
7388 case wxMOD_NONE:
7389 if ( !IsEditable() )
7390 {
7391 MoveCursorRight(false);
7392 break;
7393 }
7394 //else: fall through
7395
7396 default:
7397 event.Skip();
7398 }
ccdee36f 7399 break;
07296f0b 7400
2d66e025 7401 default:
63e2147c 7402 event.Skip();
025562fe 7403 break;
2d66e025 7404 }
f85afd4e
MB
7405 }
7406
ca65c044 7407 m_inOnKeyDown = false;
f85afd4e
MB
7408}
7409
f6bcfd97
BP
7410void wxGrid::OnKeyUp( wxKeyEvent& event )
7411{
7412 // try local handlers
7413 //
12a3f227 7414 if ( event.GetKeyCode() == WXK_SHIFT )
f6bcfd97 7415 {
8a3e536c
VZ
7416 if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
7417 m_selectedBlockBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
7418 {
7419 if ( m_selection )
7420 {
a9339fe2 7421 m_selection->SelectBlock(
8a3e536c
VZ
7422 m_selectedBlockTopLeft,
7423 m_selectedBlockBottomRight,
8b5f6d9d 7424 event);
3f3dc2ef
VZ
7425 }
7426 }
7427
8a3e536c
VZ
7428 m_selectedBlockTopLeft = wxGridNoCellCoords;
7429 m_selectedBlockBottomRight = wxGridNoCellCoords;
7430 m_selectedBlockCorner = wxGridNoCellCoords;
f6bcfd97
BP
7431 }
7432}
7433
63e2147c
RD
7434void wxGrid::OnChar( wxKeyEvent& event )
7435{
7436 // is it possible to edit the current cell at all?
7437 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7438 {
7439 // yes, now check whether the cells editor accepts the key
7440 int row = m_currentCellCoords.GetRow();
7441 int col = m_currentCellCoords.GetCol();
2f024384 7442 wxGridCellAttr *attr = GetCellAttr(row, col);
63e2147c
RD
7443 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7444
7445 // <F2> is special and will always start editing, for
7446 // other keys - ask the editor itself
7447 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7448 || editor->IsAcceptedKey(event) )
7449 {
7450 // ensure cell is visble
7451 MakeCellVisible(row, col);
7452 EnableCellEditControl();
7453
7454 // a problem can arise if the cell is not completely
7455 // visible (even after calling MakeCellVisible the
7456 // control is not created and calling StartingKey will
7457 // crash the app
046d682f 7458 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
63e2147c
RD
7459 editor->StartingKey(event);
7460 }
7461 else
7462 {
7463 event.Skip();
7464 }
7465
7466 editor->DecRef();
7467 attr->DecRef();
7468 }
7469 else
7470 {
7471 event.Skip();
7472 }
7473}
7474
2796cce3 7475void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
7476{
7477}
07296f0b 7478
8a3e536c 7479bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 7480{
8a3e536c 7481 if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 )
f6bcfd97 7482 {
8a3e536c
VZ
7483 // the event has been vetoed - do nothing
7484 return false;
f6bcfd97
BP
7485 }
7486
9553702e 7487#if !defined(__WXMAC__)
bee19958
DS
7488 wxClientDC dc( m_gridWin );
7489 PrepareDC( dc );
5d38a5f3 7490#endif
f6bcfd97 7491
2a7750d9 7492 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 7493 {
b54ba671 7494 DisableCellEditControl();
07296f0b 7495
ca65c044 7496 if ( IsVisible( m_currentCellCoords, false ) )
f6bcfd97
BP
7497 {
7498 wxRect r;
bee19958 7499 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
f6bcfd97
BP
7500 if ( !m_gridLinesEnabled )
7501 {
7502 r.x--;
7503 r.y--;
7504 r.width++;
7505 r.height++;
7506 }
d1c0b4f9 7507
3ed884a0 7508 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 7509
f6bcfd97
BP
7510 // Otherwise refresh redraws the highlight!
7511 m_currentCellCoords = coords;
275c4ae4 7512
9553702e 7513#if defined(__WXMAC__)
5d38a5f3
JS
7514 m_gridWin->Refresh(true /*, & r */);
7515#else
bee19958 7516 DrawGridCellArea( dc, cells );
f6bcfd97 7517 DrawAllGridLines( dc, r );
5d38a5f3 7518#endif
f6bcfd97 7519 }
66242c80 7520 }
8f177c8e 7521
2d66e025
MB
7522 m_currentCellCoords = coords;
7523
bee19958 7524 wxGridCellAttr *attr = GetCellAttr( coords );
76c66f19 7525#if !defined(__WXMAC__)
bee19958 7526 DrawCellHighlight( dc, attr );
5d38a5f3 7527#endif
2a7750d9 7528 attr->DecRef();
8a3e536c
VZ
7529
7530 return true;
66242c80
MB
7531}
7532
8a3e536c
VZ
7533void
7534wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
7535 int bottomRow, int rightCol)
c9097836 7536{
3f3dc2ef 7537 if ( m_selection )
c9097836 7538 {
8a3e536c 7539 switch ( m_selection->GetSelectionMode() )
3f3dc2ef 7540 {
8a3e536c
VZ
7541 default:
7542 wxFAIL_MSG( "unknown selection mode" );
7543 // fall through
7544
7545 case wxGridSelectCells:
7546 // arbitrary blocks selection allowed so just use the cell
7547 // coordinates as is
7548 break;
7549
7550 case wxGridSelectRows:
7551 // only full rows selection allowd, ensure that we do select
7552 // full rows
7553 leftCol = 0;
7554 rightCol = GetNumberCols() - 1;
7555 break;
7556
7557 case wxGridSelectColumns:
7558 // same as above but for columns
7559 topRow = 0;
7560 bottomRow = GetNumberRows() - 1;
7561 break;
7562
7563 case wxGridSelectRowsOrColumns:
7564 // in this mode we can select only full rows or full columns so
7565 // it doesn't make sense to select blocks at all (and we can't
7566 // extend the block because there is no preferred direction, we
7567 // could only extend it to cover the entire grid but this is
7568 // not useful)
7569 return;
3f3dc2ef 7570 }
c9097836 7571 }
3f3dc2ef 7572
8a3e536c
VZ
7573 m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol);
7574 MakeCellVisible(m_selectedBlockCorner);
7575
1372f8cc
VZ
7576 EnsureFirstLessThanSecond(topRow, bottomRow);
7577 EnsureFirstLessThanSecond(leftCol, rightCol);
c9097836 7578
8a3e536c
VZ
7579 wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol),
7580 updateBottomRight = wxGridCellCoords(bottomRow, rightCol);
c9097836 7581
3ed884a0 7582 // First the case that we selected a completely new area
8a3e536c
VZ
7583 if ( m_selectedBlockTopLeft == wxGridNoCellCoords ||
7584 m_selectedBlockBottomRight == wxGridNoCellCoords )
3ed884a0
SN
7585 {
7586 wxRect rect;
7587 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7588 wxGridCellCoords ( bottomRow, rightCol ) );
ca65c044 7589 m_gridWin->Refresh( false, &rect );
3ed884a0 7590 }
2f024384 7591
3ed884a0 7592 // Now handle changing an existing selection area.
8a3e536c
VZ
7593 else if ( m_selectedBlockTopLeft != updateTopLeft ||
7594 m_selectedBlockBottomRight != updateBottomRight )
c9097836
MB
7595 {
7596 // Compute two optimal update rectangles:
7597 // Either one rectangle is a real subset of the
7598 // other, or they are (almost) disjoint!
7599 wxRect rect[4];
7600 bool need_refresh[4];
7601 need_refresh[0] =
7602 need_refresh[1] =
7603 need_refresh[2] =
ca65c044 7604 need_refresh[3] = false;
c9097836
MB
7605 int i;
7606
7607 // Store intermediate values
8a3e536c
VZ
7608 wxCoord oldLeft = m_selectedBlockTopLeft.GetCol();
7609 wxCoord oldTop = m_selectedBlockTopLeft.GetRow();
7610 wxCoord oldRight = m_selectedBlockBottomRight.GetCol();
7611 wxCoord oldBottom = m_selectedBlockBottomRight.GetRow();
c9097836
MB
7612
7613 // Determine the outer/inner coordinates.
1372f8cc
VZ
7614 EnsureFirstLessThanSecond(oldLeft, leftCol);
7615 EnsureFirstLessThanSecond(oldTop, topRow);
7616 EnsureFirstLessThanSecond(rightCol, oldRight);
7617 EnsureFirstLessThanSecond(bottomRow, oldBottom);
c9097836
MB
7618
7619 // Now, either the stuff marked old is the outer
7620 // rectangle or we don't have a situation where one
7621 // is contained in the other.
7622
7623 if ( oldLeft < leftCol )
7624 {
3ed884a0
SN
7625 // Refresh the newly selected or deselected
7626 // area to the left of the old or new selection.
ca65c044 7627 need_refresh[0] = true;
a9339fe2
DS
7628 rect[0] = BlockToDeviceRect(
7629 wxGridCellCoords( oldTop, oldLeft ),
7630 wxGridCellCoords( oldBottom, leftCol - 1 ) );
c9097836
MB
7631 }
7632
2f024384 7633 if ( oldTop < topRow )
c9097836 7634 {
3ed884a0
SN
7635 // Refresh the newly selected or deselected
7636 // area above the old or new selection.
ca65c044 7637 need_refresh[1] = true;
2f024384
DS
7638 rect[1] = BlockToDeviceRect(
7639 wxGridCellCoords( oldTop, leftCol ),
7640 wxGridCellCoords( topRow - 1, rightCol ) );
c9097836
MB
7641 }
7642
7643 if ( oldRight > rightCol )
7644 {
3ed884a0
SN
7645 // Refresh the newly selected or deselected
7646 // area to the right of the old or new selection.
ca65c044 7647 need_refresh[2] = true;
2f024384 7648 rect[2] = BlockToDeviceRect(
a9339fe2
DS
7649 wxGridCellCoords( oldTop, rightCol + 1 ),
7650 wxGridCellCoords( oldBottom, oldRight ) );
c9097836
MB
7651 }
7652
7653 if ( oldBottom > bottomRow )
7654 {
3ed884a0
SN
7655 // Refresh the newly selected or deselected
7656 // area below the old or new selection.
ca65c044 7657 need_refresh[3] = true;
2f024384 7658 rect[3] = BlockToDeviceRect(
a9339fe2
DS
7659 wxGridCellCoords( bottomRow + 1, leftCol ),
7660 wxGridCellCoords( oldBottom, rightCol ) );
c9097836
MB
7661 }
7662
c9097836
MB
7663 // various Refresh() calls
7664 for (i = 0; i < 4; i++ )
7665 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
ca65c044 7666 m_gridWin->Refresh( false, &(rect[i]) );
c9097836 7667 }
2f024384
DS
7668
7669 // change selection
8a3e536c
VZ
7670 m_selectedBlockTopLeft = updateTopLeft;
7671 m_selectedBlockBottomRight = updateBottomRight;
c9097836
MB
7672}
7673
2d66e025
MB
7674//
7675// ------ functions to get/send data (see also public functions)
7676//
7677
7678bool wxGrid::GetModelValues()
66242c80 7679{
bca7bfc8
SN
7680 // Hide the editor, so it won't hide a changed value.
7681 HideCellEditControl();
c6707d16 7682
2d66e025 7683 if ( m_table )
66242c80 7684 {
2d66e025 7685 // all we need to do is repaint the grid
66242c80 7686 //
2d66e025 7687 m_gridWin->Refresh();
ca65c044 7688 return true;
66242c80 7689 }
8f177c8e 7690
ca65c044 7691 return false;
66242c80
MB
7692}
7693
2d66e025 7694bool wxGrid::SetModelValues()
f85afd4e 7695{
2d66e025 7696 int row, col;
8f177c8e 7697
c6707d16
SN
7698 // Disable the editor, so it won't hide a changed value.
7699 // Do we also want to save the current value of the editor first?
7700 // I think so ...
c6707d16
SN
7701 DisableCellEditControl();
7702
2d66e025
MB
7703 if ( m_table )
7704 {
56b6cf26 7705 for ( row = 0; row < m_numRows; row++ )
f85afd4e 7706 {
56b6cf26 7707 for ( col = 0; col < m_numCols; col++ )
f85afd4e 7708 {
2d66e025 7709 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
7710 }
7711 }
8f177c8e 7712
ca65c044 7713 return true;
f85afd4e
MB
7714 }
7715
ca65c044 7716 return false;
f85afd4e
MB
7717}
7718
2d66e025
MB
7719// Note - this function only draws cells that are in the list of
7720// exposed cells (usually set from the update region by
7721// CalcExposedCells)
7722//
d10f4bf9 7723void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 7724{
4db6714b
KH
7725 if ( !m_numRows || !m_numCols )
7726 return;
60ff3b99 7727
dc1f566f 7728 int i, numCells = cells.GetCount();
27f35b66
SN
7729 int row, col, cell_rows, cell_cols;
7730 wxGridCellCoordsArray redrawCells;
60ff3b99 7731
a9339fe2 7732 for ( i = numCells - 1; i >= 0; i-- )
f85afd4e 7733 {
27f35b66
SN
7734 row = cells[i].GetRow();
7735 col = cells[i].GetCol();
7736 GetCellSize( row, col, &cell_rows, &cell_cols );
7737
7738 // If this cell is part of a multicell block, find owner for repaint
7739 if ( cell_rows <= 0 || cell_cols <= 0 )
7740 {
a9339fe2 7741 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
ca65c044 7742 bool marked = false;
56b6cf26 7743 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
7744 {
7745 if ( cell == cells[j] )
7746 {
ca65c044 7747 marked = true;
3ed884a0 7748 break;
27f35b66
SN
7749 }
7750 }
2f024384 7751
27f35b66
SN
7752 if (!marked)
7753 {
7754 int count = redrawCells.GetCount();
dc1f566f 7755 for (int j = 0; j < count; j++)
27f35b66
SN
7756 {
7757 if ( cell == redrawCells[j] )
7758 {
ca65c044 7759 marked = true;
27f35b66
SN
7760 break;
7761 }
7762 }
2f024384 7763
4db6714b
KH
7764 if (!marked)
7765 redrawCells.Add( cell );
27f35b66 7766 }
2f024384
DS
7767
7768 // don't bother drawing this cell
7769 continue;
27f35b66
SN
7770 }
7771
7772 // If this cell is empty, find cell to left that might want to overflow
7773 if (m_table && m_table->IsEmptyCell(row, col))
7774 {
dc1f566f 7775 for ( int l = 0; l < cell_rows; l++ )
27f35b66 7776 {
56b6cf26 7777 // find a cell in this row to leave already marked for repaint
dc1f566f
SN
7778 int left = col;
7779 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7780 if ((redrawCells[k].GetCol() < left) &&
7781 (redrawCells[k].GetRow() == row))
2f024384 7782 {
4db6714b 7783 left = redrawCells[k].GetCol();
2f024384 7784 }
dc1f566f 7785
4db6714b
KH
7786 if (left == col)
7787 left = 0; // oh well
dc1f566f 7788
2f024384 7789 for (int j = col - 1; j >= left; j--)
27f35b66 7790 {
2f024384 7791 if (!m_table->IsEmptyCell(row + l, j))
27f35b66 7792 {
2f024384 7793 if (GetCellOverflow(row + l, j))
27f35b66 7794 {
2f024384 7795 wxGridCellCoords cell(row + l, j);
ca65c044 7796 bool marked = false;
27f35b66 7797
dc1f566f 7798 for (int k = 0; k < numCells; k++)
27f35b66
SN
7799 {
7800 if ( cell == cells[k] )
7801 {
ca65c044 7802 marked = true;
27f35b66
SN
7803 break;
7804 }
7805 }
4db6714b 7806
27f35b66
SN
7807 if (!marked)
7808 {
7809 int count = redrawCells.GetCount();
dc1f566f 7810 for (int k = 0; k < count; k++)
27f35b66
SN
7811 {
7812 if ( cell == redrawCells[k] )
7813 {
ca65c044 7814 marked = true;
27f35b66
SN
7815 break;
7816 }
7817 }
4db6714b
KH
7818 if (!marked)
7819 redrawCells.Add( cell );
27f35b66
SN
7820 }
7821 }
7822 break;
7823 }
7824 }
7825 }
7826 }
4db6714b 7827
d10f4bf9 7828 DrawCell( dc, cells[i] );
2d66e025 7829 }
27f35b66
SN
7830
7831 numCells = redrawCells.GetCount();
7832
56b6cf26 7833 for ( i = numCells - 1; i >= 0; i-- )
27f35b66
SN
7834 {
7835 DrawCell( dc, redrawCells[i] );
7836 }
2d66e025 7837}
8f177c8e 7838
7c8a8ad5
MB
7839void wxGrid::DrawGridSpace( wxDC& dc )
7840{
f6bcfd97
BP
7841 int cw, ch;
7842 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 7843
f6bcfd97
BP
7844 int right, bottom;
7845 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 7846
d4175745 7847 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
2f024384 7848 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7c8a8ad5 7849
f6bcfd97
BP
7850 if ( right > rightCol || bottom > bottomRow )
7851 {
7852 int left, top;
7853 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 7854
ff72f628 7855 dc.SetBrush(GetDefaultCellBackgroundColour());
f6bcfd97 7856 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 7857
f6bcfd97
BP
7858 if ( right > rightCol )
7859 {
a9339fe2 7860 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
f6bcfd97
BP
7861 }
7862
7863 if ( bottom > bottomRow )
7864 {
a9339fe2 7865 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
f6bcfd97
BP
7866 }
7867 }
7c8a8ad5
MB
7868}
7869
2d66e025
MB
7870void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7871{
ab79958a
VZ
7872 int row = coords.GetRow();
7873 int col = coords.GetCol();
60ff3b99 7874
7c1cb261 7875 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
7876 return;
7877
7878 // we draw the cell border ourselves
189d0213
VZ
7879 wxGridCellAttr* attr = GetCellAttr(row, col);
7880
7881 bool isCurrent = coords == m_currentCellCoords;
508011ce 7882
f6bcfd97 7883 wxRect rect = CellToRect( row, col );
f85afd4e 7884
189d0213 7885 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
7886 // Note: However, only if it is really _shown_, i.e. not hidden!
7887 if ( isCurrent && IsCellEditControlShown() )
189d0213 7888 {
a9339fe2 7889 // NB: this "#if..." is temporary and fixes a problem where the
962a48f6
DS
7890 // edit control is erased by this code after being rendered.
7891 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7892 // implicitly, causing this out-of order render.
5d38a5f3 7893#if !defined(__WXMAC__)
0b190b0f
VZ
7894 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7895 editor->PaintBackground(rect, attr);
7896 editor->DecRef();
962a48f6 7897#endif
189d0213
VZ
7898 }
7899 else
7900 {
a9339fe2 7901 // but all the rest is drawn by the cell renderer and hence may be customized
0b190b0f
VZ
7902 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7903 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7904 renderer->DecRef();
189d0213 7905 }
07296f0b 7906
283b7808
VZ
7907 attr->DecRef();
7908}
07296f0b 7909
283b7808 7910void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b 7911{
760be3f7
VS
7912 // don't show highlight when the grid doesn't have focus
7913 if ( !HasFocus() )
7914 return;
7915
07296f0b
RD
7916 int row = m_currentCellCoords.GetRow();
7917 int col = m_currentCellCoords.GetCol();
7918
7c1cb261 7919 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
7920 return;
7921
f6bcfd97 7922 wxRect rect = CellToRect(row, col);
07296f0b 7923
b3a7510d
VZ
7924 // hmmm... what could we do here to show that the cell is disabled?
7925 // for now, I just draw a thinner border than for the other ones, but
7926 // it doesn't look really good
b3a7510d 7927
d2520c85
RD
7928 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7929
27f35b66
SN
7930 if (penWidth > 0)
7931 {
56b6cf26
DS
7932 // The center of the drawn line is where the position/width/height of
7933 // the rectangle is actually at (on wxMSW at least), so the
7934 // size of the rectangle is reduced to compensate for the thickness of
7935 // the line. If this is too strange on non-wxMSW platforms then
d2520c85 7936 // please #ifdef this appropriately.
4db6714b
KH
7937 rect.x += penWidth / 2;
7938 rect.y += penWidth / 2;
7939 rect.width -= penWidth - 1;
7940 rect.height -= penWidth - 1;
d2520c85 7941
d2520c85 7942 // Now draw the rectangle
73145b0e
JS
7943 // use the cellHighlightColour if the cell is inside a selection, this
7944 // will ensure the cell is always visible.
ff72f628
VZ
7945 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground
7946 : m_cellHighlightColour,
7947 penWidth));
d2520c85
RD
7948 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7949 dc.DrawRectangle(rect);
7950 }
2d66e025 7951}
f85afd4e 7952
3d3f3e37
VZ
7953wxPen wxGrid::GetDefaultGridLinePen()
7954{
ff72f628 7955 return wxPen(GetGridLineColour());
3d3f3e37
VZ
7956}
7957
7958wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7959{
7960 return GetDefaultGridLinePen();
7961}
7962
7963wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7964{
7965 return GetDefaultGridLinePen();
7966}
7967
2d66e025
MB
7968void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7969{
2d66e025
MB
7970 int row = coords.GetRow();
7971 int col = coords.GetCol();
7c1cb261
VZ
7972 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7973 return;
7974
60ff3b99 7975
27f35b66
SN
7976 wxRect rect = CellToRect( row, col );
7977
2d66e025 7978 // right hand border
3d3f3e37 7979 dc.SetPen( GetColGridLinePen(col) );
27f35b66
SN
7980 dc.DrawLine( rect.x + rect.width, rect.y,
7981 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
7982
7983 // bottom border
3d3f3e37 7984 dc.SetPen( GetRowGridLinePen(row) );
2f024384 7985 dc.DrawLine( rect.x, rect.y + rect.height,
27f35b66 7986 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
7987}
7988
2f024384 7989void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
b3a7510d 7990{
2a7750d9
MB
7991 // This if block was previously in wxGrid::OnPaint but that doesn't
7992 // seem to get called under wxGTK - MB
7993 //
2f024384 7994 if ( m_currentCellCoords == wxGridNoCellCoords &&
2a7750d9
MB
7995 m_numRows && m_numCols )
7996 {
7997 m_currentCellCoords.Set(0, 0);
7998 }
7999
f6bcfd97 8000 if ( IsCellEditControlShown() )
99306db2
VZ
8001 {
8002 // don't show highlight when the edit control is shown
8003 return;
8004 }
8005
b3a7510d
VZ
8006 // if the active cell was repainted, repaint its highlight too because it
8007 // might have been damaged by the grid lines
d10f4bf9 8008 size_t count = cells.GetCount();
b3a7510d
VZ
8009 for ( size_t n = 0; n < count; n++ )
8010 {
54181a33
VZ
8011 wxGridCellCoords cell = cells[n];
8012
8013 // If we are using attributes, then we may have just exposed another
8014 // cell in a partially-visible merged cluster of cells. If the "anchor"
8015 // (upper left) cell of this merged cluster is the cell indicated by
8016 // m_currentCellCoords, then we need to refresh the cell highlight even
8017 // though the "anchor" itself is not part of our update segment.
8018 if ( CanHaveAttributes() )
8019 {
8020 int rows = 0,
8021 cols = 0;
8022 GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols);
8023
8024 if ( rows < 0 )
8025 cell.SetRow(cell.GetRow() + rows);
8026
8027 if ( cols < 0 )
8028 cell.SetCol(cell.GetCol() + cols);
8029 }
8030
8031 if ( cell == m_currentCellCoords )
b3a7510d
VZ
8032 {
8033 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
8034 DrawCellHighlight(dc, attr);
8035 attr->DecRef();
8036
8037 break;
8038 }
8039 }
8040}
2d66e025 8041
2d66e025
MB
8042// This is used to redraw all grid lines e.g. when the grid line colour
8043// has been changed
8044//
33ac7e6f 8045void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 8046{
9f7aee01 8047 if ( !m_gridLinesEnabled )
4db6714b 8048 return;
f85afd4e 8049
2d66e025 8050 int top, bottom, left, right;
796df70a 8051
ff72f628
VZ
8052 int cw, ch;
8053 m_gridWin->GetClientSize(&cw, &ch);
8054 CalcUnscrolledPosition( 0, 0, &left, &top );
8055 CalcUnscrolledPosition( cw, ch, &right, &bottom );
f85afd4e 8056
9496deb5 8057 // avoid drawing grid lines past the last row and col
9f7aee01
VZ
8058 if ( m_gridLinesClipHorz )
8059 {
8060 if ( !m_numCols )
8061 return;
8062
8063 const int lastColRight = GetColRight(GetColAt(m_numCols - 1));
8064 if ( right > lastColRight )
8065 right = lastColRight;
8066 }
8067
8068 if ( m_gridLinesClipVert )
8069 {
8070 if ( !m_numRows )
8071 return;
8072
8073 const int lastRowBottom = GetRowBottom(m_numRows - 1);
8074 if ( bottom > lastRowBottom )
8075 bottom = lastRowBottom;
8076 }
9496deb5 8077
27f35b66 8078 // no gridlines inside multicells, clip them out
d4175745 8079 int leftCol = GetColPos( internalXToCol(left) );
a9339fe2 8080 int topRow = internalYToRow(top);
d4175745 8081 int rightCol = GetColPos( internalXToCol(right) );
a967f048 8082 int bottomRow = internalYToRow(bottom);
27f35b66 8083
c03bf0c7 8084 wxRegion clippedcells(0, 0, cw, ch);
27f35b66 8085
ff72f628 8086 int cell_rows, cell_cols;
a967f048 8087 wxRect rect;
27f35b66 8088
ff72f628 8089 for ( int j = topRow; j <= bottomRow; j++ )
a967f048 8090 {
ff72f628 8091 for ( int colPos = leftCol; colPos <= rightCol; colPos++ )
27f35b66 8092 {
ff72f628 8093 int i = GetColAt( colPos );
d4175745 8094
a967f048
RG
8095 GetCellSize( j, i, &cell_rows, &cell_cols );
8096 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66 8097 {
a967f048
RG
8098 rect = CellToRect(j,i);
8099 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8100 clippedcells.Subtract(rect);
8101 }
8102 else if ((cell_rows < 0) || (cell_cols < 0))
8103 {
a9339fe2 8104 rect = CellToRect(j + cell_rows, i + cell_cols);
a967f048
RG
8105 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8106 clippedcells.Subtract(rect);
27f35b66
SN
8107 }
8108 }
8109 }
a9339fe2 8110
c1bc8d9f 8111 dc.SetDeviceClippingRegion( clippedcells );
27f35b66 8112
f85afd4e 8113
2d66e025 8114 // horizontal grid lines
ff72f628 8115 for ( int i = internalYToRow(top); i < m_numRows; i++ )
f85afd4e 8116 {
6d55126d 8117 int bot = GetRowBottom(i) - 1;
7c1cb261 8118
6d55126d 8119 if ( bot > bottom )
2d66e025 8120 break;
7c1cb261 8121
6d55126d 8122 if ( bot >= top )
2d66e025 8123 {
3d3f3e37 8124 dc.SetPen( GetRowGridLinePen(i) );
6d55126d 8125 dc.DrawLine( left, bot, right, bot );
2d66e025 8126 }
f85afd4e
MB
8127 }
8128
2d66e025 8129 // vertical grid lines
ff72f628 8130 for ( int colPos = leftCol; colPos < m_numCols; colPos++ )
f85afd4e 8131 {
ff72f628 8132 int i = GetColAt( colPos );
d4175745 8133
2121eb69
RR
8134 int colRight = GetColRight(i);
8135#ifdef __WXGTK__
8136 if (GetLayoutDirection() != wxLayout_RightToLeft)
8137#endif
8138 colRight--;
8139
7c1cb261 8140 if ( colRight > right )
2d66e025 8141 break;
7c1cb261
VZ
8142
8143 if ( colRight >= left )
2d66e025 8144 {
3d3f3e37 8145 dc.SetPen( GetColGridLinePen(i) );
7c1cb261 8146 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
8147 }
8148 }
a9339fe2 8149
27f35b66 8150 dc.DestroyClippingRegion();
2d66e025 8151}
f85afd4e 8152
c2f5b920 8153void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
2d66e025 8154{
4db6714b
KH
8155 if ( !m_numRows )
8156 return;
60ff3b99 8157
ff72f628
VZ
8158 const size_t numLabels = rows.GetCount();
8159 for ( size_t i = 0; i < numLabels; i++ )
2d66e025 8160 {
d10f4bf9 8161 DrawRowLabel( dc, rows[i] );
60ff3b99 8162 }
f85afd4e
MB
8163}
8164
2d66e025 8165void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 8166{
659af826 8167 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7c1cb261 8168 return;
60ff3b99 8169
4d1bc39c 8170 wxRect rect;
2f024384 8171
7c1cb261
VZ
8172 int rowTop = GetRowTop(row),
8173 rowBottom = GetRowBottom(row) - 1;
b99be8fb 8174
ff72f628 8175 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
a9339fe2 8176 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
73145b0e 8177 dc.DrawLine( 0, rowTop, 0, rowBottom );
73145b0e 8178 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
b99be8fb 8179
2d66e025 8180 dc.SetPen( *wxWHITE_PEN );
73145b0e 8181 dc.DrawLine( 1, rowTop, 1, rowBottom );
a9339fe2 8182 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
2f024384 8183
04ee05f9 8184 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
f85afd4e
MB
8185 dc.SetTextForeground( GetLabelTextColour() );
8186 dc.SetFont( GetLabelFont() );
8f177c8e 8187
f85afd4e 8188 int hAlign, vAlign;
2d66e025 8189 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 8190
2d66e025 8191 rect.SetX( 2 );
7c1cb261 8192 rect.SetY( GetRowTop(row) + 2 );
2d66e025 8193 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 8194 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 8195 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
8196}
8197
ad805b9e
VZ
8198void wxGrid::UseNativeColHeader(bool native)
8199{
8200 if ( native == m_useNativeHeader )
8201 return;
8202
8203 delete m_colWindow;
8204 m_useNativeHeader = native;
8205
8206 CreateColumnWindow();
8207
8208 if ( m_useNativeHeader )
3039ade9 8209 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e
VZ
8210 CalcWindowSizes();
8211}
8212
71cf399f
RR
8213void wxGrid::SetUseNativeColLabels( bool native )
8214{
ad805b9e
VZ
8215 wxASSERT_MSG( !m_useNativeHeader,
8216 "doesn't make sense when using native header" );
8217
71cf399f
RR
8218 m_nativeColumnLabels = native;
8219 if (native)
8220 {
8221 int height = wxRendererNative::Get().GetHeaderButtonHeight( this );
8222 SetColLabelSize( height );
8223 }
76c66f19 8224
ad805b9e 8225 GetColLabelWindow()->Refresh();
ff72f628 8226 m_cornerLabelWin->Refresh();
71cf399f
RR
8227}
8228
d10f4bf9 8229void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 8230{
4db6714b
KH
8231 if ( !m_numCols )
8232 return;
60ff3b99 8233
ff72f628
VZ
8234 const size_t numLabels = cols.GetCount();
8235 for ( size_t i = 0; i < numLabels; i++ )
f85afd4e 8236 {
d10f4bf9 8237 DrawColLabel( dc, cols[i] );
60ff3b99 8238 }
f85afd4e
MB
8239}
8240
ff72f628
VZ
8241void wxGrid::DrawCornerLabel(wxDC& dc)
8242{
8243 if ( m_nativeColumnLabels )
8244 {
8245 wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight));
8246 rect.Deflate(1);
8247
8248 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0);
8249 }
8250 else
8251 {
8252 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
8253 dc.DrawLine( m_rowLabelWidth - 1, m_colLabelHeight - 1,
8254 m_rowLabelWidth - 1, 0 );
8255 dc.DrawLine( m_rowLabelWidth - 1, m_colLabelHeight - 1,
8256 0, m_colLabelHeight - 1 );
8257 dc.DrawLine( 0, 0, m_rowLabelWidth, 0 );
8258 dc.DrawLine( 0, 0, 0, m_colLabelHeight );
8259
8260 dc.SetPen( *wxWHITE_PEN );
8261 dc.DrawLine( 1, 1, m_rowLabelWidth - 1, 1 );
8262 dc.DrawLine( 1, 1, 1, m_colLabelHeight - 1 );
8263 }
8264}
8265
8266void wxGrid::DrawColLabel(wxDC& dc, int col)
f85afd4e 8267{
659af826 8268 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7c1cb261 8269 return;
60ff3b99 8270
4d1bc39c 8271 int colLeft = GetColLeft(col);
ec157c8f 8272
ff72f628 8273 wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight);
2f024384 8274
ff72f628 8275 if ( m_nativeColumnLabels )
71cf399f 8276 {
11393d29
VZ
8277 wxRendererNative::Get().DrawHeaderButton
8278 (
8279 GetColLabelWindow(),
8280 dc,
8281 rect,
8282 0,
8283 IsSortingBy(col)
8284 ? IsSortOrderAscending()
8285 ? wxHDR_SORT_ICON_UP
8286 : wxHDR_SORT_ICON_DOWN
8287 : wxHDR_SORT_ICON_NONE
8288 );
71cf399f
RR
8289 }
8290 else
8291 {
8292 int colRight = GetColRight(col) - 1;
b99be8fb 8293
ff72f628
VZ
8294 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
8295 dc.DrawLine( colRight, 0,
8296 colRight, m_colLabelHeight - 1 );
8297 dc.DrawLine( colLeft, 0,
8298 colRight, 0 );
71cf399f 8299 dc.DrawLine( colLeft, m_colLabelHeight - 1,
ff72f628 8300 colRight + 1, m_colLabelHeight - 1 );
b99be8fb 8301
71cf399f
RR
8302 dc.SetPen( *wxWHITE_PEN );
8303 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
8304 dc.DrawLine( colLeft, 1, colRight, 1 );
8305 }
2f024384 8306
04ee05f9 8307 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
b0d6ff2f
MB
8308 dc.SetTextForeground( GetLabelTextColour() );
8309 dc.SetFont( GetLabelFont() );
8f177c8e 8310
ff72f628 8311 int hAlign, vAlign;
2d66e025 8312 GetColLabelAlignment( &hAlign, &vAlign );
ff72f628 8313 const int orient = GetColLabelTextOrientation();
60ff3b99 8314
ff72f628
VZ
8315 rect.Deflate(2);
8316 DrawTextRectangle(dc, GetColLabelValue(col), rect, hAlign, vAlign, orient);
f85afd4e
MB
8317}
8318
ff72f628
VZ
8319// TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8320// we just have to add textOrientation support
2d66e025
MB
8321void wxGrid::DrawTextRectangle( wxDC& dc,
8322 const wxString& value,
8323 const wxRect& rect,
8324 int horizAlign,
d43851f7
JS
8325 int vertAlign,
8326 int textOrientation )
f85afd4e 8327{
2d66e025 8328 wxArrayString lines;
ef5df12b 8329
2d66e025 8330 StringToLines( value, lines );
ef5df12b 8331
ff72f628 8332 DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation);
d10f4bf9
VZ
8333}
8334
4330c974 8335void wxGrid::DrawTextRectangle(wxDC& dc,
038a5591
JS
8336 const wxArrayString& lines,
8337 const wxRect& rect,
8338 int horizAlign,
8339 int vertAlign,
4330c974 8340 int textOrientation)
d10f4bf9 8341{
4330c974
VZ
8342 if ( lines.empty() )
8343 return;
ef5df12b 8344
4330c974 8345 wxDCClipper clip(dc, rect);
ef5df12b 8346
4330c974
VZ
8347 long textWidth,
8348 textHeight;
ef5df12b 8349
4330c974
VZ
8350 if ( textOrientation == wxHORIZONTAL )
8351 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
8352 else
8353 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
ef5df12b 8354
4330c974
VZ
8355 int x = 0,
8356 y = 0;
8357 switch ( vertAlign )
8358 {
73145b0e 8359 case wxALIGN_BOTTOM:
4db6714b 8360 if ( textOrientation == wxHORIZONTAL )
038a5591 8361 y = rect.y + (rect.height - textHeight - 1);
d43851f7 8362 else
038a5591
JS
8363 x = rect.x + rect.width - textWidth;
8364 break;
ef5df12b 8365
73145b0e 8366 case wxALIGN_CENTRE:
4db6714b 8367 if ( textOrientation == wxHORIZONTAL )
a9339fe2 8368 y = rect.y + ((rect.height - textHeight) / 2);
d43851f7 8369 else
a9339fe2 8370 x = rect.x + ((rect.width - textWidth) / 2);
038a5591 8371 break;
ef5df12b 8372
73145b0e
JS
8373 case wxALIGN_TOP:
8374 default:
4db6714b 8375 if ( textOrientation == wxHORIZONTAL )
038a5591 8376 y = rect.y + 1;
d43851f7 8377 else
038a5591 8378 x = rect.x + 1;
73145b0e 8379 break;
4330c974 8380 }
ef5df12b 8381
4330c974
VZ
8382 // Align each line of a multi-line label
8383 size_t nLines = lines.GetCount();
8384 for ( size_t l = 0; l < nLines; l++ )
8385 {
8386 const wxString& line = lines[l];
ef5df12b 8387
9b7d3c09
VZ
8388 if ( line.empty() )
8389 {
8390 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
8391 continue;
8392 }
8393
ad2633bd 8394 wxCoord lineWidth = 0,
c2b2c10e 8395 lineHeight = 0;
4330c974
VZ
8396 dc.GetTextExtent(line, &lineWidth, &lineHeight);
8397
8398 switch ( horizAlign )
8399 {
038a5591 8400 case wxALIGN_RIGHT:
4db6714b 8401 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
8402 x = rect.x + (rect.width - lineWidth - 1);
8403 else
8404 y = rect.y + lineWidth + 1;
8405 break;
ef5df12b 8406
038a5591 8407 case wxALIGN_CENTRE:
4db6714b 8408 if ( textOrientation == wxHORIZONTAL )
2f024384 8409 x = rect.x + ((rect.width - lineWidth) / 2);
038a5591 8410 else
2f024384 8411 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
038a5591 8412 break;
ef5df12b 8413
038a5591
JS
8414 case wxALIGN_LEFT:
8415 default:
4db6714b 8416 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
8417 x = rect.x + 1;
8418 else
8419 y = rect.y + rect.height - 1;
8420 break;
4330c974 8421 }
ef5df12b 8422
4330c974
VZ
8423 if ( textOrientation == wxHORIZONTAL )
8424 {
8425 dc.DrawText( line, x, y );
8426 y += lineHeight;
8427 }
8428 else
8429 {
8430 dc.DrawRotatedText( line, x, y, 90.0 );
8431 x += lineHeight;
038a5591 8432 }
f85afd4e 8433 }
f85afd4e
MB
8434}
8435
2f024384
DS
8436// Split multi-line text up into an array of strings.
8437// Any existing contents of the string array are preserved.
f85afd4e 8438//
696f3e9d 8439// TODO: refactor wxTextFile::Read() and reuse the same code from here
ef316e23 8440void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const
f85afd4e 8441{
f85afd4e
MB
8442 int startPos = 0;
8443 int pos;
6d004f67 8444 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 8445 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 8446
faa94f3e 8447 while ( startPos < (int)tVal.length() )
f85afd4e 8448 {
2433bb2e 8449 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
8450 if ( pos < 0 )
8451 {
8452 break;
8453 }
8454 else if ( pos == 0 )
8455 {
8456 lines.Add( wxEmptyString );
8457 }
8458 else
8459 {
696f3e9d 8460 lines.Add( tVal.Mid(startPos, pos) );
f85afd4e 8461 }
a9339fe2 8462
4db6714b 8463 startPos += pos + 1;
f85afd4e 8464 }
4db6714b 8465
2eafd712 8466 if ( startPos < (int)tVal.length() )
f85afd4e 8467 {
696f3e9d 8468 lines.Add( tVal.Mid( startPos ) );
f85afd4e
MB
8469 }
8470}
8471
fbfb8bcc 8472void wxGrid::GetTextBoxSize( const wxDC& dc,
d10f4bf9 8473 const wxArrayString& lines,
ef316e23 8474 long *width, long *height ) const
f85afd4e 8475{
ad2633bd
RR
8476 wxCoord w = 0;
8477 wxCoord h = 0;
8478 wxCoord lineW = 0, lineH = 0;
f85afd4e 8479
b540eb2b 8480 size_t i;
56b6cf26 8481 for ( i = 0; i < lines.GetCount(); i++ )
f85afd4e
MB
8482 {
8483 dc.GetTextExtent( lines[i], &lineW, &lineH );
8484 w = wxMax( w, lineW );
8485 h += lineH;
8486 }
8487
8488 *width = w;
8489 *height = h;
8490}
8491
f6bcfd97
BP
8492//
8493// ------ Batch processing.
8494//
8495void wxGrid::EndBatch()
8496{
8497 if ( m_batchCount > 0 )
8498 {
8499 m_batchCount--;
8500 if ( !m_batchCount )
8501 {
8502 CalcDimensions();
8503 m_rowLabelWin->Refresh();
ad805b9e 8504 m_colWindow->Refresh();
f6bcfd97
BP
8505 m_cornerLabelWin->Refresh();
8506 m_gridWin->Refresh();
8507 }
8508 }
8509}
f85afd4e 8510
d8232393
MB
8511// Use this, rather than wxWindow::Refresh(), to force an immediate
8512// repainting of the grid. Has no effect if you are already inside a
8513// BeginBatch / EndBatch block.
8514//
8515void wxGrid::ForceRefresh()
8516{
8517 BeginBatch();
8518 EndBatch();
8519}
8520
bf646121
VZ
8521bool wxGrid::Enable(bool enable)
8522{
8523 if ( !wxScrolledWindow::Enable(enable) )
8524 return false;
8525
8526 // redraw in the new state
8527 m_gridWin->Refresh();
8528
8529 return true;
8530}
d8232393 8531
f85afd4e 8532//
2d66e025 8533// ------ Edit control functions
f85afd4e
MB
8534//
8535
2d66e025 8536void wxGrid::EnableEditing( bool edit )
f85afd4e 8537{
2d66e025 8538 if ( edit != m_editable )
f85afd4e 8539 {
4db6714b
KH
8540 if (!edit)
8541 EnableCellEditControl(edit);
2d66e025 8542 m_editable = edit;
f85afd4e 8543 }
f85afd4e
MB
8544}
8545
2d66e025 8546void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 8547{
2c9a89e0
RD
8548 if (! m_editable)
8549 return;
8550
2c9a89e0
RD
8551 if ( enable != m_cellEditCtrlEnabled )
8552 {
dcfe4c3d 8553 if ( enable )
f85afd4e 8554 {
8a3e536c 8555 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 )
97a9929e 8556 return;
fe7b9ed6 8557
97a9929e 8558 // this should be checked by the caller!
a9339fe2 8559 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
b54ba671
VZ
8560
8561 // do it before ShowCellEditControl()
dcfe4c3d 8562 m_cellEditCtrlEnabled = enable;
b54ba671 8563
2d66e025 8564 ShowCellEditControl();
2d66e025
MB
8565 }
8566 else
8567 {
97a9929e 8568 //FIXME:add veto support
8a3e536c 8569 SendEvent(wxEVT_GRID_EDITOR_HIDDEN);
fe7b9ed6 8570
97a9929e 8571 HideCellEditControl();
2d66e025 8572 SaveEditControlValue();
b54ba671
VZ
8573
8574 // do it after HideCellEditControl()
dcfe4c3d 8575 m_cellEditCtrlEnabled = enable;
f85afd4e 8576 }
f85afd4e 8577 }
f85afd4e 8578}
f85afd4e 8579
b54ba671 8580bool wxGrid::IsCurrentCellReadOnly() const
283b7808 8581{
b54ba671
VZ
8582 // const_cast
8583 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
8584 bool readonly = attr->IsReadOnly();
8585 attr->DecRef();
283b7808 8586
b54ba671
VZ
8587 return readonly;
8588}
283b7808 8589
b54ba671
VZ
8590bool wxGrid::CanEnableCellControl() const
8591{
20c84410
SN
8592 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
8593 !IsCurrentCellReadOnly();
b54ba671
VZ
8594}
8595
8596bool wxGrid::IsCellEditControlEnabled() const
8597{
8598 // the cell edit control might be disable for all cells or just for the
8599 // current one if it's read only
ca65c044 8600 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
283b7808
VZ
8601}
8602
f6bcfd97
BP
8603bool wxGrid::IsCellEditControlShown() const
8604{
ca65c044 8605 bool isShown = false;
f6bcfd97
BP
8606
8607 if ( m_cellEditCtrlEnabled )
8608 {
8609 int row = m_currentCellCoords.GetRow();
8610 int col = m_currentCellCoords.GetCol();
8611 wxGridCellAttr* attr = GetCellAttr(row, col);
8612 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
8613 attr->DecRef();
8614
8615 if ( editor )
8616 {
8617 if ( editor->IsCreated() )
8618 {
8619 isShown = editor->GetControl()->IsShown();
8620 }
8621
8622 editor->DecRef();
8623 }
8624 }
8625
8626 return isShown;
8627}
8628
2d66e025 8629void wxGrid::ShowCellEditControl()
f85afd4e 8630{
2d66e025 8631 if ( IsCellEditControlEnabled() )
f85afd4e 8632 {
7db713ae 8633 if ( !IsVisible( m_currentCellCoords, false ) )
2d66e025 8634 {
ca65c044 8635 m_cellEditCtrlEnabled = false;
2d66e025
MB
8636 return;
8637 }
8638 else
8639 {
2c9a89e0
RD
8640 wxRect rect = CellToRect( m_currentCellCoords );
8641 int row = m_currentCellCoords.GetRow();
8642 int col = m_currentCellCoords.GetCol();
2d66e025 8643
27f35b66
SN
8644 // if this is part of a multicell, find owner (topleft)
8645 int cell_rows, cell_cols;
8646 GetCellSize( row, col, &cell_rows, &cell_cols );
8647 if ( cell_rows <= 0 || cell_cols <= 0 )
8648 {
8649 row += cell_rows;
8650 col += cell_cols;
8651 m_currentCellCoords.SetRow( row );
8652 m_currentCellCoords.SetCol( col );
8653 }
8654
99306db2
VZ
8655 // erase the highlight and the cell contents because the editor
8656 // might not cover the entire cell
8657 wxClientDC dc( m_gridWin );
8658 PrepareDC( dc );
2c03d771 8659 wxGridCellAttr* attr = GetCellAttr(row, col);
ff72f628 8660 dc.SetBrush(wxBrush(attr->GetBackgroundColour()));
99306db2
VZ
8661 dc.SetPen(*wxTRANSPARENT_PEN);
8662 dc.DrawRectangle(rect);
d2fdd8d2 8663
6f706ee0
VZ
8664 // convert to scrolled coords
8665 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8666
8667 int nXMove = 0;
8668 if (rect.x < 0)
8669 nXMove = rect.x;
8670
99306db2 8671 // cell is shifted by one pixel
68c5a31c 8672 // However, don't allow x or y to become negative
70e8d961 8673 // since the SetSize() method interprets that as
68c5a31c
SN
8674 // "don't change."
8675 if (rect.x > 0)
8676 rect.x--;
8677 if (rect.y > 0)
8678 rect.y--;
f0102d2a 8679
28a77bc4 8680 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
8681 if ( !editor->IsCreated() )
8682 {
ca65c044 8683 editor->Create(m_gridWin, wxID_ANY,
2c9a89e0 8684 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
8685
8686 wxGridEditorCreatedEvent evt(GetId(),
8687 wxEVT_GRID_EDITOR_CREATED,
8688 this,
8689 row,
8690 col,
8691 editor->GetControl());
8692 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
8693 }
8694
27f35b66 8695 // resize editor to overflow into righthand cells if allowed
39b80349 8696 int maxWidth = rect.width;
27f35b66
SN
8697 wxString value = GetCellValue(row, col);
8698 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
8699 {
39b80349 8700 int y;
2f024384
DS
8701 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
8702 if (maxWidth < rect.width)
8703 maxWidth = rect.width;
39b80349 8704 }
4db6714b 8705
39b80349 8706 int client_right = m_gridWin->GetClientSize().GetWidth();
2f024384 8707 if (rect.x + maxWidth > client_right)
2b5f62a0 8708 maxWidth = client_right - rect.x;
39b80349 8709
2b5f62a0
VZ
8710 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
8711 {
39b80349 8712 GetCellSize( row, col, &cell_rows, &cell_cols );
2b5f62a0 8713 // may have changed earlier
2f024384 8714 for (int i = col + cell_cols; i < m_numCols; i++)
2b5f62a0 8715 {
39b80349
SN
8716 int c_rows, c_cols;
8717 GetCellSize( row, i, &c_rows, &c_cols );
2f024384 8718
2b5f62a0 8719 // looks weird going over a multicell
bee19958 8720 if (m_table->IsEmptyCell( row, i ) &&
2b5f62a0 8721 (rect.width < maxWidth) && (c_rows == 1))
2f024384 8722 {
bee19958 8723 rect.width += GetColWidth( i );
2f024384 8724 }
2b5f62a0
VZ
8725 else
8726 break;
8727 }
4db6714b 8728
39b80349 8729 if (rect.GetRight() > client_right)
bee19958 8730 rect.SetRight( client_right - 1 );
27f35b66 8731 }
76c66f19 8732
bee19958 8733 editor->SetCellAttr( attr );
2c9a89e0 8734 editor->SetSize( rect );
e1a66d9a
RD
8735 if (nXMove != 0)
8736 editor->GetControl()->Move(
8737 editor->GetControl()->GetPosition().x + nXMove,
8738 editor->GetControl()->GetPosition().y );
ca65c044 8739 editor->Show( true, attr );
04418332
VZ
8740
8741 // recalc dimensions in case we need to
8742 // expand the scrolled window to account for editor
73145b0e 8743 CalcDimensions();
99306db2 8744
3da93aae 8745 editor->BeginEdit(row, col, this);
1bd71df9 8746 editor->SetCellAttr(NULL);
0b190b0f
VZ
8747
8748 editor->DecRef();
2c9a89e0 8749 attr->DecRef();
2b5f62a0 8750 }
f85afd4e
MB
8751 }
8752}
8753
2d66e025 8754void wxGrid::HideCellEditControl()
f85afd4e 8755{
2d66e025 8756 if ( IsCellEditControlEnabled() )
f85afd4e 8757 {
2c9a89e0
RD
8758 int row = m_currentCellCoords.GetRow();
8759 int col = m_currentCellCoords.GetCol();
8760
bee19958 8761 wxGridCellAttr *attr = GetCellAttr(row, col);
0b190b0f 8762 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7d277ac0 8763 const bool editorHadFocus = editor->GetControl()->HasFocus();
ca65c044 8764 editor->Show( false );
0b190b0f 8765 editor->DecRef();
2c9a89e0 8766 attr->DecRef();
2fb3e528 8767
7d277ac0
VZ
8768 // return the focus to the grid itself if the editor had it
8769 //
8770 // note that we must not do this unconditionally to avoid stealing
8771 // focus from the window which just received it if we are hiding the
8772 // editor precisely because we lost focus
8773 if ( editorHadFocus )
8774 m_gridWin->SetFocus();
2fb3e528 8775
27f35b66
SN
8776 // refresh whole row to the right
8777 wxRect rect( CellToRect(row, col) );
8778 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
8779 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
2f024384 8780
7d75e6c6
RD
8781#ifdef __WXMAC__
8782 // ensure that the pixels under the focus ring get refreshed as well
4db6714b 8783 rect.Inflate(10, 10);
7d75e6c6 8784#endif
2f024384 8785
ca65c044 8786 m_gridWin->Refresh( false, &rect );
f85afd4e 8787 }
2d66e025 8788}
8f177c8e 8789
2d66e025
MB
8790void wxGrid::SaveEditControlValue()
8791{
3da93aae
VZ
8792 if ( IsCellEditControlEnabled() )
8793 {
2c9a89e0
RD
8794 int row = m_currentCellCoords.GetRow();
8795 int col = m_currentCellCoords.GetCol();
8f177c8e 8796
4db6714b 8797 wxString oldval = GetCellValue(row, col);
fe7b9ed6 8798
3da93aae 8799 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8800 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 8801 bool changed = editor->EndEdit(row, col, this);
2d66e025 8802
0b190b0f 8803 editor->DecRef();
2c9a89e0 8804 attr->DecRef();
2d66e025 8805
3da93aae
VZ
8806 if (changed)
8807 {
8a3e536c 8808 if ( SendEvent(wxEVT_GRID_CELL_CHANGE) == -1 )
4db6714b 8809 {
97a9929e 8810 // Event has been vetoed, set the data back.
4db6714b 8811 SetCellValue(row, col, oldval);
97a9929e 8812 }
2d66e025 8813 }
f85afd4e
MB
8814 }
8815}
8816
2d66e025 8817//
60ff3b99
VZ
8818// ------ Grid location functions
8819// Note that all of these functions work with the logical coordinates of
2d66e025
MB
8820// grid cells and labels so you will need to convert from device
8821// coordinates for mouse events etc.
8822//
8823
8a3e536c 8824wxGridCellCoords wxGrid::XYToCell(int x, int y) const
f85afd4e 8825{
58dd5b3b
MB
8826 int row = YToRow(y);
8827 int col = XToCol(x);
8828
8a3e536c
VZ
8829 return row == -1 || col == -1 ? wxGridNoCellCoords
8830 : wxGridCellCoords(row, col);
2d66e025 8831}
f85afd4e 8832
bec70262
VZ
8833// compute row or column from some (unscrolled) coordinate value, using either
8834// m_defaultRowHeight/m_defaultColWidth or binary search on array of
8835// m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8836// for large grids)
cd4f6f5f
VZ
8837int wxGrid::PosToLinePos(int coord,
8838 bool clipToMinMax,
8839 const wxGridOperations& oper) const
2d66e025 8840{
bec70262 8841 const int numLines = oper.GetNumberOfLines(this);
a967f048 8842
bec70262 8843 if ( coord < 0 )
cd4f6f5f 8844 return clipToMinMax && numLines > 0 ? 0 : wxNOT_FOUND;
a967f048 8845
bec70262
VZ
8846 const int defaultLineSize = oper.GetDefaultLineSize(this);
8847 wxCHECK_MSG( defaultLineSize, -1, "can't have 0 default line size" );
d57ad377 8848
bec70262
VZ
8849 int maxPos = coord / defaultLineSize,
8850 minPos = 0;
8f177c8e 8851
bec70262
VZ
8852 // check for the simplest case: if we have no explicit line sizes
8853 // configured, then we already know the line this position falls in
8854 const wxArrayInt& lineEnds = oper.GetLineEnds(this);
8855 if ( lineEnds.empty() )
f85afd4e 8856 {
bec70262
VZ
8857 if ( maxPos < numLines )
8858 return maxPos;
4db6714b 8859
bec70262 8860 return clipToMinMax ? numLines - 1 : -1;
f85afd4e 8861 }
4db6714b 8862
2d66e025 8863
bec70262
VZ
8864 // adjust maxPos before starting the binary search
8865 if ( maxPos >= numLines )
b1da8107 8866 {
bec70262 8867 maxPos = numLines - 1;
b1da8107 8868 }
d4175745
VZ
8869 else
8870 {
bec70262 8871 if ( coord >= lineEnds[oper.GetLineAt(this, maxPos)])
d4175745
VZ
8872 {
8873 minPos = maxPos;
bec70262
VZ
8874 const int minDist = oper.GetMinimalAcceptableLineSize(this);
8875 if ( minDist )
8876 maxPos = coord / minDist;
d4175745 8877 else
bec70262 8878 maxPos = numLines - 1;
d4175745 8879 }
bec70262
VZ
8880
8881 if ( maxPos >= numLines )
8882 maxPos = numLines - 1;
d4175745
VZ
8883 }
8884
bec70262
VZ
8885 // check if the position is beyond the last column
8886 const int lineAtMaxPos = oper.GetLineAt(this, maxPos);
8887 if ( coord >= lineEnds[lineAtMaxPos] )
cd4f6f5f 8888 return clipToMinMax ? maxPos : -1;
d4175745 8889
bec70262
VZ
8890 // or before the first one
8891 const int lineAt0 = oper.GetLineAt(this, 0);
8892 if ( coord < lineEnds[lineAt0] )
cd4f6f5f 8893 return 0;
d4175745 8894
bec70262
VZ
8895
8896 // finally do perform the binary search
8897 while ( minPos < maxPos )
d4175745 8898 {
bec70262
VZ
8899 wxCHECK_MSG( lineEnds[oper.GetLineAt(this, minPos)] <= coord &&
8900 coord < lineEnds[oper.GetLineAt(this, maxPos)],
8901 -1,
cd4f6f5f 8902 "wxGrid: internal error in PosToLinePos()" );
d4175745 8903
bec70262 8904 if ( coord >= lineEnds[oper.GetLineAt(this, maxPos - 1)] )
cd4f6f5f 8905 return maxPos;
d4175745
VZ
8906 else
8907 maxPos--;
bec70262
VZ
8908
8909 const int median = minPos + (maxPos - minPos + 1) / 2;
8910 if ( coord < lineEnds[oper.GetLineAt(this, median)] )
d4175745
VZ
8911 maxPos = median;
8912 else
8913 minPos = median;
8914 }
bec70262 8915
cd4f6f5f
VZ
8916 return maxPos;
8917}
8918
8919int
8920wxGrid::PosToLine(int coord,
8921 bool clipToMinMax,
8922 const wxGridOperations& oper) const
8923{
8924 int pos = PosToLinePos(coord, clipToMinMax, oper);
8925
8926 return pos == wxNOT_FOUND ? wxNOT_FOUND : oper.GetLineAt(this, pos);
bec70262
VZ
8927}
8928
8929int wxGrid::YToRow(int y, bool clipToMinMax) const
8930{
8931 return PosToLine(y, clipToMinMax, wxGridRowOperations());
8932}
8933
8934int wxGrid::XToCol(int x, bool clipToMinMax) const
8935{
8936 return PosToLine(x, clipToMinMax, wxGridColumnOperations());
2d66e025 8937}
8f177c8e 8938
cd4f6f5f
VZ
8939int wxGrid::XToPos(int x) const
8940{
8941 return PosToLinePos(x, true /* clip */, wxGridColumnOperations());
8942}
8943
10a4531d
VZ
8944// return the row number that that the y coord is near the edge of, or -1 if
8945// not near an edge.
8946//
be2e4015
SN
8947// coords can only possibly be near an edge if
8948// (a) the row/column is large enough to still allow for an "inner" area
10a4531d 8949// that is _not_ near the edge (i.e., if the height/width is smaller
be2e4015
SN
8950// than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8951// near the edge).
8952// and
8953// (b) resizing rows/columns (the thing for which edge detection is
8954// relevant at all) is enabled.
2d66e025 8955//
10a4531d 8956int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const
2d66e025 8957{
10a4531d
VZ
8958 if ( !oper.CanResizeLines(this) )
8959 return -1;
33188aa4 8960
10a4531d
VZ
8961 const int line = oper.PosToLine(this, pos, true);
8962
8963 if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 8964 {
10a4531d
VZ
8965 // We know that we are in this line, test whether we are close enough
8966 // to start or end border, respectively.
8967 if ( abs(oper.GetLineEndPos(this, line) - pos) < WXGRID_LABEL_EDGE_ZONE )
8968 return line;
8969 else if ( line > 0 &&
8970 pos - oper.GetLineStartPos(this,
8971 line) < WXGRID_LABEL_EDGE_ZONE )
8972 return line - 1;
f85afd4e 8973 }
2d66e025
MB
8974
8975 return -1;
8976}
8977
10a4531d 8978int wxGrid::YToEdgeOfRow(int y) const
2d66e025 8979{
10a4531d
VZ
8980 return PosToEdgeOfLine(y, wxGridRowOperations());
8981}
2d66e025 8982
10a4531d
VZ
8983int wxGrid::XToEdgeOfCol(int x) const
8984{
8985 return PosToEdgeOfLine(x, wxGridColumnOperations());
f85afd4e
MB
8986}
8987
ef316e23 8988wxRect wxGrid::CellToRect( int row, int col ) const
f85afd4e 8989{
2d66e025 8990 wxRect rect( -1, -1, -1, -1 );
f85afd4e 8991
2f024384
DS
8992 if ( row >= 0 && row < m_numRows &&
8993 col >= 0 && col < m_numCols )
f85afd4e 8994 {
27f35b66
SN
8995 int i, cell_rows, cell_cols;
8996 rect.width = rect.height = 0;
8997 GetCellSize( row, col, &cell_rows, &cell_cols );
8998 // if negative then find multicell owner
2f024384
DS
8999 if (cell_rows < 0)
9000 row += cell_rows;
9001 if (cell_cols < 0)
9002 col += cell_cols;
27f35b66
SN
9003 GetCellSize( row, col, &cell_rows, &cell_cols );
9004
7c1cb261
VZ
9005 rect.x = GetColLeft(col);
9006 rect.y = GetRowTop(row);
2f024384
DS
9007 for (i=col; i < col + cell_cols; i++)
9008 rect.width += GetColWidth(i);
9009 for (i=row; i < row + cell_rows; i++)
27f35b66 9010 rect.height += GetRowHeight(i);
f85afd4e
MB
9011 }
9012
f6bcfd97 9013 // if grid lines are enabled, then the area of the cell is a bit smaller
4db6714b
KH
9014 if (m_gridLinesEnabled)
9015 {
f6bcfd97
BP
9016 rect.width -= 1;
9017 rect.height -= 1;
9018 }
4db6714b 9019
2d66e025
MB
9020 return rect;
9021}
9022
ef316e23 9023bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const
2d66e025
MB
9024{
9025 // get the cell rectangle in logical coords
9026 //
9027 wxRect r( CellToRect( row, col ) );
60ff3b99 9028
2d66e025
MB
9029 // convert to device coords
9030 //
9031 int left, top, right, bottom;
9032 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
9033 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 9034
2d66e025 9035 // check against the client area of the grid window
2d66e025
MB
9036 int cw, ch;
9037 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 9038
2d66e025 9039 if ( wholeCellVisible )
f85afd4e 9040 {
2d66e025 9041 // is the cell wholly visible ?
2f024384
DS
9042 return ( left >= 0 && right <= cw &&
9043 top >= 0 && bottom <= ch );
2d66e025
MB
9044 }
9045 else
9046 {
9047 // is the cell partly visible ?
9048 //
2f024384
DS
9049 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
9050 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
2d66e025
MB
9051 }
9052}
9053
2d66e025
MB
9054// make the specified cell location visible by doing a minimal amount
9055// of scrolling
9056//
9057void wxGrid::MakeCellVisible( int row, int col )
9058{
9059 int i;
60ff3b99 9060 int xpos = -1, ypos = -1;
2d66e025 9061
2f024384
DS
9062 if ( row >= 0 && row < m_numRows &&
9063 col >= 0 && col < m_numCols )
2d66e025
MB
9064 {
9065 // get the cell rectangle in logical coords
2d66e025 9066 wxRect r( CellToRect( row, col ) );
60ff3b99 9067
2d66e025 9068 // convert to device coords
2d66e025
MB
9069 int left, top, right, bottom;
9070 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
9071 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 9072
2d66e025
MB
9073 int cw, ch;
9074 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 9075
2d66e025 9076 if ( top < 0 )
3f296516 9077 {
2d66e025 9078 ypos = r.GetTop();
3f296516 9079 }
2d66e025
MB
9080 else if ( bottom > ch )
9081 {
9082 int h = r.GetHeight();
9083 ypos = r.GetTop();
56b6cf26 9084 for ( i = row - 1; i >= 0; i-- )
2d66e025 9085 {
7c1cb261
VZ
9086 int rowHeight = GetRowHeight(i);
9087 if ( h + rowHeight > ch )
9088 break;
2d66e025 9089
7c1cb261
VZ
9090 h += rowHeight;
9091 ypos -= rowHeight;
2d66e025 9092 }
f0102d2a
VZ
9093
9094 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
56b6cf26
DS
9095 // have rounding errors (this is important, because if we do,
9096 // we might not scroll at all and some cells won't be redrawn)
275c4ae4 9097 //
56b6cf26
DS
9098 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
9099 // so just add a full scroll unit...
675a9c0d 9100 ypos += m_scrollLineY;
2d66e025
MB
9101 }
9102
7db713ae 9103 // special handling for wide cells - show always left part of the cell!
faa94f3e 9104 // Otherwise, e.g. when stepping from row to row, it would jump between
7db713ae
JS
9105 // left and right part of the cell on every step!
9106// if ( left < 0 )
ccdee36f 9107 if ( left < 0 || (right - left) >= cw )
2d66e025
MB
9108 {
9109 xpos = r.GetLeft();
9110 }
9111 else if ( right > cw )
9112 {
73145b0e
JS
9113 // position the view so that the cell is on the right
9114 int x0, y0;
9115 CalcUnscrolledPosition(0, 0, &x0, &y0);
9116 xpos = x0 + (right - cw);
f0102d2a
VZ
9117
9118 // see comment for ypos above
675a9c0d 9119 xpos += m_scrollLineX;
2d66e025
MB
9120 }
9121
ccdee36f 9122 if ( xpos != -1 || ypos != -1 )
2d66e025 9123 {
97a9929e 9124 if ( xpos != -1 )
675a9c0d 9125 xpos /= m_scrollLineX;
97a9929e 9126 if ( ypos != -1 )
675a9c0d 9127 ypos /= m_scrollLineY;
2d66e025
MB
9128 Scroll( xpos, ypos );
9129 AdjustScrollbars();
9130 }
9131 }
9132}
9133
2d66e025
MB
9134//
9135// ------ Grid cursor movement functions
9136//
9137
10a4531d
VZ
9138bool
9139wxGrid::DoMoveCursor(bool expandSelection,
9140 const wxGridDirectionOperations& diroper)
2d66e025 9141{
10a4531d
VZ
9142 if ( m_currentCellCoords == wxGridNoCellCoords )
9143 return false;
9144
9145 if ( expandSelection )
2d66e025 9146 {
8a3e536c
VZ
9147 wxGridCellCoords coords = m_selectedBlockCorner;
9148 if ( coords == wxGridNoCellCoords )
9149 coords = m_currentCellCoords;
4db6714b 9150
8a3e536c 9151 if ( diroper.IsAtBoundary(coords) )
10a4531d 9152 return false;
2d66e025 9153
8a3e536c 9154 diroper.Advance(coords);
2d66e025 9155
8a3e536c 9156 UpdateBlockBeingSelected(m_currentCellCoords, coords);
10a4531d 9157 }
8a3e536c 9158 else // don't expand selection
f85afd4e 9159 {
8a3e536c
VZ
9160 ClearSelection();
9161
10a4531d 9162 if ( diroper.IsAtBoundary(m_currentCellCoords) )
ca65c044 9163 return false;
4db6714b 9164
10a4531d
VZ
9165 wxGridCellCoords coords = m_currentCellCoords;
9166 diroper.Advance(coords);
8a3e536c
VZ
9167
9168 GoToCell(coords);
f85afd4e 9169 }
2d66e025 9170
10a4531d 9171 return true;
f85afd4e
MB
9172}
9173
10a4531d 9174bool wxGrid::MoveCursorUp(bool expandSelection)
f85afd4e 9175{
10a4531d
VZ
9176 return DoMoveCursor(expandSelection,
9177 wxGridBackwardOperations(this, wxGridRowOperations()));
2d66e025
MB
9178}
9179
10a4531d 9180bool wxGrid::MoveCursorDown(bool expandSelection)
2d66e025 9181{
10a4531d
VZ
9182 return DoMoveCursor(expandSelection,
9183 wxGridForwardOperations(this, wxGridRowOperations()));
9184}
4db6714b 9185
10a4531d
VZ
9186bool wxGrid::MoveCursorLeft(bool expandSelection)
9187{
9188 return DoMoveCursor(expandSelection,
9189 wxGridBackwardOperations(this, wxGridColumnOperations()));
9190}
8f177c8e 9191
10a4531d
VZ
9192bool wxGrid::MoveCursorRight(bool expandSelection)
9193{
9194 return DoMoveCursor(expandSelection,
9195 wxGridForwardOperations(this, wxGridColumnOperations()));
2d66e025
MB
9196}
9197
10a4531d 9198bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations& diroper)
2d66e025 9199{
4db6714b
KH
9200 if ( m_currentCellCoords == wxGridNoCellCoords )
9201 return false;
60ff3b99 9202
10a4531d
VZ
9203 if ( diroper.IsAtBoundary(m_currentCellCoords) )
9204 return false;
ef5df12b 9205
10a4531d
VZ
9206 const int oldRow = m_currentCellCoords.GetRow();
9207 int newRow = diroper.MoveByPixelDistance(oldRow, m_gridWin->GetClientSize().y);
9208 if ( newRow == oldRow )
9209 {
9210 wxGridCellCoords coords(m_currentCellCoords);
9211 diroper.Advance(coords);
9212 newRow = coords.GetRow();
9213 }
8f177c8e 9214
8a3e536c 9215 GoToCell(newRow, m_currentCellCoords.GetCol());
60ff3b99 9216
10a4531d
VZ
9217 return true;
9218}
2d66e025 9219
10a4531d
VZ
9220bool wxGrid::MovePageUp()
9221{
9222 return DoMoveCursorByPage(
9223 wxGridBackwardOperations(this, wxGridRowOperations()));
2d66e025
MB
9224}
9225
9226bool wxGrid::MovePageDown()
9227{
10a4531d 9228 return DoMoveCursorByPage(
d188378e 9229 wxGridForwardOperations(this, wxGridRowOperations()));
10a4531d 9230}
60ff3b99 9231
10a4531d
VZ
9232// helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9233// until we find a non-empty cell or reach the grid end
9234void
9235wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords,
9236 const wxGridDirectionOperations& diroper)
9237{
9238 while ( !diroper.IsAtBoundary(coords) )
f85afd4e 9239 {
10a4531d
VZ
9240 diroper.Advance(coords);
9241 if ( !m_table->IsEmpty(coords) )
9242 break;
f85afd4e
MB
9243 }
9244}
8f177c8e 9245
10a4531d
VZ
9246bool
9247wxGrid::DoMoveCursorByBlock(bool expandSelection,
9248 const wxGridDirectionOperations& diroper)
2d66e025 9249{
10a4531d
VZ
9250 if ( !m_table || m_currentCellCoords == wxGridNoCellCoords )
9251 return false;
f85afd4e 9252
10a4531d
VZ
9253 if ( diroper.IsAtBoundary(m_currentCellCoords) )
9254 return false;
4db6714b 9255
10a4531d
VZ
9256 wxGridCellCoords coords(m_currentCellCoords);
9257 if ( m_table->IsEmpty(coords) )
9258 {
9259 // we are in an empty cell: find the next block of non-empty cells
9260 AdvanceToNextNonEmpty(coords, diroper);
2d66e025 9261 }
10a4531d 9262 else // current cell is not empty
f85afd4e 9263 {
10a4531d
VZ
9264 diroper.Advance(coords);
9265 if ( m_table->IsEmpty(coords) )
2d66e025 9266 {
10a4531d
VZ
9267 // we started at the end of a block, find the next one
9268 AdvanceToNextNonEmpty(coords, diroper);
2d66e025 9269 }
10a4531d 9270 else // we're in a middle of a block
2d66e025 9271 {
10a4531d
VZ
9272 // go to the end of it, i.e. find the last cell before the next
9273 // empty one
9274 while ( !diroper.IsAtBoundary(coords) )
2d66e025 9275 {
10a4531d
VZ
9276 wxGridCellCoords coordsNext(coords);
9277 diroper.Advance(coordsNext);
9278 if ( m_table->IsEmpty(coordsNext) )
2d66e025 9279 break;
2d66e025 9280
10a4531d
VZ
9281 coords = coordsNext;
9282 }
aa5e1f75 9283 }
10a4531d 9284 }
2d66e025 9285
10a4531d
VZ
9286 if ( expandSelection )
9287 {
8a3e536c 9288 UpdateBlockBeingSelected(m_currentCellCoords, coords);
10a4531d
VZ
9289 }
9290 else
9291 {
9292 ClearSelection();
8a3e536c 9293 GoToCell(coords);
f85afd4e 9294 }
f85afd4e 9295
10a4531d 9296 return true;
2d66e025 9297}
f85afd4e 9298
10a4531d 9299bool wxGrid::MoveCursorUpBlock(bool expandSelection)
f85afd4e 9300{
10a4531d
VZ
9301 return DoMoveCursorByBlock(
9302 expandSelection,
9303 wxGridBackwardOperations(this, wxGridRowOperations())
9304 );
9305}
8f177c8e 9306
10a4531d
VZ
9307bool wxGrid::MoveCursorDownBlock( bool expandSelection )
9308{
9309 return DoMoveCursorByBlock(
9310 expandSelection,
9311 wxGridForwardOperations(this, wxGridRowOperations())
9312 );
9313}
2d66e025 9314
10a4531d
VZ
9315bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
9316{
9317 return DoMoveCursorByBlock(
9318 expandSelection,
9319 wxGridBackwardOperations(this, wxGridColumnOperations())
9320 );
f85afd4e
MB
9321}
9322
5c8fc7c1 9323bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 9324{
10a4531d
VZ
9325 return DoMoveCursorByBlock(
9326 expandSelection,
9327 wxGridForwardOperations(this, wxGridColumnOperations())
9328 );
f85afd4e
MB
9329}
9330
f85afd4e 9331//
2d66e025 9332// ------ Label values and formatting
f85afd4e
MB
9333//
9334
ef316e23 9335void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const
f85afd4e 9336{
2f024384
DS
9337 if ( horiz )
9338 *horiz = m_rowLabelHorizAlign;
9339 if ( vert )
9340 *vert = m_rowLabelVertAlign;
f85afd4e
MB
9341}
9342
ef316e23 9343void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const
f85afd4e 9344{
2f024384
DS
9345 if ( horiz )
9346 *horiz = m_colLabelHorizAlign;
9347 if ( vert )
9348 *vert = m_colLabelVertAlign;
f85afd4e
MB
9349}
9350
ef316e23 9351int wxGrid::GetColLabelTextOrientation() const
d43851f7
JS
9352{
9353 return m_colLabelTextOrientation;
9354}
9355
ef316e23 9356wxString wxGrid::GetRowLabelValue( int row ) const
f85afd4e
MB
9357{
9358 if ( m_table )
9359 {
9360 return m_table->GetRowLabelValue( row );
9361 }
9362 else
9363 {
9364 wxString s;
9365 s << row;
9366 return s;
9367 }
9368}
9369
ef316e23 9370wxString wxGrid::GetColLabelValue( int col ) const
f85afd4e
MB
9371{
9372 if ( m_table )
9373 {
9374 return m_table->GetColLabelValue( col );
9375 }
9376 else
9377 {
9378 wxString s;
9379 s << col;
9380 return s;
9381 }
9382}
9383
9384void wxGrid::SetRowLabelSize( int width )
9385{
733f486a
VZ
9386 wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE );
9387
9388 if ( width == wxGRID_AUTOSIZE )
9389 {
9390 width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW);
9391 }
9392
2e8cd977
MB
9393 if ( width != m_rowLabelWidth )
9394 {
2e8cd977
MB
9395 if ( width == 0 )
9396 {
ca65c044
WS
9397 m_rowLabelWin->Show( false );
9398 m_cornerLabelWin->Show( false );
2e8cd977 9399 }
7807d81c 9400 else if ( m_rowLabelWidth == 0 )
2e8cd977 9401 {
ca65c044 9402 m_rowLabelWin->Show( true );
2f024384
DS
9403 if ( m_colLabelHeight > 0 )
9404 m_cornerLabelWin->Show( true );
2e8cd977 9405 }
b99be8fb 9406
2e8cd977 9407 m_rowLabelWidth = width;
7807d81c 9408 CalcWindowSizes();
ca65c044 9409 wxScrolledWindow::Refresh( true );
2e8cd977 9410 }
f85afd4e
MB
9411}
9412
9413void wxGrid::SetColLabelSize( int height )
9414{
733f486a
VZ
9415 wxASSERT( height >=0 || height == wxGRID_AUTOSIZE );
9416
9417 if ( height == wxGRID_AUTOSIZE )
9418 {
9419 height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN);
9420 }
9421
2e8cd977
MB
9422 if ( height != m_colLabelHeight )
9423 {
2e8cd977
MB
9424 if ( height == 0 )
9425 {
ad805b9e 9426 m_colWindow->Show( false );
ca65c044 9427 m_cornerLabelWin->Show( false );
2e8cd977 9428 }
7807d81c 9429 else if ( m_colLabelHeight == 0 )
2e8cd977 9430 {
ad805b9e 9431 m_colWindow->Show( true );
a9339fe2
DS
9432 if ( m_rowLabelWidth > 0 )
9433 m_cornerLabelWin->Show( true );
2e8cd977 9434 }
7807d81c 9435
2e8cd977 9436 m_colLabelHeight = height;
7807d81c 9437 CalcWindowSizes();
ca65c044 9438 wxScrolledWindow::Refresh( true );
2e8cd977 9439 }
f85afd4e
MB
9440}
9441
9442void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
9443{
2d66e025
MB
9444 if ( m_labelBackgroundColour != colour )
9445 {
9446 m_labelBackgroundColour = colour;
9447 m_rowLabelWin->SetBackgroundColour( colour );
ad805b9e 9448 m_colWindow->SetBackgroundColour( colour );
2d66e025
MB
9449 m_cornerLabelWin->SetBackgroundColour( colour );
9450
9451 if ( !GetBatchCount() )
9452 {
9453 m_rowLabelWin->Refresh();
ad805b9e 9454 m_colWindow->Refresh();
2d66e025
MB
9455 m_cornerLabelWin->Refresh();
9456 }
9457 }
f85afd4e
MB
9458}
9459
9460void wxGrid::SetLabelTextColour( const wxColour& colour )
9461{
2d66e025
MB
9462 if ( m_labelTextColour != colour )
9463 {
9464 m_labelTextColour = colour;
9465 if ( !GetBatchCount() )
9466 {
9467 m_rowLabelWin->Refresh();
ad805b9e 9468 m_colWindow->Refresh();
2d66e025
MB
9469 }
9470 }
f85afd4e
MB
9471}
9472
9473void wxGrid::SetLabelFont( const wxFont& font )
9474{
9475 m_labelFont = font;
2d66e025
MB
9476 if ( !GetBatchCount() )
9477 {
9478 m_rowLabelWin->Refresh();
ad805b9e 9479 m_colWindow->Refresh();
2d66e025 9480 }
f85afd4e
MB
9481}
9482
9483void wxGrid::SetRowLabelAlignment( int horiz, int vert )
9484{
4c7277db
MB
9485 // allow old (incorrect) defs to be used
9486 switch ( horiz )
9487 {
9488 case wxLEFT: horiz = wxALIGN_LEFT; break;
9489 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9490 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9491 }
84912ef8 9492
4c7277db
MB
9493 switch ( vert )
9494 {
9495 case wxTOP: vert = wxALIGN_TOP; break;
9496 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9497 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9498 }
84912ef8 9499
4c7277db 9500 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
9501 {
9502 m_rowLabelHorizAlign = horiz;
9503 }
8f177c8e 9504
4c7277db 9505 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
9506 {
9507 m_rowLabelVertAlign = vert;
9508 }
9509
2d66e025
MB
9510 if ( !GetBatchCount() )
9511 {
9512 m_rowLabelWin->Refresh();
60ff3b99 9513 }
f85afd4e
MB
9514}
9515
9516void wxGrid::SetColLabelAlignment( int horiz, int vert )
9517{
4c7277db
MB
9518 // allow old (incorrect) defs to be used
9519 switch ( horiz )
9520 {
9521 case wxLEFT: horiz = wxALIGN_LEFT; break;
9522 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9523 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9524 }
84912ef8 9525
4c7277db
MB
9526 switch ( vert )
9527 {
9528 case wxTOP: vert = wxALIGN_TOP; break;
9529 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9530 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9531 }
84912ef8 9532
4c7277db 9533 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
9534 {
9535 m_colLabelHorizAlign = horiz;
9536 }
8f177c8e 9537
4c7277db 9538 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
9539 {
9540 m_colLabelVertAlign = vert;
9541 }
9542
2d66e025
MB
9543 if ( !GetBatchCount() )
9544 {
ad805b9e 9545 m_colWindow->Refresh();
60ff3b99 9546 }
f85afd4e
MB
9547}
9548
ca65c044
WS
9549// Note: under MSW, the default column label font must be changed because it
9550// does not support vertical printing
d43851f7
JS
9551//
9552// Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
ca65c044
WS
9553// pGrid->SetLabelFont(font);
9554// pGrid->SetColLabelTextOrientation(wxVERTICAL);
d43851f7
JS
9555//
9556void wxGrid::SetColLabelTextOrientation( int textOrientation )
9557{
4db6714b 9558 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
d43851f7 9559 m_colLabelTextOrientation = textOrientation;
d43851f7
JS
9560
9561 if ( !GetBatchCount() )
ad805b9e 9562 m_colWindow->Refresh();
d43851f7
JS
9563}
9564
f85afd4e
MB
9565void wxGrid::SetRowLabelValue( int row, const wxString& s )
9566{
9567 if ( m_table )
9568 {
9569 m_table->SetRowLabelValue( row, s );
2d66e025
MB
9570 if ( !GetBatchCount() )
9571 {
ccdee36f 9572 wxRect rect = CellToRect( row, 0 );
70c7a608
SN
9573 if ( rect.height > 0 )
9574 {
cb309039 9575 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 9576 rect.x = 0;
70c7a608 9577 rect.width = m_rowLabelWidth;
ca65c044 9578 m_rowLabelWin->Refresh( true, &rect );
70c7a608 9579 }
2d66e025 9580 }
f85afd4e
MB
9581 }
9582}
9583
9584void wxGrid::SetColLabelValue( int col, const wxString& s )
9585{
9586 if ( m_table )
9587 {
9588 m_table->SetColLabelValue( col, s );
2d66e025
MB
9589 if ( !GetBatchCount() )
9590 {
ad805b9e 9591 if ( m_useNativeHeader )
70c7a608 9592 {
3039ade9 9593 GetGridColHeader()->UpdateColumn(col);
ad805b9e
VZ
9594 }
9595 else
9596 {
9597 wxRect rect = CellToRect( 0, col );
9598 if ( rect.width > 0 )
9599 {
9600 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
9601 rect.y = 0;
9602 rect.height = m_colLabelHeight;
9603 GetColLabelWindow()->Refresh( true, &rect );
9604 }
70c7a608 9605 }
2d66e025 9606 }
f85afd4e
MB
9607 }
9608}
9609
9610void wxGrid::SetGridLineColour( const wxColour& colour )
9611{
2d66e025
MB
9612 if ( m_gridLineColour != colour )
9613 {
9614 m_gridLineColour = colour;
60ff3b99 9615
9f7aee01
VZ
9616 if ( GridLinesEnabled() )
9617 RedrawGridLines();
2d66e025 9618 }
f85afd4e
MB
9619}
9620
f6bcfd97
BP
9621void wxGrid::SetCellHighlightColour( const wxColour& colour )
9622{
9623 if ( m_cellHighlightColour != colour )
9624 {
9625 m_cellHighlightColour = colour;
9626
9627 wxClientDC dc( m_gridWin );
9628 PrepareDC( dc );
9629 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
9630 DrawCellHighlight(dc, attr);
9631 attr->DecRef();
9632 }
9633}
9634
d2520c85
RD
9635void wxGrid::SetCellHighlightPenWidth(int width)
9636{
4db6714b
KH
9637 if (m_cellHighlightPenWidth != width)
9638 {
d2520c85
RD
9639 m_cellHighlightPenWidth = width;
9640
9641 // Just redrawing the cell highlight is not enough since that won't
9642 // make any visible change if the the thickness is getting smaller.
9643 int row = m_currentCellCoords.GetRow();
9644 int col = m_currentCellCoords.GetCol();
1b3b96d8 9645 if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
d2520c85 9646 return;
2f024384 9647
d2520c85 9648 wxRect rect = CellToRect(row, col);
ca65c044 9649 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9650 }
9651}
9652
9653void wxGrid::SetCellHighlightROPenWidth(int width)
9654{
4db6714b
KH
9655 if (m_cellHighlightROPenWidth != width)
9656 {
d2520c85
RD
9657 m_cellHighlightROPenWidth = width;
9658
9659 // Just redrawing the cell highlight is not enough since that won't
9660 // make any visible change if the the thickness is getting smaller.
9661 int row = m_currentCellCoords.GetRow();
9662 int col = m_currentCellCoords.GetCol();
76c66f19
VZ
9663 if ( row == -1 || col == -1 ||
9664 GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
d2520c85 9665 return;
ccdee36f 9666
d2520c85 9667 wxRect rect = CellToRect(row, col);
ca65c044 9668 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9669 }
9670}
9671
9f7aee01
VZ
9672void wxGrid::RedrawGridLines()
9673{
9674 // the lines will be redrawn when the window is thawn
9675 if ( GetBatchCount() )
9676 return;
9677
9678 if ( GridLinesEnabled() )
9679 {
9680 wxClientDC dc( m_gridWin );
9681 PrepareDC( dc );
9682 DrawAllGridLines( dc, wxRegion() );
9683 }
9684 else // remove the grid lines
9685 {
9686 m_gridWin->Refresh();
9687 }
9688}
9689
f85afd4e
MB
9690void wxGrid::EnableGridLines( bool enable )
9691{
9692 if ( enable != m_gridLinesEnabled )
9693 {
9694 m_gridLinesEnabled = enable;
2d66e025 9695
9f7aee01
VZ
9696 RedrawGridLines();
9697 }
9698}
9699
9700void wxGrid::DoClipGridLines(bool& var, bool clip)
9701{
9702 if ( clip != var )
9703 {
9704 var = clip;
9705
9706 if ( GridLinesEnabled() )
9707 RedrawGridLines();
f85afd4e
MB
9708 }
9709}
9710
ef316e23 9711int wxGrid::GetDefaultRowSize() const
f85afd4e
MB
9712{
9713 return m_defaultRowHeight;
9714}
9715
ef316e23 9716int wxGrid::GetRowSize( int row ) const
f85afd4e 9717{
b99be8fb
VZ
9718 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
9719
7c1cb261 9720 return GetRowHeight(row);
f85afd4e
MB
9721}
9722
ef316e23 9723int wxGrid::GetDefaultColSize() const
f85afd4e
MB
9724{
9725 return m_defaultColWidth;
9726}
9727
ef316e23 9728int wxGrid::GetColSize( int col ) const
f85afd4e 9729{
b99be8fb
VZ
9730 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
9731
7c1cb261 9732 return GetColWidth(col);
f85afd4e
MB
9733}
9734
2e9a6788
VZ
9735// ============================================================================
9736// access to the grid attributes: each of them has a default value in the grid
9737// itself and may be overidden on a per-cell basis
9738// ============================================================================
9739
9740// ----------------------------------------------------------------------------
9741// setting default attributes
9742// ----------------------------------------------------------------------------
9743
9744void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9745{
2796cce3 9746 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
9747#ifdef __WXGTK__
9748 m_gridWin->SetBackgroundColour(col);
9749#endif
2e9a6788
VZ
9750}
9751
9752void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9753{
2796cce3 9754 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
9755}
9756
9757void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9758{
2796cce3 9759 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
9760}
9761
27f35b66
SN
9762void wxGrid::SetDefaultCellOverflow( bool allow )
9763{
9764 m_defaultCellAttr->SetOverflow(allow);
9765}
9766
2e9a6788
VZ
9767void wxGrid::SetDefaultCellFont( const wxFont& font )
9768{
2796cce3
RD
9769 m_defaultCellAttr->SetFont(font);
9770}
9771
ca63e8e9
RD
9772// For editors and renderers the type registry takes precedence over the
9773// default attr, so we need to register the new editor/renderer for the string
9774// data type in order to make setting a default editor/renderer appear to
9775// work correctly.
9776
0ba143c9
RD
9777void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9778{
ca63e8e9
RD
9779 RegisterDataType(wxGRID_VALUE_STRING,
9780 renderer,
9781 GetDefaultEditorForType(wxGRID_VALUE_STRING));
0ba143c9 9782}
2e9a6788 9783
0ba143c9
RD
9784void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9785{
ca63e8e9
RD
9786 RegisterDataType(wxGRID_VALUE_STRING,
9787 GetDefaultRendererForType(wxGRID_VALUE_STRING),
42841dfc 9788 editor);
0ba143c9 9789}
9b4aede2 9790
2e9a6788 9791// ----------------------------------------------------------------------------
ef316e23 9792// access to the default attributes
2e9a6788
VZ
9793// ----------------------------------------------------------------------------
9794
ef316e23 9795wxColour wxGrid::GetDefaultCellBackgroundColour() const
f85afd4e 9796{
2796cce3 9797 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
9798}
9799
ef316e23 9800wxColour wxGrid::GetDefaultCellTextColour() const
2e9a6788 9801{
2796cce3 9802 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
9803}
9804
ef316e23 9805wxFont wxGrid::GetDefaultCellFont() const
2e9a6788 9806{
2796cce3 9807 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
9808}
9809
ef316e23 9810void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
2e9a6788 9811{
2796cce3 9812 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
9813}
9814
ef316e23 9815bool wxGrid::GetDefaultCellOverflow() const
27f35b66
SN
9816{
9817 return m_defaultCellAttr->GetOverflow();
9818}
9819
0ba143c9
RD
9820wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9821{
0b190b0f 9822 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 9823}
ab79958a 9824
0ba143c9
RD
9825wxGridCellEditor *wxGrid::GetDefaultEditor() const
9826{
4db6714b 9827 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
0ba143c9 9828}
9b4aede2 9829
2e9a6788
VZ
9830// ----------------------------------------------------------------------------
9831// access to cell attributes
9832// ----------------------------------------------------------------------------
9833
ef316e23 9834wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
f85afd4e 9835{
0a976765 9836 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9837 wxColour colour = attr->GetBackgroundColour();
39bcce60 9838 attr->DecRef();
2f024384 9839
b99be8fb 9840 return colour;
f85afd4e
MB
9841}
9842
ef316e23 9843wxColour wxGrid::GetCellTextColour( int row, int col ) const
f85afd4e 9844{
0a976765 9845 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9846 wxColour colour = attr->GetTextColour();
39bcce60 9847 attr->DecRef();
2f024384 9848
b99be8fb 9849 return colour;
f85afd4e
MB
9850}
9851
ef316e23 9852wxFont wxGrid::GetCellFont( int row, int col ) const
f85afd4e 9853{
0a976765 9854 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9855 wxFont font = attr->GetFont();
39bcce60 9856 attr->DecRef();
2f024384 9857
b99be8fb 9858 return font;
f85afd4e
MB
9859}
9860
ef316e23 9861void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
f85afd4e 9862{
0a976765 9863 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9864 attr->GetAlignment(horiz, vert);
39bcce60 9865 attr->DecRef();
2e9a6788
VZ
9866}
9867
ef316e23 9868bool wxGrid::GetCellOverflow( int row, int col ) const
27f35b66
SN
9869{
9870 wxGridCellAttr *attr = GetCellAttr(row, col);
9871 bool allow = attr->GetOverflow();
9872 attr->DecRef();
4db6714b 9873
27f35b66
SN
9874 return allow;
9875}
9876
ef316e23 9877void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
27f35b66
SN
9878{
9879 wxGridCellAttr *attr = GetCellAttr(row, col);
9880 attr->GetSize( num_rows, num_cols );
9881 attr->DecRef();
9882}
9883
ef316e23 9884wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
2796cce3
RD
9885{
9886 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9887 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 9888 attr->DecRef();
0b190b0f 9889
2796cce3
RD
9890 return renderer;
9891}
9892
ef316e23 9893wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
9b4aede2
RD
9894{
9895 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9896 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 9897 attr->DecRef();
0b190b0f 9898
9b4aede2
RD
9899 return editor;
9900}
9901
283b7808
VZ
9902bool wxGrid::IsReadOnly(int row, int col) const
9903{
9904 wxGridCellAttr* attr = GetCellAttr(row, col);
9905 bool isReadOnly = attr->IsReadOnly();
9906 attr->DecRef();
4db6714b 9907
283b7808
VZ
9908 return isReadOnly;
9909}
9910
2e9a6788 9911// ----------------------------------------------------------------------------
758cbedf 9912// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
9913// ----------------------------------------------------------------------------
9914
ef316e23 9915bool wxGrid::CanHaveAttributes() const
2e9a6788
VZ
9916{
9917 if ( !m_table )
9918 {
ca65c044 9919 return false;
2e9a6788
VZ
9920 }
9921
f2d76237 9922 return m_table->CanHaveAttributes();
2e9a6788
VZ
9923}
9924
0a976765
VZ
9925void wxGrid::ClearAttrCache()
9926{
9927 if ( m_attrCache.row != -1 )
9928 {
54181a33 9929 wxGridCellAttr *oldAttr = m_attrCache.attr;
19d7140e 9930 m_attrCache.attr = NULL;
0a976765 9931 m_attrCache.row = -1;
1506cc66
SN
9932 // wxSafeDecRec(...) might cause event processing that accesses
9933 // the cached attribute, if one exists (e.g. by deleting the
9934 // editor stored within the attribute). Therefore it is important
ace8d849 9935 // to invalidate the cache before calling wxSafeDecRef!
1506cc66 9936 wxSafeDecRef(oldAttr);
0a976765
VZ
9937 }
9938}
9939
9940void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9941{
2b5f62a0
VZ
9942 if ( attr != NULL )
9943 {
9944 wxGrid *self = (wxGrid *)this; // const_cast
0a976765 9945
2b5f62a0
VZ
9946 self->ClearAttrCache();
9947 self->m_attrCache.row = row;
9948 self->m_attrCache.col = col;
9949 self->m_attrCache.attr = attr;
9950 wxSafeIncRef(attr);
9951 }
0a976765
VZ
9952}
9953
9954bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9955{
9956 if ( row == m_attrCache.row && col == m_attrCache.col )
9957 {
9958 *attr = m_attrCache.attr;
39bcce60 9959 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
9960
9961#ifdef DEBUG_ATTR_CACHE
9962 gs_nAttrCacheHits++;
9963#endif
9964
ca65c044 9965 return true;
0a976765
VZ
9966 }
9967 else
9968 {
9969#ifdef DEBUG_ATTR_CACHE
9970 gs_nAttrCacheMisses++;
9971#endif
4db6714b 9972
ca65c044 9973 return false;
0a976765
VZ
9974 }
9975}
9976
2e9a6788
VZ
9977wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9978{
a373d23b
SN
9979 wxGridCellAttr *attr = NULL;
9980 // Additional test to avoid looking at the cache e.g. for
9981 // wxNoCellCoords, as this will confuse memory management.
9982 if ( row >= 0 )
9983 {
3ed884a0
SN
9984 if ( !LookupAttr(row, col, &attr) )
9985 {
c2f5b920 9986 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
10a4531d 9987 : NULL;
3ed884a0
SN
9988 CacheAttr(row, col, attr);
9989 }
0a976765 9990 }
4db6714b 9991
508011ce
VZ
9992 if (attr)
9993 {
2796cce3 9994 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
9995 }
9996 else
9997 {
2796cce3
RD
9998 attr = m_defaultCellAttr;
9999 attr->IncRef();
10000 }
2e9a6788 10001
0a976765
VZ
10002 return attr;
10003}
10004
10005wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
10006{
10a4531d 10007 wxGridCellAttr *attr = NULL;
71e60f70 10008 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
0a976765 10009
71e60f70
RD
10010 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
10011 wxCHECK_MSG( m_table, attr, _T("must have a table") );
1df4050d
VZ
10012
10013 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
10014 if ( !attr )
10015 {
10016 attr = new wxGridCellAttr(m_defaultCellAttr);
10017
10018 // artificially inc the ref count to match DecRef() in caller
10019 attr->IncRef();
10020 m_table->SetAttr(attr, row, col);
10021 }
0a976765 10022
2e9a6788
VZ
10023 return attr;
10024}
10025
0b190b0f
VZ
10026// ----------------------------------------------------------------------------
10027// setting column attributes (wrappers around SetColAttr)
10028// ----------------------------------------------------------------------------
10029
10030void wxGrid::SetColFormatBool(int col)
10031{
10032 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
10033}
10034
10035void wxGrid::SetColFormatNumber(int col)
10036{
10037 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
10038}
10039
10040void wxGrid::SetColFormatFloat(int col, int width, int precision)
10041{
10042 wxString typeName = wxGRID_VALUE_FLOAT;
10043 if ( (width != -1) || (precision != -1) )
10044 {
10045 typeName << _T(':') << width << _T(',') << precision;
10046 }
10047
10048 SetColFormatCustom(col, typeName);
10049}
10050
10051void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
10052{
999836aa 10053 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
4db6714b 10054 if (!attr)
19d7140e 10055 attr = new wxGridCellAttr;
0b190b0f
VZ
10056 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
10057 attr->SetRenderer(renderer);
54181a33
VZ
10058 wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
10059 attr->SetEditor(editor);
0b190b0f
VZ
10060
10061 SetColAttr(col, attr);
19d7140e 10062
0b190b0f
VZ
10063}
10064
758cbedf
VZ
10065// ----------------------------------------------------------------------------
10066// setting cell attributes: this is forwarded to the table
10067// ----------------------------------------------------------------------------
10068
27f35b66
SN
10069void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
10070{
10071 if ( CanHaveAttributes() )
10072 {
10073 m_table->SetAttr(attr, row, col);
10074 ClearAttrCache();
10075 }
10076 else
10077 {
10078 wxSafeDecRef(attr);
10079 }
10080}
10081
758cbedf
VZ
10082void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
10083{
10084 if ( CanHaveAttributes() )
10085 {
10086 m_table->SetRowAttr(attr, row);
19d7140e 10087 ClearAttrCache();
758cbedf
VZ
10088 }
10089 else
10090 {
39bcce60 10091 wxSafeDecRef(attr);
758cbedf
VZ
10092 }
10093}
10094
10095void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
10096{
10097 if ( CanHaveAttributes() )
10098 {
10099 m_table->SetColAttr(attr, col);
19d7140e 10100 ClearAttrCache();
758cbedf
VZ
10101 }
10102 else
10103 {
39bcce60 10104 wxSafeDecRef(attr);
758cbedf
VZ
10105 }
10106}
10107
2e9a6788
VZ
10108void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
10109{
10110 if ( CanHaveAttributes() )
10111 {
0a976765 10112 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
10113 attr->SetBackgroundColour(colour);
10114 attr->DecRef();
10115 }
10116}
10117
10118void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
10119{
10120 if ( CanHaveAttributes() )
10121 {
0a976765 10122 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
10123 attr->SetTextColour(colour);
10124 attr->DecRef();
10125 }
10126}
10127
10128void wxGrid::SetCellFont( int row, int col, const wxFont& font )
10129{
10130 if ( CanHaveAttributes() )
10131 {
0a976765 10132 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
10133 attr->SetFont(font);
10134 attr->DecRef();
10135 }
10136}
10137
10138void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
10139{
10140 if ( CanHaveAttributes() )
10141 {
0a976765 10142 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
10143 attr->SetAlignment(horiz, vert);
10144 attr->DecRef();
ab79958a
VZ
10145 }
10146}
10147
27f35b66
SN
10148void wxGrid::SetCellOverflow( int row, int col, bool allow )
10149{
10150 if ( CanHaveAttributes() )
10151 {
10152 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10153 attr->SetOverflow(allow);
10154 attr->DecRef();
10155 }
10156}
10157
10158void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
10159{
10160 if ( CanHaveAttributes() )
10161 {
10162 int cell_rows, cell_cols;
10163
10164 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10165 attr->GetSize(&cell_rows, &cell_cols);
10166 attr->SetSize(num_rows, num_cols);
10167 attr->DecRef();
10168
10169 // Cannot set the size of a cell to 0 or negative values
10170 // While it is perfectly legal to do that, this function cannot
10171 // handle all the possibilies, do it by hand by getting the CellAttr.
10172 // You can only set the size of a cell to 1,1 or greater with this fn
10173 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
10174 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10175 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
10176 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10177
10178 // if this was already a multicell then "turn off" the other cells first
a6b2078d 10179 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66
SN
10180 {
10181 int i, j;
2f024384 10182 for (j=row; j < row + cell_rows; j++)
27f35b66 10183 {
2f024384 10184 for (i=col; i < col + cell_cols; i++)
27f35b66
SN
10185 {
10186 if ((i != col) || (j != row))
10187 {
10188 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10189 attr_stub->SetSize( 1, 1 );
10190 attr_stub->DecRef();
10191 }
10192 }
10193 }
10194 }
10195
10196 // mark the cells that will be covered by this cell to
10197 // negative or zero values to point back at this cell
10198 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
10199 {
10200 int i, j;
2f024384 10201 for (j=row; j < row + num_rows; j++)
27f35b66 10202 {
2f024384 10203 for (i=col; i < col + num_cols; i++)
27f35b66
SN
10204 {
10205 if ((i != col) || (j != row))
10206 {
10207 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
2f024384 10208 attr_stub->SetSize( row - j, col - i );
27f35b66
SN
10209 attr_stub->DecRef();
10210 }
10211 }
10212 }
10213 }
10214 }
10215}
10216
ab79958a
VZ
10217void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
10218{
10219 if ( CanHaveAttributes() )
10220 {
0a976765 10221 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
10222 attr->SetRenderer(renderer);
10223 attr->DecRef();
9b4aede2
RD
10224 }
10225}
10226
10227void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
10228{
10229 if ( CanHaveAttributes() )
10230 {
10231 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10232 attr->SetEditor(editor);
10233 attr->DecRef();
283b7808
VZ
10234 }
10235}
10236
10237void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
10238{
10239 if ( CanHaveAttributes() )
10240 {
10241 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10242 attr->SetReadOnly(isReadOnly);
10243 attr->DecRef();
2e9a6788 10244 }
f85afd4e
MB
10245}
10246
f2d76237
RD
10247// ----------------------------------------------------------------------------
10248// Data type registration
10249// ----------------------------------------------------------------------------
10250
10251void wxGrid::RegisterDataType(const wxString& typeName,
10252 wxGridCellRenderer* renderer,
10253 wxGridCellEditor* editor)
10254{
10255 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
10256}
10257
10258
a9339fe2 10259wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
10260{
10261 wxString typeName = m_table->GetTypeName(row, col);
10262 return GetDefaultEditorForType(typeName);
10263}
10264
a9339fe2 10265wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
10266{
10267 wxString typeName = m_table->GetTypeName(row, col);
10268 return GetDefaultRendererForType(typeName);
10269}
10270
a9339fe2 10271wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 10272{
c4608a8a 10273 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
10274 if ( index == wxNOT_FOUND )
10275 {
767e0835 10276 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
0b190b0f 10277
f2d76237
RD
10278 return NULL;
10279 }
0b190b0f 10280
f2d76237
RD
10281 return m_typeRegistry->GetEditor(index);
10282}
10283
a9339fe2 10284wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 10285{
c4608a8a 10286 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
10287 if ( index == wxNOT_FOUND )
10288 {
767e0835 10289 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
0b190b0f 10290
c4608a8a 10291 return NULL;
e72b4213 10292 }
0b190b0f 10293
c4608a8a 10294 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
10295}
10296
2e9a6788
VZ
10297// ----------------------------------------------------------------------------
10298// row/col size
10299// ----------------------------------------------------------------------------
10300
6e8524b1
MB
10301void wxGrid::EnableDragRowSize( bool enable )
10302{
10303 m_canDragRowSize = enable;
10304}
10305
6e8524b1
MB
10306void wxGrid::EnableDragColSize( bool enable )
10307{
10308 m_canDragColSize = enable;
10309}
10310
4cfa5de6
RD
10311void wxGrid::EnableDragGridSize( bool enable )
10312{
10313 m_canDragGridSize = enable;
10314}
10315
79dbea21
RD
10316void wxGrid::EnableDragCell( bool enable )
10317{
10318 m_canDragCell = enable;
10319}
6e8524b1 10320
f85afd4e
MB
10321void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
10322{
b8d24d4e 10323 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
f85afd4e
MB
10324
10325 if ( resizeExistingRows )
10326 {
b1da8107
SN
10327 // since we are resizing all rows to the default row size,
10328 // we can simply clear the row heights and row bottoms
10329 // arrays (which also allows us to take advantage of
10330 // some speed optimisations)
10331 m_rowHeights.Empty();
10332 m_rowBottoms.Empty();
edb89f7e
VZ
10333 if ( !GetBatchCount() )
10334 CalcDimensions();
f85afd4e
MB
10335 }
10336}
10337
10338void wxGrid::SetRowSize( int row, int height )
10339{
b99be8fb 10340 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 10341
ad7502d8
SN
10342 // if < 0 then calculate new height from label
10343 if ( height < 0 )
10344 {
10345 long w, h;
10346 wxArrayString lines;
10347 wxClientDC dc(m_rowLabelWin);
10348 dc.SetFont(GetLabelFont());
10349 StringToLines(GetRowLabelValue( row ), lines);
ace8d849
VZ
10350 GetTextBoxSize( dc, lines, &w, &h );
10351 //check that it is not less than the minimal height
10352 height = wxMax(h, GetRowMinimalAcceptableHeight());
ad7502d8
SN
10353 }
10354
b4bfd0fa 10355 // See comment in SetColSize
4db6714b
KH
10356 if ( height < GetRowMinimalAcceptableHeight())
10357 return;
b8d24d4e 10358
7c1cb261
VZ
10359 if ( m_rowHeights.IsEmpty() )
10360 {
10361 // need to really create the array
10362 InitRowHeights();
10363 }
60ff3b99 10364
b99be8fb
VZ
10365 int h = wxMax( 0, height );
10366 int diff = h - m_rowHeights[row];
60ff3b99 10367
b99be8fb 10368 m_rowHeights[row] = h;
0ed3b812 10369 for ( int i = row; i < m_numRows; i++ )
f85afd4e 10370 {
b99be8fb 10371 m_rowBottoms[i] += diff;
f85afd4e 10372 }
2f024384 10373
faec5a43
SN
10374 if ( !GetBatchCount() )
10375 CalcDimensions();
f85afd4e
MB
10376}
10377
10378void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
10379{
ef316e23
VZ
10380 // we dont allow zero default column width
10381 m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
f85afd4e
MB
10382
10383 if ( resizeExistingCols )
10384 {
b1da8107
SN
10385 // since we are resizing all columns to the default column size,
10386 // we can simply clear the col widths and col rights
10387 // arrays (which also allows us to take advantage of
10388 // some speed optimisations)
10389 m_colWidths.Empty();
10390 m_colRights.Empty();
edb89f7e
VZ
10391 if ( !GetBatchCount() )
10392 CalcDimensions();
f85afd4e
MB
10393 }
10394}
10395
10396void wxGrid::SetColSize( int col, int width )
10397{
b99be8fb 10398 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 10399
ad7502d8
SN
10400 // if < 0 then calculate new width from label
10401 if ( width < 0 )
10402 {
10403 long w, h;
10404 wxArrayString lines;
ad805b9e 10405 wxClientDC dc(m_colWindow);
ad7502d8
SN
10406 dc.SetFont(GetLabelFont());
10407 StringToLines(GetColLabelValue(col), lines);
10408 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
10409 GetTextBoxSize( dc, lines, &w, &h );
10410 else
10411 GetTextBoxSize( dc, lines, &h, &w );
10412 width = w + 6;
ace8d849 10413 //check that it is not less than the minimal width
54181a33 10414 width = wxMax(width, GetColMinimalAcceptableWidth());
ad7502d8
SN
10415 }
10416
a06072d0
VZ
10417 // we intentionally don't test whether the width is less than
10418 // GetColMinimalWidth() here but we do compare it with
10419 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
009c7216
VZ
10420 // #651) -- and we also always allow the width of 0 as it has the special
10421 // sense of hiding the column
10422 if ( width > 0 && width < GetColMinimalAcceptableWidth() )
4db6714b 10423 return;
3e13956a 10424
7c1cb261
VZ
10425 if ( m_colWidths.IsEmpty() )
10426 {
10427 // need to really create the array
10428 InitColWidths();
10429 }
f85afd4e 10430
009c7216
VZ
10431 const int diff = width - m_colWidths[col];
10432 m_colWidths[col] = width;
10433 if ( m_useNativeHeader )
3039ade9 10434 GetGridColHeader()->UpdateColumn(col);
009c7216 10435 //else: will be refreshed when the header is redrawn
60ff3b99 10436
0ed3b812 10437 for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
f85afd4e 10438 {
0ed3b812 10439 m_colRights[GetColAt(colPos)] += diff;
f85afd4e 10440 }
962a48f6 10441
faec5a43 10442 if ( !GetBatchCount() )
ad805b9e 10443 {
faec5a43 10444 CalcDimensions();
ad805b9e
VZ
10445 Refresh();
10446 }
f85afd4e
MB
10447}
10448
43947979
VZ
10449void wxGrid::SetColMinimalWidth( int col, int width )
10450{
4db6714b
KH
10451 if (width > GetColMinimalAcceptableWidth())
10452 {
c6fbe2f0 10453 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 10454 m_colMinWidths[key] = width;
b8d24d4e 10455 }
af547d51
VZ
10456}
10457
10458void wxGrid::SetRowMinimalHeight( int row, int width )
10459{
4db6714b
KH
10460 if (width > GetRowMinimalAcceptableHeight())
10461 {
c6fbe2f0 10462 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 10463 m_rowMinHeights[key] = width;
b8d24d4e 10464 }
43947979
VZ
10465}
10466
10467int wxGrid::GetColMinimalWidth(int col) const
10468{
c6fbe2f0 10469 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 10470 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
962a48f6 10471
ba8c1601 10472 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
af547d51
VZ
10473}
10474
10475int wxGrid::GetRowMinimalHeight(int row) const
10476{
c6fbe2f0 10477 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 10478 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
962a48f6 10479
ba8c1601 10480 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
b8d24d4e
RG
10481}
10482
10483void wxGrid::SetColMinimalAcceptableWidth( int width )
10484{
20c84410 10485 // We do allow a width of 0 since this gives us
962a48f6
DS
10486 // an easy way to temporarily hiding columns.
10487 if ( width >= 0 )
10488 m_minAcceptableColWidth = width;
b8d24d4e
RG
10489}
10490
10491void wxGrid::SetRowMinimalAcceptableHeight( int height )
10492{
20c84410 10493 // We do allow a height of 0 since this gives us
962a48f6
DS
10494 // an easy way to temporarily hiding rows.
10495 if ( height >= 0 )
10496 m_minAcceptableRowHeight = height;
17a1ebd1 10497}
b8d24d4e
RG
10498
10499int wxGrid::GetColMinimalAcceptableWidth() const
10500{
10501 return m_minAcceptableColWidth;
10502}
10503
10504int wxGrid::GetRowMinimalAcceptableHeight() const
10505{
10506 return m_minAcceptableRowHeight;
43947979
VZ
10507}
10508
57c086ef
VZ
10509// ----------------------------------------------------------------------------
10510// auto sizing
10511// ----------------------------------------------------------------------------
10512
733f486a
VZ
10513void
10514wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
65e4e78e 10515{
733f486a
VZ
10516 const bool column = direction == wxGRID_COLUMN;
10517
65e4e78e
VZ
10518 wxClientDC dc(m_gridWin);
10519
962a48f6 10520 // cancel editing of cell
13f6e9e8
RG
10521 HideCellEditControl();
10522 SaveEditControlValue();
10523
962a48f6 10524 // init both of them to avoid compiler warnings, even if we only need one
a95e38c0
VZ
10525 int row = -1,
10526 col = -1;
af547d51
VZ
10527 if ( column )
10528 col = colOrRow;
10529 else
10530 row = colOrRow;
10531
10532 wxCoord extent, extentMax = 0;
10533 int max = column ? m_numRows : m_numCols;
39bcce60 10534 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 10535 {
af547d51
VZ
10536 if ( column )
10537 row = rowOrCol;
10538 else
10539 col = rowOrCol;
10540
2f024384
DS
10541 wxGridCellAttr *attr = GetCellAttr(row, col);
10542 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
10543 if ( renderer )
10544 {
af547d51
VZ
10545 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
10546 extent = column ? size.x : size.y;
10547 if ( extent > extentMax )
af547d51 10548 extentMax = extent;
0b190b0f
VZ
10549
10550 renderer->DecRef();
65e4e78e
VZ
10551 }
10552
10553 attr->DecRef();
10554 }
10555
af547d51
VZ
10556 // now also compare with the column label extent
10557 wxCoord w, h;
65e4e78e 10558 dc.SetFont( GetLabelFont() );
294d195c
MB
10559
10560 if ( column )
d43851f7 10561 {
9d4b8a5d 10562 dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h );
4db6714b 10563 if ( GetColLabelTextOrientation() == wxVERTICAL )
d43851f7
JS
10564 w = h;
10565 }
294d195c 10566 else
9d4b8a5d 10567 dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h );
294d195c 10568
af547d51
VZ
10569 extent = column ? w : h;
10570 if ( extent > extentMax )
af547d51 10571 extentMax = extent;
65e4e78e 10572
af547d51 10573 if ( !extentMax )
65e4e78e 10574 {
af547d51 10575 // empty column - give default extent (notice that if extentMax is less
2f024384 10576 // than default extent but != 0, it's OK)
af547d51 10577 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
10578 }
10579 else
10580 {
a95e38c0 10581 if ( column )
a95e38c0
VZ
10582 // leave some space around text
10583 extentMax += 10;
f6bcfd97 10584 else
f6bcfd97 10585 extentMax += 6;
65e4e78e
VZ
10586 }
10587
edb89f7e
VZ
10588 if ( column )
10589 {
a01cfc08
VS
10590 // Ensure automatic width is not less than minimal width. See the
10591 // comment in SetColSize() for explanation of why this isn't done
10592 // in SetColSize().
10593 if ( !setAsMin )
10594 extentMax = wxMax(extentMax, GetColMinimalWidth(col));
10595
962a48f6 10596 SetColSize( col, extentMax );
faec5a43
SN
10597 if ( !GetBatchCount() )
10598 {
ad805b9e
VZ
10599 if ( m_useNativeHeader )
10600 {
3039ade9 10601 GetGridColHeader()->UpdateColumn(col);
ad805b9e
VZ
10602 }
10603 else
10604 {
10605 int cw, ch, dummy;
10606 m_gridWin->GetClientSize( &cw, &ch );
10607 wxRect rect ( CellToRect( 0, col ) );
10608 rect.y = 0;
10609 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
10610 rect.width = cw - rect.x;
10611 rect.height = m_colLabelHeight;
10612 GetColLabelWindow()->Refresh( true, &rect );
10613 }
edb89f7e
VZ
10614 }
10615 }
10616 else
10617 {
a01cfc08
VS
10618 // Ensure automatic width is not less than minimal height. See the
10619 // comment in SetColSize() for explanation of why this isn't done
10620 // in SetRowSize().
10621 if ( !setAsMin )
10622 extentMax = wxMax(extentMax, GetRowMinimalHeight(row));
10623
39bcce60 10624 SetRowSize(row, extentMax);
faec5a43
SN
10625 if ( !GetBatchCount() )
10626 {
edb89f7e
VZ
10627 int cw, ch, dummy;
10628 m_gridWin->GetClientSize( &cw, &ch );
ccdee36f 10629 wxRect rect( CellToRect( row, 0 ) );
edb89f7e
VZ
10630 rect.x = 0;
10631 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10632 rect.width = m_rowLabelWidth;
faec5a43 10633 rect.height = ch - rect.y;
ca65c044 10634 m_rowLabelWin->Refresh( true, &rect );
edb89f7e 10635 }
faec5a43 10636 }
2f024384 10637
65e4e78e
VZ
10638 if ( setAsMin )
10639 {
af547d51
VZ
10640 if ( column )
10641 SetColMinimalWidth(col, extentMax);
10642 else
39bcce60 10643 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
10644 }
10645}
10646
733f486a
VZ
10647wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction)
10648{
10649 // calculate size for the rows or columns?
10650 const bool calcRows = direction == wxGRID_ROW;
10651
10652 wxClientDC dc(calcRows ? GetGridRowLabelWindow()
10653 : GetGridColLabelWindow());
10654 dc.SetFont(GetLabelFont());
10655
10656 // which dimension should we take into account for calculations?
10657 //
10658 // for columns, the text can be only horizontal so it's easy but for rows
10659 // we also have to take into account the text orientation
10660 const bool
10661 useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL);
10662
10663 wxArrayString lines;
10664 wxCoord extentMax = 0;
10665
10666 const int numRowsOrCols = calcRows ? m_numRows : m_numCols;
10667 for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ )
10668 {
10669 lines.Clear();
add4bb40
VS
10670
10671 wxString label = calcRows ? GetRowLabelValue(rowOrCol)
10672 : GetColLabelValue(rowOrCol);
10673 StringToLines(label, lines);
733f486a
VZ
10674
10675 long w, h;
10676 GetTextBoxSize(dc, lines, &w, &h);
10677
10678 const wxCoord extent = useWidth ? w : h;
10679 if ( extent > extentMax )
10680 extentMax = extent;
10681 }
10682
10683 if ( !extentMax )
10684 {
10685 // empty column - give default extent (notice that if extentMax is less
10686 // than default extent but != 0, it's OK)
10687 extentMax = calcRows ? GetDefaultRowLabelSize()
10688 : GetDefaultColLabelSize();
10689 }
10690
10691 // leave some space around text (taken from AutoSizeColOrRow)
10692 if ( calcRows )
10693 extentMax += 10;
10694 else
10695 extentMax += 6;
10696
10697 return extentMax;
10698}
10699
266e8367 10700int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 10701{
57c086ef
VZ
10702 int width = m_rowLabelWidth;
10703
b62f94ff
VZ
10704 wxGridUpdateLocker locker;
10705 if(!calcOnly)
10706 locker.Create(this);
97a9929e 10707
65e4e78e
VZ
10708 for ( int col = 0; col < m_numCols; col++ )
10709 {
266e8367 10710 if ( !calcOnly )
266e8367 10711 AutoSizeColumn(col, setAsMin);
57c086ef
VZ
10712
10713 width += GetColWidth(col);
65e4e78e 10714 }
97a9929e 10715
266e8367 10716 return width;
65e4e78e
VZ
10717}
10718
266e8367 10719int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
10720{
10721 int height = m_colLabelHeight;
10722
b62f94ff
VZ
10723 wxGridUpdateLocker locker;
10724 if(!calcOnly)
10725 locker.Create(this);
97a9929e 10726
57c086ef
VZ
10727 for ( int row = 0; row < m_numRows; row++ )
10728 {
af547d51 10729 if ( !calcOnly )
af547d51 10730 AutoSizeRow(row, setAsMin);
57c086ef
VZ
10731
10732 height += GetRowHeight(row);
10733 }
97a9929e 10734
266e8367 10735 return height;
57c086ef
VZ
10736}
10737
10738void wxGrid::AutoSize()
10739{
b62f94ff 10740 wxGridUpdateLocker locker(this);
97a9929e 10741
0ed3b812
VZ
10742 wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth,
10743 SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight);
97a9929e 10744
0ed3b812
VZ
10745 // we know that we're not going to have scrollbars so disable them now to
10746 // avoid trouble in SetClientSize() which can otherwise set the correct
10747 // client size but also leave space for (not needed any more) scrollbars
39621ee0 10748 SetScrollbars(0, 0, 0, 0, 0, 0, true);
72b0a1de
VZ
10749
10750 // restore the scroll rate parameters overwritten by SetScrollbars()
10751 SetScrollRate(m_scrollLineX, m_scrollLineY);
10752
10753 SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight);
266e8367
VZ
10754}
10755
d43851f7
JS
10756void wxGrid::AutoSizeRowLabelSize( int row )
10757{
d43851f7 10758 // Hide the edit control, so it
4db6714b
KH
10759 // won't interfere with drag-shrinking.
10760 if ( IsCellEditControlShown() )
d43851f7
JS
10761 {
10762 HideCellEditControl();
10763 SaveEditControlValue();
10764 }
10765
10766 // autosize row height depending on label text
ad7502d8 10767 SetRowSize(row, -1);
d43851f7
JS
10768 ForceRefresh();
10769}
10770
10771void wxGrid::AutoSizeColLabelSize( int col )
10772{
d43851f7 10773 // Hide the edit control, so it
c2f5b920 10774 // won't interfere with drag-shrinking.
4db6714b 10775 if ( IsCellEditControlShown() )
d43851f7
JS
10776 {
10777 HideCellEditControl();
10778 SaveEditControlValue();
10779 }
10780
10781 // autosize column width depending on label text
ad7502d8 10782 SetColSize(col, -1);
d43851f7
JS
10783 ForceRefresh();
10784}
10785
266e8367
VZ
10786wxSize wxGrid::DoGetBestSize() const
10787{
266e8367
VZ
10788 wxGrid *self = (wxGrid *)this; // const_cast
10789
dc4689ef
VZ
10790 // we do the same as in AutoSize() here with the exception that we don't
10791 // change the column/row sizes, only calculate them
10792 wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth,
10793 self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight);
962a48f6 10794
6d308072
RD
10795 // NOTE: This size should be cached, but first we need to add calls to
10796 // InvalidateBestSize everywhere that could change the results of this
10797 // calculation.
10798 // CacheBestSize(size);
962a48f6 10799
72b0a1de 10800 return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight)
dc4689ef 10801 + GetWindowBorderSize();
266e8367
VZ
10802}
10803
10804void wxGrid::Fit()
10805{
10806 AutoSize();
57c086ef
VZ
10807}
10808
6fc0f38f
SN
10809wxPen& wxGrid::GetDividerPen() const
10810{
10811 return wxNullPen;
10812}
10813
57c086ef
VZ
10814// ----------------------------------------------------------------------------
10815// cell value accessor functions
10816// ----------------------------------------------------------------------------
f85afd4e
MB
10817
10818void wxGrid::SetCellValue( int row, int col, const wxString& s )
10819{
10820 if ( m_table )
10821 {
f6bcfd97 10822 m_table->SetValue( row, col, s );
2d66e025
MB
10823 if ( !GetBatchCount() )
10824 {
27f35b66
SN
10825 int dummy;
10826 wxRect rect( CellToRect( row, col ) );
10827 rect.x = 0;
10828 rect.width = m_gridWin->GetClientSize().GetWidth();
10829 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
ca65c044 10830 m_gridWin->Refresh( false, &rect );
2d66e025 10831 }
60ff3b99 10832
f85afd4e 10833 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 10834 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
10835 IsCellEditControlShown())
10836 // Note: If we are using IsCellEditControlEnabled,
10837 // this interacts badly with calling SetCellValue from
10838 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 10839 {
4cfa5de6
RD
10840 HideCellEditControl();
10841 ShowCellEditControl(); // will reread data from table
f85afd4e 10842 }
f85afd4e
MB
10843 }
10844}
10845
962a48f6 10846// ----------------------------------------------------------------------------
2f024384 10847// block, row and column selection
962a48f6 10848// ----------------------------------------------------------------------------
f85afd4e
MB
10849
10850void wxGrid::SelectRow( int row, bool addToSelected )
10851{
8b5f6d9d
VZ
10852 if ( !m_selection )
10853 return;
10854
10855 if ( !addToSelected )
e32352cf 10856 ClearSelection();
70c7a608 10857
8b5f6d9d 10858 m_selection->SelectRow(row);
f85afd4e
MB
10859}
10860
f85afd4e
MB
10861void wxGrid::SelectCol( int col, bool addToSelected )
10862{
8b5f6d9d
VZ
10863 if ( !m_selection )
10864 return;
10865
10866 if ( !addToSelected )
e32352cf 10867 ClearSelection();
f85afd4e 10868
8b5f6d9d 10869 m_selection->SelectCol(col);
f85afd4e
MB
10870}
10871
8b5f6d9d
VZ
10872void wxGrid::SelectBlock(int topRow, int leftCol, int bottomRow, int rightCol,
10873 bool addToSelected)
f85afd4e 10874{
8b5f6d9d
VZ
10875 if ( !m_selection )
10876 return;
10877
10878 if ( !addToSelected )
c9097836 10879 ClearSelection();
f85afd4e 10880
8b5f6d9d 10881 m_selection->SelectBlock(topRow, leftCol, bottomRow, rightCol);
f85afd4e
MB
10882}
10883
10884void wxGrid::SelectAll()
10885{
f74d0b57 10886 if ( m_numRows > 0 && m_numCols > 0 )
3f3dc2ef
VZ
10887 {
10888 if ( m_selection )
ccdee36f 10889 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
3f3dc2ef 10890 }
b5808881 10891}
f85afd4e 10892
962a48f6
DS
10893// ----------------------------------------------------------------------------
10894// cell, row and col deselection
10895// ----------------------------------------------------------------------------
f7b4b343 10896
bec70262 10897void wxGrid::DeselectLine(int line, const wxGridOperations& oper)
f7b4b343 10898{
3f3dc2ef
VZ
10899 if ( !m_selection )
10900 return;
10901
bec70262
VZ
10902 const wxGridSelectionModes mode = m_selection->GetSelectionMode();
10903 if ( mode == oper.GetSelectionMode() )
f7b4b343 10904 {
bec70262
VZ
10905 const wxGridCellCoords c(oper.MakeCoords(line, 0));
10906 if ( m_selection->IsInSelection(c) )
10907 m_selection->ToggleCellSelection(c);
ffdd3c98 10908 }
bec70262 10909 else if ( mode != oper.Dual().GetSelectionMode() )
f7b4b343 10910 {
bec70262
VZ
10911 const int nOther = oper.Dual().GetNumberOfLines(this);
10912 for ( int i = 0; i < nOther; i++ )
f7b4b343 10913 {
bec70262
VZ
10914 const wxGridCellCoords c(oper.MakeCoords(line, i));
10915 if ( m_selection->IsInSelection(c) )
10916 m_selection->ToggleCellSelection(c);
f7b4b343
VZ
10917 }
10918 }
bec70262
VZ
10919 //else: can only select orthogonal lines so no lines in this direction
10920 // could have been selected anyhow
f7b4b343
VZ
10921}
10922
bec70262 10923void wxGrid::DeselectRow(int row)
f7b4b343 10924{
bec70262
VZ
10925 DeselectLine(row, wxGridRowOperations());
10926}
3f3dc2ef 10927
bec70262
VZ
10928void wxGrid::DeselectCol(int col)
10929{
10930 DeselectLine(col, wxGridColumnOperations());
f7b4b343
VZ
10931}
10932
10933void wxGrid::DeselectCell( int row, int col )
10934{
3f3dc2ef 10935 if ( m_selection && m_selection->IsInSelection(row, col) )
f7b4b343
VZ
10936 m_selection->ToggleCellSelection(row, col);
10937}
10938
ef316e23 10939bool wxGrid::IsSelection() const
b5808881 10940{
3f3dc2ef 10941 return ( m_selection && (m_selection->IsSelection() ||
8a3e536c
VZ
10942 ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
10943 m_selectedBlockBottomRight != wxGridNoCellCoords) ) );
f85afd4e
MB
10944}
10945
84035150 10946bool wxGrid::IsInSelection( int row, int col ) const
b5808881 10947{
3f3dc2ef 10948 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
8a3e536c
VZ
10949 ( row >= m_selectedBlockTopLeft.GetRow() &&
10950 col >= m_selectedBlockTopLeft.GetCol() &&
10951 row <= m_selectedBlockBottomRight.GetRow() &&
10952 col <= m_selectedBlockBottomRight.GetCol() )) );
b5808881 10953}
f85afd4e 10954
aa5b8857
SN
10955wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10956{
4db6714b
KH
10957 if (!m_selection)
10958 {
10959 wxGridCellCoordsArray a;
10960 return a;
10961 }
10962
aa5b8857
SN
10963 return m_selection->m_cellSelection;
10964}
4db6714b 10965
aa5b8857
SN
10966wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10967{
4db6714b
KH
10968 if (!m_selection)
10969 {
10970 wxGridCellCoordsArray a;
10971 return a;
10972 }
10973
aa5b8857
SN
10974 return m_selection->m_blockSelectionTopLeft;
10975}
4db6714b 10976
aa5b8857
SN
10977wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10978{
4db6714b
KH
10979 if (!m_selection)
10980 {
10981 wxGridCellCoordsArray a;
10982 return a;
10983 }
10984
a8de8190 10985 return m_selection->m_blockSelectionBottomRight;
aa5b8857 10986}
4db6714b 10987
aa5b8857
SN
10988wxArrayInt wxGrid::GetSelectedRows() const
10989{
4db6714b
KH
10990 if (!m_selection)
10991 {
10992 wxArrayInt a;
10993 return a;
10994 }
10995
aa5b8857
SN
10996 return m_selection->m_rowSelection;
10997}
4db6714b 10998
aa5b8857
SN
10999wxArrayInt wxGrid::GetSelectedCols() const
11000{
4db6714b
KH
11001 if (!m_selection)
11002 {
11003 wxArrayInt a;
11004 return a;
11005 }
11006
aa5b8857
SN
11007 return m_selection->m_colSelection;
11008}
11009
f85afd4e
MB
11010void wxGrid::ClearSelection()
11011{
8a3e536c
VZ
11012 wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft,
11013 m_selectedBlockBottomRight);
11014 wxRect r2 = BlockToDeviceRect(m_currentCellCoords,
11015 m_selectedBlockCorner);
11016
11017 m_selectedBlockTopLeft =
11018 m_selectedBlockBottomRight =
11019 m_selectedBlockCorner = wxGridNoCellCoords;
11020
6fb1c1cb
SN
11021 Refresh( false, &r1 );
11022 Refresh( false, &r2 );
8a3e536c 11023
3f3dc2ef
VZ
11024 if ( m_selection )
11025 m_selection->ClearSelection();
8f177c8e 11026}
f85afd4e 11027
da6af900 11028// This function returns the rectangle that encloses the given block
2d66e025
MB
11029// in device coords clipped to the client size of the grid window.
11030//
731330ec
VZ
11031wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft,
11032 const wxGridCellCoords& bottomRight ) const
f85afd4e 11033{
731330ec
VZ
11034 wxRect resultRect;
11035 wxRect tempCellRect = CellToRect(topLeft);
11036 if ( tempCellRect != wxGridNoCellRect )
f85afd4e 11037 {
731330ec 11038 resultRect = tempCellRect;
58dd5b3b
MB
11039 }
11040 else
11041 {
731330ec 11042 resultRect = wxRect(0, 0, 0, 0);
58dd5b3b 11043 }
60ff3b99 11044
731330ec
VZ
11045 tempCellRect = CellToRect(bottomRight);
11046 if ( tempCellRect != wxGridNoCellRect )
58dd5b3b 11047 {
731330ec 11048 resultRect += tempCellRect;
2d66e025
MB
11049 }
11050 else
11051 {
731330ec 11052 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
2d66e025 11053 return wxGridNoCellRect;
f85afd4e
MB
11054 }
11055
731330ec
VZ
11056 // Ensure that left/right and top/bottom pairs are in order.
11057 int left = resultRect.GetLeft();
11058 int top = resultRect.GetTop();
11059 int right = resultRect.GetRight();
11060 int bottom = resultRect.GetBottom();
27f35b66
SN
11061
11062 int leftCol = topLeft.GetCol();
11063 int topRow = topLeft.GetRow();
11064 int rightCol = bottomRight.GetCol();
11065 int bottomRow = bottomRight.GetRow();
11066
3ed884a0
SN
11067 if (left > right)
11068 {
731330ec 11069 int tmp = left;
3ed884a0 11070 left = right;
731330ec
VZ
11071 right = tmp;
11072
11073 tmp = leftCol;
4db6714b 11074 leftCol = rightCol;
731330ec 11075 rightCol = tmp;
3ed884a0
SN
11076 }
11077
11078 if (top > bottom)
11079 {
731330ec 11080 int tmp = top;
3ed884a0 11081 top = bottom;
731330ec
VZ
11082 bottom = tmp;
11083
11084 tmp = topRow;
3ed884a0 11085 topRow = bottomRow;
731330ec 11086 bottomRow = tmp;
3ed884a0
SN
11087 }
11088
731330ec
VZ
11089 // The following loop is ONLY necessary to detect and handle merged cells.
11090 int cw, ch;
11091 m_gridWin->GetClientSize( &cw, &ch );
11092
11093 // Get the origin coordinates: notice that they will be negative if the
11094 // grid is scrolled downwards/to the right.
11095 int gridOriginX = 0;
11096 int gridOriginY = 0;
11097 CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY);
11098
11099 int onScreenLeftmostCol = internalXToCol(-gridOriginX);
11100 int onScreenUppermostRow = internalYToRow(-gridOriginY);
11101
11102 int onScreenRightmostCol = internalXToCol(-gridOriginX + cw);
11103 int onScreenBottommostRow = internalYToRow(-gridOriginY + ch);
11104
11105 // Bound our loop so that we only examine the portion of the selected block
11106 // that is shown on screen. Therefore, we compare the Top-Left block values
11107 // to the Top-Left screen values, and the Bottom-Right block values to the
11108 // Bottom-Right screen values, choosing appropriately.
11109 const int visibleTopRow = wxMax(topRow, onScreenUppermostRow);
11110 const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow);
11111 const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol);
11112 const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol);
11113
11114 for ( int j = visibleTopRow; j <= visibleBottomRow; j++ )
27f35b66 11115 {
731330ec 11116 for ( int i = visibleLeftCol; i <= visibleRightCol; i++ )
27f35b66 11117 {
731330ec
VZ
11118 if ( (j == visibleTopRow) || (j == visibleBottomRow) ||
11119 (i == visibleLeftCol) || (i == visibleRightCol) )
27f35b66 11120 {
731330ec 11121 tempCellRect = CellToRect( j, i );
27f35b66 11122
731330ec
VZ
11123 if (tempCellRect.x < left)
11124 left = tempCellRect.x;
11125 if (tempCellRect.y < top)
11126 top = tempCellRect.y;
11127 if (tempCellRect.x + tempCellRect.width > right)
11128 right = tempCellRect.x + tempCellRect.width;
11129 if (tempCellRect.y + tempCellRect.height > bottom)
11130 bottom = tempCellRect.y + tempCellRect.height;
27f35b66 11131 }
4db6714b
KH
11132 else
11133 {
731330ec 11134 i = visibleRightCol; // jump over inner cells.
4db6714b 11135 }
27f35b66
SN
11136 }
11137 }
11138
731330ec 11139 // Convert to scrolled coords
27f35b66
SN
11140 CalcScrolledPosition( left, top, &left, &top );
11141 CalcScrolledPosition( right, bottom, &right, &bottom );
58dd5b3b 11142
f6bcfd97 11143 if (right < 0 || bottom < 0 || left > cw || top > ch)
c47addef 11144 return wxRect(0,0,0,0);
f6bcfd97 11145
731330ec
VZ
11146 resultRect.SetLeft( wxMax(0, left) );
11147 resultRect.SetTop( wxMax(0, top) );
11148 resultRect.SetRight( wxMin(cw, right) );
11149 resultRect.SetBottom( wxMin(ch, bottom) );
58dd5b3b 11150
731330ec 11151 return resultRect;
f85afd4e
MB
11152}
11153
1edce33f
VZ
11154// ----------------------------------------------------------------------------
11155// drop target
11156// ----------------------------------------------------------------------------
11157
11158#if wxUSE_DRAG_AND_DROP
11159
11160// this allow setting drop target directly on wxGrid
11161void wxGrid::SetDropTarget(wxDropTarget *dropTarget)
11162{
11163 GetGridWindow()->SetDropTarget(dropTarget);
11164}
11165
11166#endif // wxUSE_DRAG_AND_DROP
11167
962a48f6
DS
11168// ----------------------------------------------------------------------------
11169// grid event classes
11170// ----------------------------------------------------------------------------
f85afd4e 11171
bf7945ce 11172IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
11173
11174wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 11175 int row, int col, int x, int y, bool sel,
f85afd4e 11176 bool control, bool shift, bool alt, bool meta )
8b5f6d9d
VZ
11177 : wxNotifyEvent( type, id ),
11178 wxKeyboardState(control, shift, alt, meta)
f85afd4e 11179{
8b5f6d9d 11180 Init(row, col, x, y, sel);
8f177c8e 11181
f85afd4e
MB
11182 SetEventObject(obj);
11183}
11184
bf7945ce 11185IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
11186
11187wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
11188 int rowOrCol, int x, int y,
11189 bool control, bool shift, bool alt, bool meta )
8b5f6d9d
VZ
11190 : wxNotifyEvent( type, id ),
11191 wxKeyboardState(control, shift, alt, meta)
f85afd4e 11192{
8b5f6d9d 11193 Init(rowOrCol, x, y);
8f177c8e 11194
f85afd4e
MB
11195 SetEventObject(obj);
11196}
11197
11198
bf7945ce 11199IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
11200
11201wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
11202 const wxGridCellCoords& topLeft,
11203 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
11204 bool sel, bool control,
11205 bool shift, bool alt, bool meta )
8b5f6d9d
VZ
11206 : wxNotifyEvent( type, id ),
11207 wxKeyboardState(control, shift, alt, meta)
11208{
11209 Init(topLeft, bottomRight, sel);
f85afd4e
MB
11210
11211 SetEventObject(obj);
11212}
11213
11214
bf7945ce
RD
11215IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
11216
11217wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
11218 wxObject* obj, int row,
11219 int col, wxControl* ctrl)
11220 : wxCommandEvent(type, id)
11221{
11222 SetEventObject(obj);
11223 m_row = row;
11224 m_col = col;
11225 m_ctrl = ctrl;
11226}
11227
27b92ca4 11228#endif // wxUSE_GRID