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