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