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