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