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