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