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