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