]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Initialize the field count in Create(). It was using a garbage value
[wxWidgets.git] / src / generic / grid.cpp
CommitLineData
2796cce3 1///////////////////////////////////////////////////////////////////////////
d16c04bb 2// Name: generic/grid.cpp
f85afd4e
MB
3// Purpose: wxGrid and related classes
4// Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5// Modified by:
6// Created: 1/08/1999
7// RCS-ID: $Id$
8// Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
758cbedf
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
f85afd4e
MB
20#ifdef __GNUG__
21 #pragma implementation "grid.h"
22#endif
23
4d85bcd1
JS
24// For compilers that support precompilation, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#include "wx/defs.h"
f85afd4e
MB
28
29#ifdef __BORLANDC__
30 #pragma hdrstop
31#endif
32
8f177c8e 33#if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
4d85bcd1
JS
34#include "gridg.cpp"
35#else
36
f85afd4e
MB
37#ifndef WX_PRECOMP
38 #include "wx/utils.h"
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
41 #include "wx/log.h"
508011ce
VZ
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
4ee5fc9c 44 #include "wx/combobox.h"
816be743 45 #include "wx/valtext.h"
f85afd4e
MB
46#endif
47
cb5df486 48#include "wx/textfile.h"
816be743 49#include "wx/spinctrl.h"
c4608a8a 50#include "wx/tokenzr.h"
6d004f67 51
07296f0b 52#include "wx/grid.h"
b5808881 53#include "wx/generic/gridsel.h"
07296f0b 54
0b7e6e7d
SN
55#if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
c78b3acd 57#else
0b7e6e7d 58 #define WXUNUSED_MOTIF(identifier) identifier
c78b3acd
SN
59#endif
60
61#if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
63#else
64 #define WXUNUSED_GTK(identifier) identifier
65#endif
66
3f8e5072
JS
67// Required for wxIs... functions
68#include <ctype.h>
69
b99be8fb 70// ----------------------------------------------------------------------------
758cbedf 71// array classes
b99be8fb
VZ
72// ----------------------------------------------------------------------------
73
758cbedf
VZ
74WX_DEFINE_ARRAY(wxGridCellAttr *, wxArrayAttrs);
75
b99be8fb
VZ
76struct wxGridCellWithAttr
77{
2e9a6788
VZ
78 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
79 : coords(row, col), attr(attr_)
b99be8fb
VZ
80 {
81 }
82
2e9a6788
VZ
83 ~wxGridCellWithAttr()
84 {
85 attr->DecRef();
86 }
87
b99be8fb 88 wxGridCellCoords coords;
2e9a6788 89 wxGridCellAttr *attr;
b99be8fb
VZ
90};
91
92WX_DECLARE_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray);
93
94#include "wx/arrimpl.cpp"
95
96WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
97WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
98
99// ----------------------------------------------------------------------------
100// private classes
101// ----------------------------------------------------------------------------
102
103class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow
104{
105public:
106 wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
107 wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
108 const wxPoint &pos, const wxSize &size );
109
110private:
111 wxGrid *m_owner;
112
113 void OnPaint( wxPaintEvent& event );
114 void OnMouseEvent( wxMouseEvent& event );
115 void OnKeyDown( wxKeyEvent& event );
116
117 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow)
118 DECLARE_EVENT_TABLE()
119};
120
121
122class WXDLLEXPORT wxGridColLabelWindow : public wxWindow
123{
124public:
125 wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
126 wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
127 const wxPoint &pos, const wxSize &size );
128
129private:
130 wxGrid *m_owner;
131
132 void OnPaint( wxPaintEvent &event );
133 void OnMouseEvent( wxMouseEvent& event );
134 void OnKeyDown( wxKeyEvent& event );
135
136 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow)
137 DECLARE_EVENT_TABLE()
138};
139
140
141class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow
142{
143public:
144 wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
145 wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
146 const wxPoint &pos, const wxSize &size );
147
148private:
149 wxGrid *m_owner;
150
151 void OnMouseEvent( wxMouseEvent& event );
152 void OnKeyDown( wxKeyEvent& event );
153 void OnPaint( wxPaintEvent& event );
154
155 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow)
156 DECLARE_EVENT_TABLE()
157};
158
159class WXDLLEXPORT wxGridWindow : public wxPanel
160{
161public:
162 wxGridWindow()
163 {
164 m_owner = (wxGrid *)NULL;
165 m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
166 m_colLabelWin = (wxGridColLabelWindow *)NULL;
167 }
168
169 wxGridWindow( wxGrid *parent,
170 wxGridRowLabelWindow *rowLblWin,
171 wxGridColLabelWindow *colLblWin,
172 wxWindowID id, const wxPoint &pos, const wxSize &size );
173 ~wxGridWindow();
174
175 void ScrollWindow( int dx, int dy, const wxRect *rect );
176
177private:
178 wxGrid *m_owner;
179 wxGridRowLabelWindow *m_rowLabelWin;
180 wxGridColLabelWindow *m_colLabelWin;
181
182 void OnPaint( wxPaintEvent &event );
183 void OnMouseEvent( wxMouseEvent& event );
184 void OnKeyDown( wxKeyEvent& );
2796cce3
RD
185 void OnEraseBackground( wxEraseEvent& );
186
b99be8fb
VZ
187
188 DECLARE_DYNAMIC_CLASS(wxGridWindow)
189 DECLARE_EVENT_TABLE()
190};
191
2796cce3
RD
192
193
194class wxGridCellEditorEvtHandler : public wxEvtHandler
195{
196public:
197 wxGridCellEditorEvtHandler()
198 : m_grid(0), m_editor(0)
199 { }
200 wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
201 : m_grid(grid), m_editor(editor)
202 { }
203
204 void OnKeyDown(wxKeyEvent& event);
fb0de762 205 void OnChar(wxKeyEvent& event);
2796cce3
RD
206
207private:
208 wxGrid* m_grid;
209 wxGridCellEditor* m_editor;
210 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
211 DECLARE_EVENT_TABLE()
212};
213
214
215IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
216BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
217 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
fb0de762 218 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
2796cce3
RD
219END_EVENT_TABLE()
220
221
222
758cbedf 223// ----------------------------------------------------------------------------
b99be8fb 224// the internal data representation used by wxGridCellAttrProvider
758cbedf
VZ
225// ----------------------------------------------------------------------------
226
227// this class stores attributes set for cells
228class WXDLLEXPORT wxGridCellAttrData
b99be8fb
VZ
229{
230public:
2e9a6788 231 void SetAttr(wxGridCellAttr *attr, int row, int col);
b99be8fb 232 wxGridCellAttr *GetAttr(int row, int col) const;
4d60017a
SN
233 void UpdateAttrRows( size_t pos, int numRows );
234 void UpdateAttrCols( size_t pos, int numCols );
b99be8fb
VZ
235
236private:
237 // searches for the attr for given cell, returns wxNOT_FOUND if not found
238 int FindIndex(int row, int col) const;
239
240 wxGridCellWithAttrArray m_attrs;
241};
242
758cbedf
VZ
243// this class stores attributes set for rows or columns
244class WXDLLEXPORT wxGridRowOrColAttrData
245{
246public:
ee6694a7
VZ
247 // empty ctor to suppress warnings
248 wxGridRowOrColAttrData() { }
758cbedf
VZ
249 ~wxGridRowOrColAttrData();
250
251 void SetAttr(wxGridCellAttr *attr, int rowOrCol);
252 wxGridCellAttr *GetAttr(int rowOrCol) const;
4d60017a 253 void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
758cbedf
VZ
254
255private:
256 wxArrayInt m_rowsOrCols;
257 wxArrayAttrs m_attrs;
258};
259
260// NB: this is just a wrapper around 3 objects: one which stores cell
261// attributes, and 2 others for row/col ones
262class WXDLLEXPORT wxGridCellAttrProviderData
263{
264public:
265 wxGridCellAttrData m_cellAttrs;
266 wxGridRowOrColAttrData m_rowAttrs,
267 m_colAttrs;
268};
269
f2d76237
RD
270
271// ----------------------------------------------------------------------------
272// data structures used for the data type registry
273// ----------------------------------------------------------------------------
274
b94ae1ea
VZ
275struct wxGridDataTypeInfo
276{
f2d76237
RD
277 wxGridDataTypeInfo(const wxString& typeName,
278 wxGridCellRenderer* renderer,
279 wxGridCellEditor* editor)
280 : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
281 { }
282
39bcce60
VZ
283 ~wxGridDataTypeInfo()
284 {
285 wxSafeDecRef(m_renderer);
286 wxSafeDecRef(m_editor);
287 }
f2d76237
RD
288
289 wxString m_typeName;
290 wxGridCellRenderer* m_renderer;
291 wxGridCellEditor* m_editor;
292};
293
294
295WX_DEFINE_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
296
297
b94ae1ea
VZ
298class WXDLLEXPORT wxGridTypeRegistry
299{
f2d76237 300public:
c78b3acd 301 wxGridTypeRegistry() {}
f2d76237 302 ~wxGridTypeRegistry();
b94ae1ea 303
f2d76237
RD
304 void RegisterDataType(const wxString& typeName,
305 wxGridCellRenderer* renderer,
306 wxGridCellEditor* editor);
c4608a8a
VZ
307
308 // find one of already registered data types
309 int FindRegisteredDataType(const wxString& typeName);
310
311 // try to FindRegisteredDataType(), if this fails and typeName is one of
312 // standard typenames, register it and return its index
f2d76237 313 int FindDataType(const wxString& typeName);
c4608a8a
VZ
314
315 // try to FindDataType(), if it fails see if it is not one of already
316 // registered data types with some params in which case clone the
317 // registered data type and set params for it
318 int FindOrCloneDataType(const wxString& typeName);
319
f2d76237
RD
320 wxGridCellRenderer* GetRenderer(int index);
321 wxGridCellEditor* GetEditor(int index);
322
323private:
324 wxGridDataTypeInfoArray m_typeinfo;
325};
326
b99be8fb
VZ
327// ----------------------------------------------------------------------------
328// conditional compilation
329// ----------------------------------------------------------------------------
330
9496deb5
MB
331#ifndef WXGRID_DRAW_LINES
332#define WXGRID_DRAW_LINES 1
796df70a
SN
333#endif
334
0a976765
VZ
335// ----------------------------------------------------------------------------
336// globals
337// ----------------------------------------------------------------------------
338
339//#define DEBUG_ATTR_CACHE
340#ifdef DEBUG_ATTR_CACHE
341 static size_t gs_nAttrCacheHits = 0;
342 static size_t gs_nAttrCacheMisses = 0;
343#endif // DEBUG_ATTR_CACHE
f85afd4e 344
43947979
VZ
345// ----------------------------------------------------------------------------
346// constants
347// ----------------------------------------------------------------------------
348
f85afd4e
MB
349wxGridCellCoords wxGridNoCellCoords( -1, -1 );
350wxRect wxGridNoCellRect( -1, -1, -1, -1 );
351
f0102d2a
VZ
352// scroll line size
353// TODO: fixed so far - make configurable later (and also different for x/y)
354static const size_t GRID_SCROLL_LINE = 10;
f85afd4e 355
43947979
VZ
356// the size of hash tables used a bit everywhere (the max number of elements
357// in these hash tables is the number of rows/columns)
358static const int GRID_HASH_SIZE = 100;
359
ab79958a
VZ
360// ============================================================================
361// implementation
362// ============================================================================
363
2796cce3
RD
364// ----------------------------------------------------------------------------
365// wxGridCellEditor
366// ----------------------------------------------------------------------------
367
368wxGridCellEditor::wxGridCellEditor()
369{
370 m_control = NULL;
371}
372
373
374wxGridCellEditor::~wxGridCellEditor()
375{
376 Destroy();
377}
378
508011ce
VZ
379void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
380 wxWindowID WXUNUSED(id),
381 wxEvtHandler* evtHandler)
382{
189d0213 383 if ( evtHandler )
508011ce
VZ
384 m_control->PushEventHandler(evtHandler);
385}
2796cce3 386
189d0213
VZ
387void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
388 wxGridCellAttr *attr)
389{
390 // erase the background because we might not fill the cell
391 wxClientDC dc(m_control->GetParent());
392 dc.SetPen(*wxTRANSPARENT_PEN);
393 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
394 dc.DrawRectangle(rectCell);
395
396 // redraw the control we just painted over
397 m_control->Refresh();
398}
399
2796cce3
RD
400void wxGridCellEditor::Destroy()
401{
508011ce
VZ
402 if (m_control)
403 {
b94ae1ea
VZ
404 m_control->PopEventHandler(TRUE /* delete it*/);
405
2796cce3
RD
406 m_control->Destroy();
407 m_control = NULL;
408 }
409}
410
3da93aae 411void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
2796cce3
RD
412{
413 wxASSERT_MSG(m_control,
414 wxT("The wxGridCellEditor must be Created first!"));
415 m_control->Show(show);
3da93aae
VZ
416
417 if ( show )
418 {
419 // set the colours/fonts if we have any
420 if ( attr )
421 {
f2d76237
RD
422 m_colFgOld = m_control->GetForegroundColour();
423 m_control->SetForegroundColour(attr->GetTextColour());
3da93aae 424
f2d76237
RD
425 m_colBgOld = m_control->GetBackgroundColour();
426 m_control->SetBackgroundColour(attr->GetBackgroundColour());
3da93aae 427
f2d76237
RD
428 m_fontOld = m_control->GetFont();
429 m_control->SetFont(attr->GetFont());
3da93aae
VZ
430
431 // can't do anything more in the base class version, the other
432 // attributes may only be used by the derived classes
433 }
434 }
435 else
436 {
437 // restore the standard colours fonts
438 if ( m_colFgOld.Ok() )
439 {
440 m_control->SetForegroundColour(m_colFgOld);
441 m_colFgOld = wxNullColour;
442 }
443
444 if ( m_colBgOld.Ok() )
445 {
446 m_control->SetBackgroundColour(m_colBgOld);
447 m_colBgOld = wxNullColour;
448 }
449
450 if ( m_fontOld.Ok() )
451 {
452 m_control->SetFont(m_fontOld);
453 m_fontOld = wxNullFont;
454 }
455 }
2796cce3
RD
456}
457
458void wxGridCellEditor::SetSize(const wxRect& rect)
459{
460 wxASSERT_MSG(m_control,
461 wxT("The wxGridCellEditor must be Created first!"));
28a77bc4 462 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
2796cce3
RD
463}
464
465void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
466{
467 event.Skip();
468}
469
470
2c9a89e0
RD
471void wxGridCellEditor::StartingKey(wxKeyEvent& event)
472{
e195a54c
VZ
473 event.Skip();
474}
2c9a89e0 475
e195a54c
VZ
476void wxGridCellEditor::StartingClick()
477{
b54ba671 478}
2c9a89e0 479
b54ba671
VZ
480// ----------------------------------------------------------------------------
481// wxGridCellTextEditor
482// ----------------------------------------------------------------------------
2c9a89e0 483
2796cce3
RD
484wxGridCellTextEditor::wxGridCellTextEditor()
485{
c4608a8a 486 m_maxChars = 0;
2796cce3
RD
487}
488
489void wxGridCellTextEditor::Create(wxWindow* parent,
490 wxWindowID id,
2796cce3
RD
491 wxEvtHandler* evtHandler)
492{
508011ce 493 m_control = new wxTextCtrl(parent, id, wxEmptyString,
2c9a89e0 494 wxDefaultPosition, wxDefaultSize
2796cce3 495#if defined(__WXMSW__)
2c9a89e0 496 , wxTE_MULTILINE | wxTE_NO_VSCROLL // necessary ???
2796cce3 497#endif
508011ce 498 );
2796cce3 499
c4608a8a
VZ
500 // TODO: use m_maxChars
501
508011ce 502 wxGridCellEditor::Create(parent, id, evtHandler);
2796cce3
RD
503}
504
189d0213
VZ
505void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
506 wxGridCellAttr * WXUNUSED(attr))
507{
508 // as we fill the entire client area, don't do anything here to minimize
509 // flicker
510}
2796cce3 511
99306db2
VZ
512void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
513{
514 wxRect rect(rectOrig);
515
516 // Make the edit control large enough to allow for internal
517 // margins
518 //
519 // TODO: remove this if the text ctrl sizing is improved esp. for
520 // unix
521 //
522#if defined(__WXGTK__)
b0e282b3
RR
523 if (rect.x != 0)
524 {
525 rect.x += 1;
526 rect.y += 1;
527 rect.width -= 1;
528 rect.height -= 1;
529 }
99306db2 530#else // !GTK
cb105ad4
SN
531 int extra_x = ( rect.x > 2 )? 2 : 1;
532 int extra_y = ( rect.y > 2 )? 2 : 1;
99306db2 533#if defined(__WXMOTIF__)
cb105ad4
SN
534 extra_x *= 2;
535 extra_y *= 2;
99306db2 536#endif
cb105ad4
SN
537 rect.SetLeft( wxMax(0, rect.x - extra_x) );
538 rect.SetTop( wxMax(0, rect.y - extra_y) );
539 rect.SetRight( rect.GetRight() + 2*extra_x );
540 rect.SetBottom( rect.GetBottom() + 2*extra_y );
99306db2
VZ
541#endif // GTK/!GTK
542
543 wxGridCellEditor::SetSize(rect);
544}
545
3da93aae 546void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
2796cce3
RD
547{
548 wxASSERT_MSG(m_control,
549 wxT("The wxGridCellEditor must be Created first!"));
550
551 m_startValue = grid->GetTable()->GetValue(row, col);
816be743
VZ
552
553 DoBeginEdit(m_startValue);
554}
555
556void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
557{
558 Text()->SetValue(startValue);
b54ba671
VZ
559 Text()->SetInsertionPointEnd();
560 Text()->SetFocus();
2796cce3
RD
561}
562
3324d5f5 563bool wxGridCellTextEditor::EndEdit(int row, int col,
3da93aae 564 wxGrid* grid)
2796cce3
RD
565{
566 wxASSERT_MSG(m_control,
567 wxT("The wxGridCellEditor must be Created first!"));
568
569 bool changed = FALSE;
b54ba671 570 wxString value = Text()->GetValue();
2796cce3
RD
571 if (value != m_startValue)
572 changed = TRUE;
573
574 if (changed)
575 grid->GetTable()->SetValue(row, col, value);
2c9a89e0 576
3da93aae 577 m_startValue = wxEmptyString;
b54ba671 578 Text()->SetValue(m_startValue);
2796cce3
RD
579
580 return changed;
581}
582
583
584void wxGridCellTextEditor::Reset()
585{
586 wxASSERT_MSG(m_control,
587 wxT("The wxGridCellEditor must be Created first!"));
588
816be743
VZ
589 DoReset(m_startValue);
590}
591
592void wxGridCellTextEditor::DoReset(const wxString& startValue)
593{
594 Text()->SetValue(startValue);
b54ba671 595 Text()->SetInsertionPointEnd();
2796cce3
RD
596}
597
2c9a89e0
RD
598void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
599{
b54ba671
VZ
600 if ( !event.AltDown() && !event.MetaDown() && !event.ControlDown() )
601 {
602 // insert the key in the control
4a64bee4 603 int keycode = (int)event.KeyCode();
b54ba671
VZ
604 if ( isprint(keycode) )
605 {
606 // FIXME this is not going to work for non letters...
607 if ( !event.ShiftDown() )
608 {
609 keycode = tolower(keycode);
610 }
611
612 Text()->AppendText((wxChar)keycode);
613
614 return;
615 }
2c9a89e0 616
2c9a89e0 617 }
2c9a89e0 618
b54ba671
VZ
619 event.Skip();
620}
2c9a89e0 621
c78b3acd 622void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
0b7e6e7d 623 WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
2796cce3
RD
624{
625#if defined(__WXMOTIF__) || defined(__WXGTK__)
626 // wxMotif needs a little extra help...
6fc0f38f 627 size_t pos = (size_t)( Text()->GetInsertionPoint() );
b54ba671 628 wxString s( Text()->GetValue() );
2796cce3 629 s = s.Left(pos) + "\n" + s.Mid(pos);
b54ba671
VZ
630 Text()->SetValue(s);
631 Text()->SetInsertionPoint( pos );
2796cce3
RD
632#else
633 // the other ports can handle a Return key press
634 //
635 event.Skip();
636#endif
637}
638
c4608a8a
VZ
639void wxGridCellTextEditor::SetParameters(const wxString& params)
640{
641 if ( !params )
642 {
643 // reset to default
644 m_maxChars = 0;
645 }
646 else
647 {
648 long tmp;
649 if ( !params.ToLong(&tmp) )
650 {
651 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string "
652 "'%s' ignored"), params.c_str());
653 }
654 else
655 {
656 m_maxChars = (size_t)tmp;
657 }
658 }
659}
660
816be743
VZ
661// ----------------------------------------------------------------------------
662// wxGridCellNumberEditor
663// ----------------------------------------------------------------------------
664
665wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
666{
667 m_min = min;
668 m_max = max;
669}
670
671void wxGridCellNumberEditor::Create(wxWindow* parent,
672 wxWindowID id,
673 wxEvtHandler* evtHandler)
674{
675 if ( HasRange() )
676 {
677 // create a spin ctrl
678 m_control = new wxSpinCtrl(parent, -1, wxEmptyString,
679 wxDefaultPosition, wxDefaultSize,
680 wxSP_ARROW_KEYS,
681 m_min, m_max);
682
683 wxGridCellEditor::Create(parent, id, evtHandler);
684 }
685 else
686 {
687 // just a text control
688 wxGridCellTextEditor::Create(parent, id, evtHandler);
689
690#if wxUSE_VALIDATORS
85bc0351 691 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
692#endif // wxUSE_VALIDATORS
693 }
694}
695
696void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
697{
698 // first get the value
699 wxGridTableBase *table = grid->GetTable();
700 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
701 {
702 m_valueOld = table->GetValueAsLong(row, col);
703 }
704 else
705 {
a5777624
RD
706 wxString sValue = table->GetValue(row, col);
707 if (! sValue.ToLong(&m_valueOld))
708 {
709 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
710 return;
711 }
816be743
VZ
712 }
713
714 if ( HasRange() )
715 {
4a64bee4 716 Spin()->SetValue((int)m_valueOld);
816be743
VZ
717 }
718 else
719 {
720 DoBeginEdit(GetString());
721 }
722}
723
3324d5f5 724bool wxGridCellNumberEditor::EndEdit(int row, int col,
816be743
VZ
725 wxGrid* grid)
726{
727 bool changed;
728 long value;
729
730 if ( HasRange() )
731 {
732 value = Spin()->GetValue();
733 changed = value != m_valueOld;
734 }
735 else
736 {
737 changed = Text()->GetValue().ToLong(&value) && (value != m_valueOld);
738 }
739
740 if ( changed )
741 {
a5777624
RD
742 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
743 grid->GetTable()->SetValueAsLong(row, col, value);
744 else
745 grid->GetTable()->SetValue(row, col, wxString::Format("%ld", value));
816be743
VZ
746 }
747
748 return changed;
749}
750
751void wxGridCellNumberEditor::Reset()
752{
753 if ( HasRange() )
754 {
4a64bee4 755 Spin()->SetValue((int)m_valueOld);
816be743
VZ
756 }
757 else
758 {
759 DoReset(GetString());
760 }
761}
762
763void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
764{
765 if ( !HasRange() )
766 {
4a64bee4 767 int keycode = (int) event.KeyCode();
816be743
VZ
768 if ( isdigit(keycode) || keycode == '+' || keycode == '-' )
769 {
770 wxGridCellTextEditor::StartingKey(event);
771
772 // skip Skip() below
773 return;
774 }
775 }
776
777 event.Skip();
778}
9c4ba614 779
c4608a8a
VZ
780void wxGridCellNumberEditor::SetParameters(const wxString& params)
781{
782 if ( !params )
783 {
784 // reset to default
785 m_min =
786 m_max = -1;
787 }
788 else
789 {
790 long tmp;
791 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
792 {
793 m_min = (int)tmp;
794
795 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
796 {
797 m_max = (int)tmp;
798
799 // skip the error message below
800 return;
801 }
802 }
803
804 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string "
805 "'%s' ignored"), params.c_str());
806 }
807}
808
816be743
VZ
809// ----------------------------------------------------------------------------
810// wxGridCellFloatEditor
811// ----------------------------------------------------------------------------
812
813void wxGridCellFloatEditor::Create(wxWindow* parent,
814 wxWindowID id,
815 wxEvtHandler* evtHandler)
816{
817 wxGridCellTextEditor::Create(parent, id, evtHandler);
818
819#if wxUSE_VALIDATORS
85bc0351 820 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
821#endif // wxUSE_VALIDATORS
822}
823
824void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
825{
826 // first get the value
827 wxGridTableBase *table = grid->GetTable();
828 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
829 {
830 m_valueOld = table->GetValueAsDouble(row, col);
831 }
832 else
833 {
a5777624
RD
834 wxString sValue = table->GetValue(row, col);
835 if (! sValue.ToDouble(&m_valueOld))
836 {
837 wxFAIL_MSG( _T("this cell doesn't have float value") );
838 return;
839 }
816be743
VZ
840 }
841
842 DoBeginEdit(GetString());
843}
844
3324d5f5 845bool wxGridCellFloatEditor::EndEdit(int row, int col,
816be743
VZ
846 wxGrid* grid)
847{
848 double value;
849 if ( Text()->GetValue().ToDouble(&value) && (value != m_valueOld) )
850 {
a5777624
RD
851 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
852 grid->GetTable()->SetValueAsDouble(row, col, value);
853 else
854 grid->GetTable()->SetValue(row, col, wxString::Format("%f", value));
816be743
VZ
855
856 return TRUE;
857 }
858 else
859 {
860 return FALSE;
861 }
862}
863
864void wxGridCellFloatEditor::Reset()
865{
866 DoReset(GetString());
867}
868
869void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
870{
4a64bee4 871 int keycode = (int)event.KeyCode();
816be743
VZ
872 if ( isdigit(keycode) ||
873 keycode == '+' || keycode == '-' || keycode == '.' )
874 {
875 wxGridCellTextEditor::StartingKey(event);
876
877 // skip Skip() below
878 return;
879 }
880
881 event.Skip();
882}
883
508011ce
VZ
884// ----------------------------------------------------------------------------
885// wxGridCellBoolEditor
886// ----------------------------------------------------------------------------
887
888void wxGridCellBoolEditor::Create(wxWindow* parent,
889 wxWindowID id,
890 wxEvtHandler* evtHandler)
891{
892 m_control = new wxCheckBox(parent, id, wxEmptyString,
893 wxDefaultPosition, wxDefaultSize,
894 wxNO_BORDER);
895
896 wxGridCellEditor::Create(parent, id, evtHandler);
897}
898
899void wxGridCellBoolEditor::SetSize(const wxRect& r)
900{
b94ae1ea
VZ
901 bool resize = FALSE;
902 wxSize size = m_control->GetSize();
903 wxCoord minSize = wxMin(r.width, r.height);
904
905 // check if the checkbox is not too big/small for this cell
906 wxSize sizeBest = m_control->GetBestSize();
907 if ( !(size == sizeBest) )
908 {
909 // reset to default size if it had been made smaller
910 size = sizeBest;
911
912 resize = TRUE;
913 }
914
915 if ( size.x >= minSize || size.y >= minSize )
916 {
917 // leave 1 pixel margin
918 size.x = size.y = minSize - 2;
919
920 resize = TRUE;
921 }
922
923 if ( resize )
924 {
925 m_control->SetSize(size);
926 }
927
508011ce 928 // position it in the centre of the rectangle (TODO: support alignment?)
508011ce 929
b94ae1ea 930#if defined(__WXGTK__) || defined (__WXMOTIF__)
508011ce
VZ
931 // the checkbox without label still has some space to the right in wxGTK,
932 // so shift it to the right
b94ae1ea
VZ
933 size.x -= 8;
934#elif defined(__WXMSW__)
a95e38c0
VZ
935 // here too, but in other way
936 size.x += 1;
b94ae1ea
VZ
937 size.y -= 2;
938#endif
508011ce 939
b94ae1ea 940 m_control->Move(r.x + r.width/2 - size.x/2, r.y + r.height/2 - size.y/2);
508011ce
VZ
941}
942
943void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
944{
99306db2
VZ
945 m_control->Show(show);
946
189d0213 947 if ( show )
508011ce 948 {
189d0213
VZ
949 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
950 CBox()->SetBackgroundColour(colBg);
508011ce 951 }
508011ce
VZ
952}
953
954void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
955{
956 wxASSERT_MSG(m_control,
957 wxT("The wxGridCellEditor must be Created first!"));
958
28a77bc4 959 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
960 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
961 else
962 m_startValue = !!grid->GetTable()->GetValue(row, col);
508011ce
VZ
963 CBox()->SetValue(m_startValue);
964 CBox()->SetFocus();
965}
966
967bool wxGridCellBoolEditor::EndEdit(int row, int col,
508011ce
VZ
968 wxGrid* grid)
969{
970 wxASSERT_MSG(m_control,
971 wxT("The wxGridCellEditor must be Created first!"));
972
973 bool changed = FALSE;
974 bool value = CBox()->GetValue();
975 if ( value != m_startValue )
976 changed = TRUE;
977
978 if ( changed )
979 {
28a77bc4 980 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
981 grid->GetTable()->SetValueAsBool(row, col, value);
982 else
983 grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
508011ce
VZ
984 }
985
986 return changed;
987}
988
989void wxGridCellBoolEditor::Reset()
990{
991 wxASSERT_MSG(m_control,
992 wxT("The wxGridCellEditor must be Created first!"));
993
994 CBox()->SetValue(m_startValue);
995}
996
e195a54c 997void wxGridCellBoolEditor::StartingClick()
508011ce 998{
e195a54c 999 CBox()->SetValue(!CBox()->GetValue());
508011ce
VZ
1000}
1001
4ee5fc9c
VZ
1002// ----------------------------------------------------------------------------
1003// wxGridCellChoiceEditor
1004// ----------------------------------------------------------------------------
1005
1006wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
1007 const wxChar* choices[],
1008 bool allowOthers)
1009 : m_allowOthers(allowOthers)
1010{
c4608a8a 1011 if ( count )
4ee5fc9c 1012 {
c4608a8a
VZ
1013 m_choices.Alloc(count);
1014 for ( size_t n = 0; n < count; n++ )
1015 {
1016 m_choices.Add(choices[n]);
1017 }
4ee5fc9c
VZ
1018 }
1019}
1020
c4608a8a
VZ
1021wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
1022{
1023 wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
1024 editor->m_allowOthers = m_allowOthers;
1025 editor->m_choices = m_choices;
1026
1027 return editor;
1028}
1029
4ee5fc9c
VZ
1030void wxGridCellChoiceEditor::Create(wxWindow* parent,
1031 wxWindowID id,
1032 wxEvtHandler* evtHandler)
1033{
1034 size_t count = m_choices.GetCount();
1035 wxString *choices = new wxString[count];
1036 for ( size_t n = 0; n < count; n++ )
1037 {
1038 choices[n] = m_choices[n];
1039 }
1040
1041 m_control = new wxComboBox(parent, id, wxEmptyString,
1042 wxDefaultPosition, wxDefaultSize,
1043 count, choices,
1044 m_allowOthers ? 0 : wxCB_READONLY);
1045
1046 delete [] choices;
1047
1048 wxGridCellEditor::Create(parent, id, evtHandler);
1049}
1050
a5777624
RD
1051void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
1052 wxGridCellAttr * attr)
4ee5fc9c
VZ
1053{
1054 // as we fill the entire client area, don't do anything here to minimize
1055 // flicker
a5777624
RD
1056
1057 // TODO: It doesn't actually fill the client area since the height of a
1058 // combo always defaults to the standard... Until someone has time to
1059 // figure out the right rectangle to paint, just do it the normal way...
1060 wxGridCellEditor::PaintBackground(rectCell, attr);
4ee5fc9c
VZ
1061}
1062
1063void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
1064{
1065 wxASSERT_MSG(m_control,
1066 wxT("The wxGridCellEditor must be Created first!"));
1067
1068 m_startValue = grid->GetTable()->GetValue(row, col);
1069
1070 Combo()->SetValue(m_startValue);
28a77bc4
RD
1071 size_t count = m_choices.GetCount();
1072 for (size_t i=0; i<count; i++)
1073 {
1074 if (m_startValue == m_choices[i])
1075 {
1076 Combo()->SetSelection(i);
1077 break;
1078 }
1079 }
4ee5fc9c
VZ
1080 Combo()->SetInsertionPointEnd();
1081 Combo()->SetFocus();
1082}
1083
28a77bc4 1084bool wxGridCellChoiceEditor::EndEdit(int row, int col,
4ee5fc9c
VZ
1085 wxGrid* grid)
1086{
1087 wxString value = Combo()->GetValue();
1088 bool changed = value != m_startValue;
1089
1090 if ( changed )
1091 grid->GetTable()->SetValue(row, col, value);
1092
1093 m_startValue = wxEmptyString;
1094 Combo()->SetValue(m_startValue);
1095
1096 return changed;
1097}
1098
1099void wxGridCellChoiceEditor::Reset()
1100{
1101 Combo()->SetValue(m_startValue);
1102 Combo()->SetInsertionPointEnd();
1103}
1104
c4608a8a
VZ
1105void wxGridCellChoiceEditor::SetParameters(const wxString& params)
1106{
1107 if ( !params )
1108 {
1109 // what can we do?
1110 return;
1111 }
1112
1113 m_choices.Empty();
1114
1115 wxStringTokenizer tk(params, _T(','));
1116 while ( tk.HasMoreTokens() )
1117 {
1118 m_choices.Add(tk.GetNextToken());
1119 }
1120}
1121
508011ce
VZ
1122// ----------------------------------------------------------------------------
1123// wxGridCellEditorEvtHandler
1124// ----------------------------------------------------------------------------
2796cce3
RD
1125
1126void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
1127{
1128 switch ( event.KeyCode() )
1129 {
1130 case WXK_ESCAPE:
1131 m_editor->Reset();
b54ba671 1132 m_grid->DisableCellEditControl();
2796cce3
RD
1133 break;
1134
2c9a89e0 1135 case WXK_TAB:
9b4aede2
RD
1136 event.Skip( m_grid->ProcessEvent( event ) );
1137 break;
1138
2796cce3
RD
1139 case WXK_RETURN:
1140 if (!m_grid->ProcessEvent(event))
1141 m_editor->HandleReturn(event);
1142 break;
1143
2796cce3
RD
1144
1145 default:
1146 event.Skip();
1147 }
1148}
1149
fb0de762
RD
1150void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
1151{
1152 switch ( event.KeyCode() )
1153 {
1154 case WXK_ESCAPE:
1155 case WXK_TAB:
1156 case WXK_RETURN:
1157 break;
1158
1159 default:
1160 event.Skip();
1161 }
1162}
1163
c4608a8a
VZ
1164// ----------------------------------------------------------------------------
1165// wxGridCellWorker is an (almost) empty common base class for
1166// wxGridCellRenderer and wxGridCellEditor managing ref counting
1167// ----------------------------------------------------------------------------
1168
1169void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1170{
1171 // nothing to do
1172}
1173
1174wxGridCellWorker::~wxGridCellWorker()
1175{
1176}
1177
508011ce
VZ
1178// ============================================================================
1179// renderer classes
1180// ============================================================================
1181
ab79958a
VZ
1182// ----------------------------------------------------------------------------
1183// wxGridCellRenderer
1184// ----------------------------------------------------------------------------
1185
1186void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 1187 wxGridCellAttr& attr,
ab79958a
VZ
1188 wxDC& dc,
1189 const wxRect& rect,
c78b3acd 1190 int WXUNUSED(row), int WXUNUSED(col),
ab79958a
VZ
1191 bool isSelected)
1192{
1193 dc.SetBackgroundMode( wxSOLID );
1194
1195 if ( isSelected )
1196 {
2796cce3 1197 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
ab79958a
VZ
1198 }
1199 else
1200 {
2796cce3 1201 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
ab79958a
VZ
1202 }
1203
1204 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
1205 dc.DrawRectangle(rect);
1206}
1207
508011ce
VZ
1208// ----------------------------------------------------------------------------
1209// wxGridCellStringRenderer
1210// ----------------------------------------------------------------------------
1211
816be743
VZ
1212void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
1213 wxGridCellAttr& attr,
1214 wxDC& dc,
1215 bool isSelected)
ab79958a 1216{
ab79958a
VZ
1217 dc.SetBackgroundMode( wxTRANSPARENT );
1218
283b7808
VZ
1219 // TODO some special colours for attr.IsReadOnly() case?
1220
ab79958a
VZ
1221 if ( isSelected )
1222 {
2796cce3
RD
1223 dc.SetTextBackground( grid.GetSelectionBackground() );
1224 dc.SetTextForeground( grid.GetSelectionForeground() );
ab79958a
VZ
1225 }
1226 else
1227 {
2796cce3
RD
1228 dc.SetTextBackground( attr.GetBackgroundColour() );
1229 dc.SetTextForeground( attr.GetTextColour() );
ab79958a 1230 }
816be743 1231
2796cce3 1232 dc.SetFont( attr.GetFont() );
816be743
VZ
1233}
1234
65e4e78e
VZ
1235wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
1236 wxDC& dc,
1237 const wxString& text)
1238{
1239 wxCoord x, y;
1240 dc.SetFont(attr.GetFont());
1241 dc.GetTextExtent(text, &x, &y);
1242
1243 return wxSize(x, y);
1244}
1245
1246wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1247 wxGridCellAttr& attr,
1248 wxDC& dc,
1249 int row, int col)
1250{
1251 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1252}
1253
816be743
VZ
1254void wxGridCellStringRenderer::Draw(wxGrid& grid,
1255 wxGridCellAttr& attr,
1256 wxDC& dc,
1257 const wxRect& rectCell,
1258 int row, int col,
1259 bool isSelected)
1260{
1261 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1262
1263 // now we only have to draw the text
1264 SetTextColoursAndFont(grid, attr, dc, isSelected);
ab79958a
VZ
1265
1266 int hAlign, vAlign;
2796cce3 1267 attr.GetAlignment(&hAlign, &vAlign);
ab79958a
VZ
1268
1269 wxRect rect = rectCell;
816be743 1270 rect.Inflate(-1);
ab79958a
VZ
1271
1272 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1273 rect, hAlign, vAlign);
1274}
1275
65e4e78e
VZ
1276// ----------------------------------------------------------------------------
1277// wxGridCellNumberRenderer
1278// ----------------------------------------------------------------------------
1279
1280wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
1281{
1282 wxGridTableBase *table = grid.GetTable();
1283 wxString text;
1284 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1285 {
1286 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1287 }
9c4ba614
VZ
1288 else
1289 {
1290 text = table->GetValue(row, col);
1291 }
65e4e78e
VZ
1292
1293 return text;
1294}
1295
816be743
VZ
1296void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1297 wxGridCellAttr& attr,
1298 wxDC& dc,
1299 const wxRect& rectCell,
1300 int row, int col,
1301 bool isSelected)
1302{
1303 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1304
1305 SetTextColoursAndFont(grid, attr, dc, isSelected);
1306
1307 // draw the text right aligned by default
1308 int hAlign, vAlign;
1309 attr.GetAlignment(&hAlign, &vAlign);
1310 hAlign = wxRIGHT;
1311
1312 wxRect rect = rectCell;
1313 rect.Inflate(-1);
1314
65e4e78e
VZ
1315 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1316}
816be743 1317
65e4e78e
VZ
1318wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1319 wxGridCellAttr& attr,
1320 wxDC& dc,
1321 int row, int col)
1322{
1323 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1324}
1325
1326// ----------------------------------------------------------------------------
1327// wxGridCellFloatRenderer
1328// ----------------------------------------------------------------------------
1329
1330wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1331{
1332 SetWidth(width);
1333 SetPrecision(precision);
1334}
1335
e72b4213
VZ
1336wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1337{
1338 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1339 renderer->m_width = m_width;
1340 renderer->m_precision = m_precision;
1341 renderer->m_format = m_format;
1342
1343 return renderer;
1344}
1345
65e4e78e
VZ
1346wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
1347{
1348 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
1349
1350 bool hasDouble;
1351 double val;
65e4e78e
VZ
1352 wxString text;
1353 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1354 {
0b190b0f
VZ
1355 val = table->GetValueAsDouble(row, col);
1356 hasDouble = TRUE;
65e4e78e 1357 }
9c4ba614
VZ
1358 else
1359 {
1360 text = table->GetValue(row, col);
0b190b0f 1361 hasDouble = text.ToDouble(&val);
9c4ba614 1362 }
65e4e78e 1363
0b190b0f
VZ
1364 if ( hasDouble )
1365 {
1366 if ( !m_format )
1367 {
1368 if ( m_width == -1 )
1369 {
1370 // default width/precision
1371 m_format = _T("%f");
1372 }
1373 else if ( m_precision == -1 )
1374 {
1375 // default precision
1376 m_format.Printf(_T("%%%d.f"), m_width);
1377 }
1378 else
1379 {
1380 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
1381 }
1382 }
1383
1384 text.Printf(m_format, val);
1385 }
1386 //else: text already contains the string
1387
65e4e78e
VZ
1388 return text;
1389}
1390
816be743
VZ
1391void wxGridCellFloatRenderer::Draw(wxGrid& grid,
1392 wxGridCellAttr& attr,
1393 wxDC& dc,
1394 const wxRect& rectCell,
1395 int row, int col,
1396 bool isSelected)
1397{
1398 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1399
1400 SetTextColoursAndFont(grid, attr, dc, isSelected);
1401
1402 // draw the text right aligned by default
1403 int hAlign, vAlign;
1404 attr.GetAlignment(&hAlign, &vAlign);
1405 hAlign = wxRIGHT;
1406
1407 wxRect rect = rectCell;
1408 rect.Inflate(-1);
1409
65e4e78e
VZ
1410 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1411}
816be743 1412
65e4e78e
VZ
1413wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
1414 wxGridCellAttr& attr,
1415 wxDC& dc,
1416 int row, int col)
1417{
1418 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1419}
1420
0b190b0f
VZ
1421void wxGridCellFloatRenderer::SetParameters(const wxString& params)
1422{
1423 bool ok = TRUE;
1424
1425 if ( !params )
1426 {
1427 // reset to defaults
1428 SetWidth(-1);
1429 SetPrecision(-1);
1430 }
1431 else
1432 {
1433 wxString tmp = params.BeforeFirst(_T(','));
1434 if ( !!tmp )
1435 {
1436 long width;
1437 if ( !tmp.ToLong(&width) )
1438 {
1439 ok = FALSE;
1440 }
1441 else
1442 {
1443 SetWidth((int)width);
1444
1445 tmp = params.AfterFirst(_T(','));
1446 if ( !!tmp )
1447 {
1448 long precision;
1449 if ( !tmp.ToLong(&precision) )
1450 {
1451 ok = FALSE;
1452 }
1453 else
1454 {
1455 SetPrecision((int)precision);
1456 }
1457 }
1458 }
1459 }
1460
1461 if ( !ok )
1462 {
1463 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
1464 "'%s ignored"), params.c_str());
1465 }
1466 }
1467}
1468
508011ce
VZ
1469// ----------------------------------------------------------------------------
1470// wxGridCellBoolRenderer
1471// ----------------------------------------------------------------------------
1472
65e4e78e 1473wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 1474
b94ae1ea
VZ
1475// FIXME these checkbox size calculations are really ugly...
1476
65e4e78e 1477// between checkmark and box
a95e38c0 1478static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 1479
65e4e78e
VZ
1480wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
1481 wxGridCellAttr& WXUNUSED(attr),
1482 wxDC& WXUNUSED(dc),
1483 int WXUNUSED(row),
1484 int WXUNUSED(col))
1485{
1486 // compute it only once (no locks for MT safeness in GUI thread...)
1487 if ( !ms_sizeCheckMark.x )
297da4ba 1488 {
65e4e78e
VZ
1489 // get checkbox size
1490 wxCoord checkSize = 0;
297da4ba
VZ
1491 wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString);
1492 wxSize size = checkbox->GetBestSize();
a95e38c0 1493 checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
297da4ba 1494
65e4e78e 1495 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
69d8f612 1496#if defined(__WXGTK__) || defined(__WXMOTIF__)
65e4e78e 1497 checkSize -= size.y / 2;
297da4ba
VZ
1498#endif
1499
1500 delete checkbox;
65e4e78e
VZ
1501
1502 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
1503 }
1504
65e4e78e
VZ
1505 return ms_sizeCheckMark;
1506}
1507
1508void wxGridCellBoolRenderer::Draw(wxGrid& grid,
1509 wxGridCellAttr& attr,
1510 wxDC& dc,
1511 const wxRect& rect,
1512 int row, int col,
1513 bool isSelected)
1514{
1515 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1516
297da4ba 1517 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 1518 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
1519
1520 // don't draw outside the cell
1521 wxCoord minSize = wxMin(rect.width, rect.height);
1522 if ( size.x >= minSize || size.y >= minSize )
1523 {
1524 // and even leave (at least) 1 pixel margin
1525 size.x = size.y = minSize - 2;
1526 }
1527
1528 // draw a border around checkmark
a95e38c0
VZ
1529 wxRect rectBorder;
1530 rectBorder.x = rect.x + rect.width/2 - size.x/2;
1531 rectBorder.y = rect.y + rect.height/2 - size.y/2;
1532 rectBorder.width = size.x;
1533 rectBorder.height = size.y;
b94ae1ea 1534
f2d76237 1535 bool value;
b94ae1ea 1536 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
f2d76237
RD
1537 value = grid.GetTable()->GetValueAsBool(row, col);
1538 else
1539 value = !!grid.GetTable()->GetValue(row, col);
1540
1541 if ( value )
508011ce 1542 {
a95e38c0
VZ
1543 wxRect rectMark = rectBorder;
1544#ifdef __WXMSW__
1545 // MSW DrawCheckMark() is weird (and should probably be changed...)
1546 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
1547 rectMark.x++;
1548 rectMark.y++;
1549#else // !MSW
1550 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
1551#endif // MSW/!MSW
1552
508011ce
VZ
1553 dc.SetTextForeground(attr.GetTextColour());
1554 dc.DrawCheckMark(rectMark);
1555 }
a95e38c0
VZ
1556
1557 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1558 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
1559 dc.DrawRectangle(rectBorder);
508011ce
VZ
1560}
1561
2796cce3
RD
1562// ----------------------------------------------------------------------------
1563// wxGridCellAttr
1564// ----------------------------------------------------------------------------
1565
39bcce60 1566wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246
VZ
1567{
1568 wxGridCellAttr *attr = new wxGridCellAttr;
1569 if ( HasTextColour() )
1570 attr->SetTextColour(GetTextColour());
1571 if ( HasBackgroundColour() )
1572 attr->SetBackgroundColour(GetBackgroundColour());
1573 if ( HasFont() )
1574 attr->SetFont(GetFont());
1575 if ( HasAlignment() )
1576 attr->SetAlignment(m_hAlign, m_vAlign);
1577
1578 if ( m_renderer )
1579 {
1580 attr->SetRenderer(m_renderer);
39bcce60 1581 m_renderer->IncRef();
a68c1246
VZ
1582 }
1583 if ( m_editor )
1584 {
1585 attr->SetEditor(m_editor);
39bcce60 1586 m_editor->IncRef();
a68c1246
VZ
1587 }
1588
1589 if ( IsReadOnly() )
1590 attr->SetReadOnly();
1591
1592 attr->SetDefAttr(m_defGridAttr);
1593
1594 return attr;
1595}
1596
2796cce3
RD
1597const wxColour& wxGridCellAttr::GetTextColour() const
1598{
1599 if (HasTextColour())
508011ce 1600 {
2796cce3 1601 return m_colText;
508011ce 1602 }
2796cce3 1603 else if (m_defGridAttr != this)
508011ce 1604 {
2796cce3 1605 return m_defGridAttr->GetTextColour();
508011ce
VZ
1606 }
1607 else
1608 {
2796cce3
RD
1609 wxFAIL_MSG(wxT("Missing default cell attribute"));
1610 return wxNullColour;
1611 }
1612}
1613
1614
1615const wxColour& wxGridCellAttr::GetBackgroundColour() const
1616{
1617 if (HasBackgroundColour())
1618 return m_colBack;
1619 else if (m_defGridAttr != this)
1620 return m_defGridAttr->GetBackgroundColour();
508011ce
VZ
1621 else
1622 {
2796cce3
RD
1623 wxFAIL_MSG(wxT("Missing default cell attribute"));
1624 return wxNullColour;
1625 }
1626}
1627
1628
1629const wxFont& wxGridCellAttr::GetFont() const
1630{
1631 if (HasFont())
1632 return m_font;
1633 else if (m_defGridAttr != this)
1634 return m_defGridAttr->GetFont();
508011ce
VZ
1635 else
1636 {
2796cce3
RD
1637 wxFAIL_MSG(wxT("Missing default cell attribute"));
1638 return wxNullFont;
1639 }
1640}
1641
1642
1643void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
1644{
508011ce
VZ
1645 if (HasAlignment())
1646 {
2796cce3
RD
1647 if ( hAlign ) *hAlign = m_hAlign;
1648 if ( vAlign ) *vAlign = m_vAlign;
1649 }
1650 else if (m_defGridAttr != this)
1651 m_defGridAttr->GetAlignment(hAlign, vAlign);
508011ce
VZ
1652 else
1653 {
2796cce3
RD
1654 wxFAIL_MSG(wxT("Missing default cell attribute"));
1655 }
1656}
1657
1658
f2d76237 1659// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
1660// which attribute to use. If a non-default attr object has one then it is
1661// used, otherwise the default editor or renderer is fetched from the grid and
1662// used. It should be the default for the data type of the cell. If it is
1663// NULL (because the table has a type that the grid does not have in its
1664// registry,) then the grid's default editor or renderer is used.
1665
1666wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
1667{
28a77bc4 1668 wxGridCellRenderer* renderer = NULL;
28a77bc4 1669
0b190b0f
VZ
1670 if ( m_defGridAttr != this || grid == NULL )
1671 {
1672 renderer = m_renderer; // use local attribute
1673 if ( renderer )
1674 renderer->IncRef();
1675 }
1676
1677 if ( !renderer && grid ) // get renderer for the data type
1678 {
1679 // GetDefaultRendererForCell() will do IncRef() for us
1680 renderer = grid->GetDefaultRendererForCell(row, col);
1681 }
1682
1683 if ( !renderer )
1684 {
28a77bc4 1685 // if we still don't have one then use the grid default
0b190b0f 1686 // (no need for IncRef() here neither)
28a77bc4 1687 renderer = m_defGridAttr->GetRenderer(NULL,0,0);
0b190b0f 1688 }
28a77bc4 1689
0b190b0f
VZ
1690 if ( !renderer)
1691 {
2796cce3 1692 wxFAIL_MSG(wxT("Missing default cell attribute"));
0b190b0f 1693 }
28a77bc4
RD
1694
1695 return renderer;
2796cce3
RD
1696}
1697
28a77bc4 1698wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
07296f0b 1699{
28a77bc4 1700 wxGridCellEditor* editor = NULL;
0b190b0f
VZ
1701
1702 if ( m_defGridAttr != this || grid == NULL )
1703 {
1704 editor = m_editor; // use local attribute
1705 if ( editor )
1706 editor->IncRef();
1707 }
1708
a95e38c0
VZ
1709 if ( !editor && grid ) // get renderer for the data type
1710 editor = grid->GetDefaultEditorForCell(row, col);
28a77bc4 1711
0b190b0f 1712 if ( !editor )
28a77bc4
RD
1713 // if we still don't have one then use the grid default
1714 editor = m_defGridAttr->GetEditor(NULL,0,0);
1715
0b190b0f
VZ
1716 if ( !editor )
1717 {
07296f0b 1718 wxFAIL_MSG(wxT("Missing default cell attribute"));
0b190b0f 1719 }
28a77bc4
RD
1720
1721 return editor;
07296f0b
RD
1722}
1723
b99be8fb 1724// ----------------------------------------------------------------------------
758cbedf 1725// wxGridCellAttrData
b99be8fb
VZ
1726// ----------------------------------------------------------------------------
1727
758cbedf 1728void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb
VZ
1729{
1730 int n = FindIndex(row, col);
1731 if ( n == wxNOT_FOUND )
1732 {
1733 // add the attribute
1734 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
1735 }
1736 else
1737 {
1738 if ( attr )
1739 {
1740 // change the attribute
2e9a6788 1741 m_attrs[(size_t)n].attr = attr;
b99be8fb
VZ
1742 }
1743 else
1744 {
1745 // remove this attribute
1746 m_attrs.RemoveAt((size_t)n);
1747 }
1748 }
b99be8fb
VZ
1749}
1750
758cbedf 1751wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb
VZ
1752{
1753 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1754
1755 int n = FindIndex(row, col);
1756 if ( n != wxNOT_FOUND )
1757 {
2e9a6788
VZ
1758 attr = m_attrs[(size_t)n].attr;
1759 attr->IncRef();
b99be8fb
VZ
1760 }
1761
1762 return attr;
1763}
1764
4d60017a
SN
1765void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
1766{
1767 size_t count = m_attrs.GetCount();
1768 for ( size_t n = 0; n < count; n++ )
1769 {
1770 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
1771 wxCoord row = coords.GetRow();
1772 if ((size_t)row >= pos)
1773 {
1774 if (numRows > 0)
1775 {
1776 // If rows inserted, include row counter where necessary
1777 coords.SetRow(row + numRows);
1778 }
1779 else if (numRows < 0)
1780 {
1781 // If rows deleted ...
1782 if ((size_t)row >= pos - numRows)
1783 {
1784 // ...either decrement row counter (if row still exists)...
1785 coords.SetRow(row + numRows);
1786 }
1787 else
1788 {
1789 // ...or remove the attribute
1790 m_attrs.RemoveAt((size_t)n);
1791 n--; count--;
1792 }
1793 }
4d60017a
SN
1794 }
1795 }
1796}
1797
1798void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
1799{
1800 size_t count = m_attrs.GetCount();
1801 for ( size_t n = 0; n < count; n++ )
1802 {
1803 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
1804 wxCoord col = coords.GetCol();
1805 if ( (size_t)col >= pos )
1806 {
1807 if ( numCols > 0 )
1808 {
1809 // If rows inserted, include row counter where necessary
1810 coords.SetCol(col + numCols);
1811 }
1812 else if (numCols < 0)
1813 {
1814 // If rows deleted ...
1815 if ((size_t)col >= pos - numCols)
1816 {
1817 // ...either decrement row counter (if row still exists)...
1818 coords.SetCol(col + numCols);
1819 }
1820 else
1821 {
1822 // ...or remove the attribute
1823 m_attrs.RemoveAt((size_t)n);
1824 n--; count--;
1825 }
1826 }
4d60017a
SN
1827 }
1828 }
1829}
1830
758cbedf 1831int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
1832{
1833 size_t count = m_attrs.GetCount();
1834 for ( size_t n = 0; n < count; n++ )
1835 {
1836 const wxGridCellCoords& coords = m_attrs[n].coords;
1837 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
1838 {
1839 return n;
1840 }
1841 }
1842
1843 return wxNOT_FOUND;
1844}
1845
758cbedf
VZ
1846// ----------------------------------------------------------------------------
1847// wxGridRowOrColAttrData
1848// ----------------------------------------------------------------------------
1849
1850wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1851{
1852 size_t count = m_attrs.Count();
1853 for ( size_t n = 0; n < count; n++ )
1854 {
1855 m_attrs[n]->DecRef();
1856 }
1857}
1858
1859wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
1860{
1861 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1862
1863 int n = m_rowsOrCols.Index(rowOrCol);
1864 if ( n != wxNOT_FOUND )
1865 {
1866 attr = m_attrs[(size_t)n];
1867 attr->IncRef();
1868 }
1869
1870 return attr;
1871}
1872
1873void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
1874{
a95e38c0
VZ
1875 int i = m_rowsOrCols.Index(rowOrCol);
1876 if ( i == wxNOT_FOUND )
758cbedf
VZ
1877 {
1878 // add the attribute
1879 m_rowsOrCols.Add(rowOrCol);
1880 m_attrs.Add(attr);
1881 }
1882 else
1883 {
a95e38c0 1884 size_t n = (size_t)i;
758cbedf
VZ
1885 if ( attr )
1886 {
1887 // change the attribute
a95e38c0
VZ
1888 m_attrs[n]->DecRef();
1889 m_attrs[n] = attr;
758cbedf
VZ
1890 }
1891 else
1892 {
1893 // remove this attribute
a95e38c0
VZ
1894 m_attrs[n]->DecRef();
1895 m_rowsOrCols.RemoveAt(n);
1896 m_attrs.RemoveAt(n);
758cbedf
VZ
1897 }
1898 }
1899}
1900
4d60017a
SN
1901void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
1902{
1903 size_t count = m_attrs.GetCount();
1904 for ( size_t n = 0; n < count; n++ )
1905 {
1906 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
1907 if ( (size_t)rowOrCol >= pos )
1908 {
1909 if ( numRowsOrCols > 0 )
1910 {
1911 // If rows inserted, include row counter where necessary
1912 rowOrCol += numRowsOrCols;
1913 }
1914 else if ( numRowsOrCols < 0)
1915 {
1916 // If rows deleted, either decrement row counter (if row still exists)
1917 if ((size_t)rowOrCol >= pos - numRowsOrCols)
1918 rowOrCol += numRowsOrCols;
1919 else
1920 {
1921 m_rowsOrCols.RemoveAt((size_t)n);
1922 m_attrs.RemoveAt((size_t)n);
1923 n--; count--;
1924 }
1925 }
4d60017a
SN
1926 }
1927 }
1928}
1929
b99be8fb
VZ
1930// ----------------------------------------------------------------------------
1931// wxGridCellAttrProvider
1932// ----------------------------------------------------------------------------
1933
1934wxGridCellAttrProvider::wxGridCellAttrProvider()
1935{
1936 m_data = (wxGridCellAttrProviderData *)NULL;
1937}
1938
1939wxGridCellAttrProvider::~wxGridCellAttrProvider()
1940{
1941 delete m_data;
1942}
1943
1944void wxGridCellAttrProvider::InitData()
1945{
1946 m_data = new wxGridCellAttrProviderData;
1947}
1948
1949wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) const
1950{
758cbedf
VZ
1951 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1952 if ( m_data )
1953 {
1954 // first look for the attribute of this specific cell
1955 attr = m_data->m_cellAttrs.GetAttr(row, col);
1956
1957 if ( !attr )
1958 {
1959 // then look for the col attr (col attributes are more common than
1960 // the row ones, hence they have priority)
1961 attr = m_data->m_colAttrs.GetAttr(col);
1962 }
1963
1964 if ( !attr )
1965 {
1966 // finally try the row attributes
1967 attr = m_data->m_rowAttrs.GetAttr(row);
1968 }
1969 }
1970
1971 return attr;
b99be8fb
VZ
1972}
1973
2e9a6788 1974void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
1975 int row, int col)
1976{
1977 if ( !m_data )
1978 InitData();
1979
758cbedf
VZ
1980 m_data->m_cellAttrs.SetAttr(attr, row, col);
1981}
1982
1983void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
1984{
1985 if ( !m_data )
1986 InitData();
1987
1988 m_data->m_rowAttrs.SetAttr(attr, row);
1989}
1990
1991void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
1992{
1993 if ( !m_data )
1994 InitData();
1995
1996 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
1997}
1998
4d60017a
SN
1999void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
2000{
2001 if ( m_data )
2002 {
2003 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2004
d1c0b4f9 2005 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
2006 }
2007}
2008
2009void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
2010{
2011 if ( m_data )
2012 {
2013 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2014
d1c0b4f9 2015 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
2016 }
2017}
2018
f2d76237
RD
2019// ----------------------------------------------------------------------------
2020// wxGridTypeRegistry
2021// ----------------------------------------------------------------------------
2022
2023wxGridTypeRegistry::~wxGridTypeRegistry()
2024{
b94ae1ea
VZ
2025 size_t count = m_typeinfo.Count();
2026 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
2027 delete m_typeinfo[i];
2028}
2029
2030
2031void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2032 wxGridCellRenderer* renderer,
2033 wxGridCellEditor* editor)
2034{
f2d76237
RD
2035 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2036
2037 // is it already registered?
c4608a8a 2038 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
2039 if ( loc != wxNOT_FOUND )
2040 {
f2d76237
RD
2041 delete m_typeinfo[loc];
2042 m_typeinfo[loc] = info;
2043 }
39bcce60
VZ
2044 else
2045 {
f2d76237
RD
2046 m_typeinfo.Add(info);
2047 }
2048}
2049
c4608a8a
VZ
2050int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2051{
2052 size_t count = m_typeinfo.GetCount();
2053 for ( size_t i = 0; i < count; i++ )
2054 {
2055 if ( typeName == m_typeinfo[i]->m_typeName )
2056 {
2057 return i;
2058 }
2059 }
2060
2061 return wxNOT_FOUND;
2062}
2063
f2d76237
RD
2064int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2065{
c4608a8a
VZ
2066 int index = FindRegisteredDataType(typeName);
2067 if ( index == wxNOT_FOUND )
2068 {
2069 // check whether this is one of the standard ones, in which case
2070 // register it "on the fly"
2071 if ( typeName == wxGRID_VALUE_STRING )
2072 {
2073 RegisterDataType(wxGRID_VALUE_STRING,
2074 new wxGridCellStringRenderer,
2075 new wxGridCellTextEditor);
2076 }
2077 else if ( typeName == wxGRID_VALUE_BOOL )
2078 {
2079 RegisterDataType(wxGRID_VALUE_BOOL,
2080 new wxGridCellBoolRenderer,
2081 new wxGridCellBoolEditor);
2082 }
2083 else if ( typeName == wxGRID_VALUE_NUMBER )
2084 {
2085 RegisterDataType(wxGRID_VALUE_NUMBER,
2086 new wxGridCellNumberRenderer,
2087 new wxGridCellNumberEditor);
2088 }
2089 else if ( typeName == wxGRID_VALUE_FLOAT )
2090 {
2091 RegisterDataType(wxGRID_VALUE_FLOAT,
2092 new wxGridCellFloatRenderer,
2093 new wxGridCellFloatEditor);
2094 }
2095 else if ( typeName == wxGRID_VALUE_CHOICE )
2096 {
2097 RegisterDataType(wxGRID_VALUE_CHOICE,
2098 new wxGridCellStringRenderer,
2099 new wxGridCellChoiceEditor);
2100 }
2101 else
2102 {
2103 return wxNOT_FOUND;
2104 }
f2d76237 2105
c4608a8a
VZ
2106 // we get here only if just added the entry for this type, so return
2107 // the last index
2108 index = m_typeinfo.GetCount() - 1;
2109 }
2110
2111 return index;
2112}
2113
2114int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
2115{
2116 int index = FindDataType(typeName);
2117 if ( index == wxNOT_FOUND )
2118 {
2119 // the first part of the typename is the "real" type, anything after ':'
2120 // are the parameters for the renderer
2121 index = FindDataType(typeName.BeforeFirst(_T(':')));
2122 if ( index == wxNOT_FOUND )
2123 {
2124 return wxNOT_FOUND;
f2d76237 2125 }
c4608a8a
VZ
2126
2127 wxGridCellRenderer *renderer = GetRenderer(index);
2128 wxGridCellRenderer *rendererOld = renderer;
2129 renderer = renderer->Clone();
2130 rendererOld->DecRef();
2131
2132 wxGridCellEditor *editor = GetEditor(index);
2133 wxGridCellEditor *editorOld = editor;
2134 editor = editor->Clone();
2135 editorOld->DecRef();
2136
2137 // do it even if there are no parameters to reset them to defaults
2138 wxString params = typeName.AfterFirst(_T(':'));
2139 renderer->SetParameters(params);
2140 editor->SetParameters(params);
2141
2142 // register the new typename
c4608a8a
VZ
2143 RegisterDataType(typeName, renderer, editor);
2144
2145 // we just registered it, it's the last one
2146 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
2147 }
2148
c4608a8a 2149 return index;
f2d76237
RD
2150}
2151
2152wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
2153{
2154 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
0b190b0f 2155 renderer->IncRef();
f2d76237
RD
2156 return renderer;
2157}
2158
0b190b0f 2159wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
2160{
2161 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
0b190b0f 2162 editor->IncRef();
f2d76237
RD
2163 return editor;
2164}
2165
758cbedf
VZ
2166// ----------------------------------------------------------------------------
2167// wxGridTableBase
2168// ----------------------------------------------------------------------------
2169
f85afd4e
MB
2170IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
2171
2172
2173wxGridTableBase::wxGridTableBase()
f85afd4e
MB
2174{
2175 m_view = (wxGrid *) NULL;
b99be8fb 2176 m_attrProvider = (wxGridCellAttrProvider *) NULL;
f85afd4e
MB
2177}
2178
2179wxGridTableBase::~wxGridTableBase()
2180{
b99be8fb
VZ
2181 delete m_attrProvider;
2182}
2183
2184void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
2185{
2186 delete m_attrProvider;
2187 m_attrProvider = attrProvider;
f85afd4e
MB
2188}
2189
f2d76237
RD
2190bool wxGridTableBase::CanHaveAttributes()
2191{
2192 if ( ! GetAttrProvider() )
2193 {
2194 // use the default attr provider by default
2195 SetAttrProvider(new wxGridCellAttrProvider);
2196 }
2197 return TRUE;
2198}
2199
b99be8fb
VZ
2200wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col)
2201{
2202 if ( m_attrProvider )
2203 return m_attrProvider->GetAttr(row, col);
2204 else
2205 return (wxGridCellAttr *)NULL;
2206}
2207
758cbedf 2208void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
2209{
2210 if ( m_attrProvider )
2211 {
2212 m_attrProvider->SetAttr(attr, row, col);
2213 }
2214 else
2215 {
2216 // as we take ownership of the pointer and don't store it, we must
2217 // free it now
39bcce60 2218 wxSafeDecRef(attr);
b99be8fb
VZ
2219 }
2220}
2221
758cbedf
VZ
2222void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
2223{
2224 if ( m_attrProvider )
2225 {
2226 m_attrProvider->SetRowAttr(attr, row);
2227 }
2228 else
2229 {
2230 // as we take ownership of the pointer and don't store it, we must
2231 // free it now
39bcce60 2232 wxSafeDecRef(attr);
758cbedf
VZ
2233 }
2234}
2235
2236void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
2237{
2238 if ( m_attrProvider )
2239 {
2240 m_attrProvider->SetColAttr(attr, col);
2241 }
2242 else
2243 {
2244 // as we take ownership of the pointer and don't store it, we must
2245 // free it now
39bcce60 2246 wxSafeDecRef(attr);
758cbedf
VZ
2247 }
2248}
2249
4d60017a
SN
2250void wxGridTableBase::UpdateAttrRows( size_t pos, int numRows )
2251{
2252 if ( m_attrProvider )
2253 {
2254 m_attrProvider->UpdateAttrRows( pos, numRows );
2255 }
2256}
2257
2258void wxGridTableBase::UpdateAttrCols( size_t pos, int numCols )
2259{
2260 if ( m_attrProvider )
2261 {
2262 m_attrProvider->UpdateAttrCols( pos, numCols );
2263 }
2264}
2265
aa5e1f75
SN
2266bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
2267 size_t WXUNUSED(numRows) )
f85afd4e 2268{
bd3c6758
MB
2269 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2270 "but your derived table class does not override this function") );
8f177c8e 2271
f85afd4e
MB
2272 return FALSE;
2273}
2274
aa5e1f75 2275bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 2276{
bd3c6758
MB
2277 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2278 "but your derived table class does not override this function"));
8f177c8e 2279
f85afd4e
MB
2280 return FALSE;
2281}
2282
aa5e1f75
SN
2283bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
2284 size_t WXUNUSED(numRows) )
f85afd4e 2285{
bd3c6758
MB
2286 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2287 "but your derived table class does not override this function"));
8f177c8e 2288
f85afd4e
MB
2289 return FALSE;
2290}
2291
aa5e1f75
SN
2292bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
2293 size_t WXUNUSED(numCols) )
f85afd4e 2294{
bd3c6758
MB
2295 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2296 "but your derived table class does not override this function"));
8f177c8e 2297
f85afd4e
MB
2298 return FALSE;
2299}
2300
aa5e1f75 2301bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 2302{
bd3c6758
MB
2303 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2304 "but your derived table class does not override this function"));
8f177c8e 2305
f85afd4e
MB
2306 return FALSE;
2307}
2308
aa5e1f75
SN
2309bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
2310 size_t WXUNUSED(numCols) )
f85afd4e 2311{
bd3c6758
MB
2312 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2313 "but your derived table class does not override this function"));
8f177c8e 2314
f85afd4e
MB
2315 return FALSE;
2316}
2317
2318
2319wxString wxGridTableBase::GetRowLabelValue( int row )
2320{
2321 wxString s;
f2d76237
RD
2322 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
2323 // how much it makes sense to us geeks.
f85afd4e
MB
2324 return s;
2325}
2326
2327wxString wxGridTableBase::GetColLabelValue( int col )
2328{
2329 // default col labels are:
2330 // cols 0 to 25 : A-Z
2331 // cols 26 to 675 : AA-ZZ
2332 // etc.
2333
2334 wxString s;
2335 unsigned int i, n;
2336 for ( n = 1; ; n++ )
2337 {
f0102d2a 2338 s += (_T('A') + (wxChar)( col%26 ));
f85afd4e
MB
2339 col = col/26 - 1;
2340 if ( col < 0 ) break;
2341 }
2342
2343 // reverse the string...
2344 wxString s2;
2345 for ( i = 0; i < n; i++ )
2346 {
2347 s2 += s[n-i-1];
2348 }
2349
2350 return s2;
2351}
2352
2353
f2d76237
RD
2354wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
2355{
816be743 2356 return wxGRID_VALUE_STRING;
f2d76237
RD
2357}
2358
2359bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
2360 const wxString& typeName )
2361{
816be743 2362 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
2363}
2364
2365bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
2366{
2367 return CanGetValueAs(row, col, typeName);
2368}
2369
2370long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
2371{
2372 return 0;
2373}
2374
2375double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
2376{
2377 return 0.0;
2378}
2379
2380bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
2381{
2382 return FALSE;
2383}
2384
2385void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
2386 long WXUNUSED(value) )
2387{
2388}
2389
2390void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
2391 double WXUNUSED(value) )
2392{
2393}
2394
2395void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
2396 bool WXUNUSED(value) )
2397{
2398}
2399
2400
2401void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2402 const wxString& WXUNUSED(typeName) )
2403{
2404 return NULL;
2405}
2406
2407void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2408 const wxString& WXUNUSED(typeName),
2409 void* WXUNUSED(value) )
2410{
2411}
2412
f85afd4e
MB
2413//////////////////////////////////////////////////////////////////////
2414//
2415// Message class for the grid table to send requests and notifications
2416// to the grid view
2417//
2418
2419wxGridTableMessage::wxGridTableMessage()
2420{
2421 m_table = (wxGridTableBase *) NULL;
2422 m_id = -1;
2423 m_comInt1 = -1;
2424 m_comInt2 = -1;
2425}
2426
2427wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
2428 int commandInt1, int commandInt2 )
2429{
2430 m_table = table;
2431 m_id = id;
2432 m_comInt1 = commandInt1;
2433 m_comInt2 = commandInt2;
2434}
2435
2436
2437
2438//////////////////////////////////////////////////////////////////////
2439//
2440// A basic grid table for string data. An object of this class will
2441// created by wxGrid if you don't specify an alternative table class.
2442//
2443
223d09f6 2444WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
2445
2446IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
2447
2448wxGridStringTable::wxGridStringTable()
2449 : wxGridTableBase()
2450{
2451}
2452
2453wxGridStringTable::wxGridStringTable( int numRows, int numCols )
2454 : wxGridTableBase()
2455{
2456 int row, col;
8f177c8e 2457
f85afd4e
MB
2458 m_data.Alloc( numRows );
2459
2460 wxArrayString sa;
2461 sa.Alloc( numCols );
2462 for ( col = 0; col < numCols; col++ )
2463 {
2464 sa.Add( wxEmptyString );
2465 }
8f177c8e 2466
f85afd4e
MB
2467 for ( row = 0; row < numRows; row++ )
2468 {
2469 m_data.Add( sa );
2470 }
2471}
2472
2473wxGridStringTable::~wxGridStringTable()
2474{
2475}
2476
e32352cf 2477int wxGridStringTable::GetNumberRows()
f85afd4e
MB
2478{
2479 return m_data.GetCount();
2480}
2481
e32352cf 2482int wxGridStringTable::GetNumberCols()
f85afd4e
MB
2483{
2484 if ( m_data.GetCount() > 0 )
2485 return m_data[0].GetCount();
2486 else
2487 return 0;
2488}
2489
2490wxString wxGridStringTable::GetValue( int row, int col )
2491{
831243b1 2492 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
2493 _T("invalid row or column index in wxGridStringTable") );
2494
f85afd4e
MB
2495 return m_data[row][col];
2496}
2497
f2d76237 2498void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 2499{
831243b1 2500 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
2501 _T("invalid row or column index in wxGridStringTable") );
2502
f2d76237 2503 m_data[row][col] = value;
f85afd4e
MB
2504}
2505
2506bool wxGridStringTable::IsEmptyCell( int row, int col )
2507{
831243b1 2508 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
2509 _T("invalid row or column index in wxGridStringTable") );
2510
f85afd4e
MB
2511 return (m_data[row][col] == wxEmptyString);
2512}
2513
f85afd4e
MB
2514void wxGridStringTable::Clear()
2515{
2516 int row, col;
2517 int numRows, numCols;
8f177c8e 2518
f85afd4e
MB
2519 numRows = m_data.GetCount();
2520 if ( numRows > 0 )
2521 {
2522 numCols = m_data[0].GetCount();
2523
2524 for ( row = 0; row < numRows; row++ )
2525 {
2526 for ( col = 0; col < numCols; col++ )
2527 {
2528 m_data[row][col] = wxEmptyString;
2529 }
2530 }
2531 }
2532}
2533
2534
2535bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
2536{
2537 size_t row, col;
2538
2539 size_t curNumRows = m_data.GetCount();
2540 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2541
f85afd4e
MB
2542 if ( pos >= curNumRows )
2543 {
2544 return AppendRows( numRows );
2545 }
8f177c8e 2546
f85afd4e
MB
2547 wxArrayString sa;
2548 sa.Alloc( curNumCols );
2549 for ( col = 0; col < curNumCols; col++ )
2550 {
2551 sa.Add( wxEmptyString );
2552 }
2553
2554 for ( row = pos; row < pos + numRows; row++ )
2555 {
2556 m_data.Insert( sa, row );
2557 }
4d60017a 2558 UpdateAttrRows( pos, numRows );
f85afd4e
MB
2559 if ( GetView() )
2560 {
2561 wxGridTableMessage msg( this,
2562 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
2563 pos,
2564 numRows );
8f177c8e 2565
f85afd4e
MB
2566 GetView()->ProcessTableMessage( msg );
2567 }
2568
2569 return TRUE;
2570}
2571
2572bool wxGridStringTable::AppendRows( size_t numRows )
2573{
2574 size_t row, col;
2575
2576 size_t curNumRows = m_data.GetCount();
2577 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2578
f85afd4e
MB
2579 wxArrayString sa;
2580 if ( curNumCols > 0 )
2581 {
2582 sa.Alloc( curNumCols );
2583 for ( col = 0; col < curNumCols; col++ )
2584 {
2585 sa.Add( wxEmptyString );
2586 }
2587 }
8f177c8e 2588
f85afd4e
MB
2589 for ( row = 0; row < numRows; row++ )
2590 {
2591 m_data.Add( sa );
2592 }
2593
2594 if ( GetView() )
2595 {
2596 wxGridTableMessage msg( this,
2597 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
2598 numRows );
8f177c8e 2599
f85afd4e
MB
2600 GetView()->ProcessTableMessage( msg );
2601 }
2602
8f177c8e 2603 return TRUE;
f85afd4e
MB
2604}
2605
2606bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
2607{
2608 size_t n;
2609
2610 size_t curNumRows = m_data.GetCount();
8f177c8e 2611
f85afd4e
MB
2612 if ( pos >= curNumRows )
2613 {
bd3c6758
MB
2614 wxString errmsg;
2615 errmsg.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2616 "Pos value is invalid for present table with %d rows",
2617 pos, numRows, curNumRows );
2618 wxFAIL_MSG( wxT(errmsg) );
f85afd4e
MB
2619 return FALSE;
2620 }
2621
2622 if ( numRows > curNumRows - pos )
2623 {
2624 numRows = curNumRows - pos;
2625 }
8f177c8e 2626
f85afd4e
MB
2627 if ( numRows >= curNumRows )
2628 {
2629 m_data.Empty(); // don't release memory just yet
2630 }
2631 else
2632 {
2633 for ( n = 0; n < numRows; n++ )
2634 {
2635 m_data.Remove( pos );
2636 }
2637 }
9b4aede2 2638 UpdateAttrRows( pos, -((int)numRows) );
f85afd4e
MB
2639 if ( GetView() )
2640 {
2641 wxGridTableMessage msg( this,
2642 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
2643 pos,
2644 numRows );
8f177c8e 2645
f85afd4e
MB
2646 GetView()->ProcessTableMessage( msg );
2647 }
2648
8f177c8e 2649 return TRUE;
f85afd4e
MB
2650}
2651
2652bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
2653{
2654 size_t row, col;
2655
2656 size_t curNumRows = m_data.GetCount();
2657 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2658
f85afd4e
MB
2659 if ( pos >= curNumCols )
2660 {
2661 return AppendCols( numCols );
2662 }
2663
2664 for ( row = 0; row < curNumRows; row++ )
2665 {
2666 for ( col = pos; col < pos + numCols; col++ )
2667 {
2668 m_data[row].Insert( wxEmptyString, col );
2669 }
2670 }
4d60017a 2671 UpdateAttrCols( pos, numCols );
f85afd4e
MB
2672 if ( GetView() )
2673 {
2674 wxGridTableMessage msg( this,
2675 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
2676 pos,
2677 numCols );
8f177c8e 2678
f85afd4e
MB
2679 GetView()->ProcessTableMessage( msg );
2680 }
2681
2682 return TRUE;
2683}
2684
2685bool wxGridStringTable::AppendCols( size_t numCols )
2686{
2687 size_t row, n;
2688
2689 size_t curNumRows = m_data.GetCount();
2690 if ( !curNumRows )
2691 {
2692 // TODO: something better than this ?
2693 //
bd3c6758
MB
2694 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2695 "Call AppendRows() first") );
f85afd4e
MB
2696 return FALSE;
2697 }
8f177c8e 2698
f85afd4e
MB
2699 for ( row = 0; row < curNumRows; row++ )
2700 {
2701 for ( n = 0; n < numCols; n++ )
2702 {
2703 m_data[row].Add( wxEmptyString );
2704 }
2705 }
2706
2707 if ( GetView() )
2708 {
2709 wxGridTableMessage msg( this,
2710 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
2711 numCols );
8f177c8e 2712
f85afd4e
MB
2713 GetView()->ProcessTableMessage( msg );
2714 }
2715
2716 return TRUE;
2717}
2718
2719bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
2720{
2721 size_t row, n;
2722
2723 size_t curNumRows = m_data.GetCount();
2724 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2725
f85afd4e
MB
2726 if ( pos >= curNumCols )
2727 {
bd3c6758
MB
2728 wxString errmsg;
2729 errmsg.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2730 "Pos value is invalid for present table with %d cols",
2731 pos, numCols, curNumCols );
2732 wxFAIL_MSG( wxT( errmsg ) );
8f177c8e 2733 return FALSE;
f85afd4e
MB
2734 }
2735
2736 if ( numCols > curNumCols - pos )
2737 {
8f177c8e 2738 numCols = curNumCols - pos;
f85afd4e
MB
2739 }
2740
2741 for ( row = 0; row < curNumRows; row++ )
2742 {
2743 if ( numCols >= curNumCols )
2744 {
2745 m_data[row].Clear();
2746 }
2747 else
2748 {
2749 for ( n = 0; n < numCols; n++ )
2750 {
2751 m_data[row].Remove( pos );
2752 }
2753 }
2754 }
9b4aede2 2755 UpdateAttrCols( pos, -((int)numCols) );
f85afd4e
MB
2756 if ( GetView() )
2757 {
2758 wxGridTableMessage msg( this,
2759 wxGRIDTABLE_NOTIFY_COLS_DELETED,
2760 pos,
2761 numCols );
8f177c8e 2762
f85afd4e
MB
2763 GetView()->ProcessTableMessage( msg );
2764 }
2765
8f177c8e 2766 return TRUE;
f85afd4e
MB
2767}
2768
2769wxString wxGridStringTable::GetRowLabelValue( int row )
2770{
2771 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
2772 {
2773 // using default label
2774 //
2775 return wxGridTableBase::GetRowLabelValue( row );
2776 }
2777 else
2778 {
2779 return m_rowLabels[ row ];
2780 }
2781}
2782
2783wxString wxGridStringTable::GetColLabelValue( int col )
2784{
2785 if ( col > (int)(m_colLabels.GetCount()) - 1 )
2786 {
2787 // using default label
2788 //
2789 return wxGridTableBase::GetColLabelValue( col );
2790 }
2791 else
2792 {
2793 return m_colLabels[ col ];
2794 }
2795}
2796
2797void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
2798{
2799 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
2800 {
2801 int n = m_rowLabels.GetCount();
2802 int i;
2803 for ( i = n; i <= row; i++ )
2804 {
2805 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
2806 }
2807 }
2808
2809 m_rowLabels[row] = value;
2810}
2811
2812void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
2813{
2814 if ( col > (int)(m_colLabels.GetCount()) - 1 )
2815 {
2816 int n = m_colLabels.GetCount();
2817 int i;
2818 for ( i = n; i <= col; i++ )
2819 {
2820 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
2821 }
2822 }
2823
2824 m_colLabels[col] = value;
2825}
2826
2827
2828
f85afd4e 2829//////////////////////////////////////////////////////////////////////
2d66e025
MB
2830//////////////////////////////////////////////////////////////////////
2831
2832IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
2833
2834BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
2835 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
2836 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
2837 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
2838END_EVENT_TABLE()
2839
60ff3b99
VZ
2840wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
2841 wxWindowID id,
2d66e025 2842 const wxPoint &pos, const wxSize &size )
ebd773c6 2843 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
2844{
2845 m_owner = parent;
2846}
2847
aa5e1f75 2848void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
2849{
2850 wxPaintDC dc(this);
2851
2852 // NO - don't do this because it will set both the x and y origin
2853 // coords to match the parent scrolled window and we just want to
2854 // set the y coord - MB
2855 //
2856 // m_owner->PrepareDC( dc );
60ff3b99 2857
790ad94f 2858 int x, y;
2d66e025
MB
2859 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
2860 dc.SetDeviceOrigin( 0, -y );
60ff3b99 2861
2d66e025
MB
2862 m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
2863 m_owner->DrawRowLabels( dc );
2864}
2865
2866
2867void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
2868{
2869 m_owner->ProcessRowLabelMouseEvent( event );
2870}
2871
2872
2873// This seems to be required for wxMotif otherwise the mouse
2874// cursor must be in the cell edit control to get key events
2875//
2876void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
2877{
2878 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2879}
2880
2881
2882
2883//////////////////////////////////////////////////////////////////////
2884
2885IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
2886
2887BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
2888 EVT_PAINT( wxGridColLabelWindow::OnPaint )
2889 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
2890 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
2891END_EVENT_TABLE()
2892
60ff3b99
VZ
2893wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
2894 wxWindowID id,
2d66e025 2895 const wxPoint &pos, const wxSize &size )
ebd773c6 2896 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
2897{
2898 m_owner = parent;
2899}
2900
aa5e1f75 2901void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
2902{
2903 wxPaintDC dc(this);
2904
2905 // NO - don't do this because it will set both the x and y origin
2906 // coords to match the parent scrolled window and we just want to
2907 // set the x coord - MB
2908 //
2909 // m_owner->PrepareDC( dc );
60ff3b99 2910
790ad94f 2911 int x, y;
2d66e025
MB
2912 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
2913 dc.SetDeviceOrigin( -x, 0 );
2914
60ff3b99
VZ
2915 m_owner->CalcColLabelsExposed( GetUpdateRegion() );
2916 m_owner->DrawColLabels( dc );
2d66e025
MB
2917}
2918
2919
2920void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
2921{
2922 m_owner->ProcessColLabelMouseEvent( event );
2923}
2924
2925
2926// This seems to be required for wxMotif otherwise the mouse
2927// cursor must be in the cell edit control to get key events
2928//
2929void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
2930{
2931 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2932}
2933
2934
2935
2936//////////////////////////////////////////////////////////////////////
2937
2938IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
2939
2940BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
2941 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
d2fdd8d2 2942 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
2d66e025
MB
2943 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
2944END_EVENT_TABLE()
2945
60ff3b99
VZ
2946wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
2947 wxWindowID id,
2d66e025 2948 const wxPoint &pos, const wxSize &size )
ebd773c6 2949 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
2950{
2951 m_owner = parent;
2952}
2953
d2fdd8d2
RR
2954void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2955{
2956 wxPaintDC dc(this);
b99be8fb 2957
d2fdd8d2
RR
2958 int client_height = 0;
2959 int client_width = 0;
2960 GetClientSize( &client_width, &client_height );
b99be8fb 2961
d2fdd8d2
RR
2962 dc.SetPen( *wxBLACK_PEN );
2963 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
2964 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
b99be8fb 2965
d2fdd8d2
RR
2966 dc.SetPen( *wxWHITE_PEN );
2967 dc.DrawLine( 0, 0, client_width, 0 );
2968 dc.DrawLine( 0, 0, 0, client_height );
2969}
2970
2d66e025
MB
2971
2972void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
2973{
2974 m_owner->ProcessCornerLabelMouseEvent( event );
2975}
2976
2977
2978// This seems to be required for wxMotif otherwise the mouse
2979// cursor must be in the cell edit control to get key events
2980//
2981void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
2982{
2983 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2984}
2985
2986
2987
f85afd4e
MB
2988//////////////////////////////////////////////////////////////////////
2989
2d66e025
MB
2990IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxPanel )
2991
2992BEGIN_EVENT_TABLE( wxGridWindow, wxPanel )
2993 EVT_PAINT( wxGridWindow::OnPaint )
2d66e025
MB
2994 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
2995 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
2796cce3 2996 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
2997END_EVENT_TABLE()
2998
60ff3b99
VZ
2999wxGridWindow::wxGridWindow( wxGrid *parent,
3000 wxGridRowLabelWindow *rowLblWin,
2d66e025
MB
3001 wxGridColLabelWindow *colLblWin,
3002 wxWindowID id, const wxPoint &pos, const wxSize &size )
ebd773c6 3003 : wxPanel( parent, id, pos, size, wxWANTS_CHARS, "grid window" )
2d66e025
MB
3004{
3005 m_owner = parent;
3006 m_rowLabelWin = rowLblWin;
3007 m_colLabelWin = colLblWin;
2d66e025
MB
3008 SetBackgroundColour( "WHITE" );
3009}
3010
3011
3012wxGridWindow::~wxGridWindow()
3013{
3014}
3015
3016
3017void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3018{
3019 wxPaintDC dc( this );
3020 m_owner->PrepareDC( dc );
796df70a
SN
3021 wxRegion reg = GetUpdateRegion();
3022 m_owner->CalcCellsExposed( reg );
2d66e025 3023 m_owner->DrawGridCellArea( dc );
9496deb5 3024#if WXGRID_DRAW_LINES
796df70a
SN
3025 m_owner->DrawAllGridLines( dc, reg );
3026#endif
a5777624 3027 m_owner->DrawGridSpace( dc );
b3a7510d 3028 m_owner->DrawHighlight( dc );
2d66e025
MB
3029}
3030
3031
3032void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3033{
3034 wxPanel::ScrollWindow( dx, dy, rect );
3035 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3036 m_colLabelWin->ScrollWindow( dx, 0, rect );
3037}
3038
3039
3040void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3041{
3042 m_owner->ProcessGridCellMouseEvent( event );
3043}
3044
3045
3046// This seems to be required for wxMotif otherwise the mouse
3047// cursor must be in the cell edit control to get key events
3048//
3049void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3050{
3051 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
3052}
f85afd4e 3053
7c8a8ad5 3054
aa5e1f75 3055void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 3056{
8dd4f536 3057}
025562fe 3058
2d66e025
MB
3059
3060//////////////////////////////////////////////////////////////////////
3061
07296f0b 3062
2d66e025
MB
3063IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
3064
3065BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
f85afd4e
MB
3066 EVT_PAINT( wxGrid::OnPaint )
3067 EVT_SIZE( wxGrid::OnSize )
f85afd4e 3068 EVT_KEY_DOWN( wxGrid::OnKeyDown )
2796cce3 3069 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
f85afd4e 3070END_EVENT_TABLE()
8f177c8e 3071
2d66e025
MB
3072wxGrid::wxGrid( wxWindow *parent,
3073 wxWindowID id,
3074 const wxPoint& pos,
3075 const wxSize& size,
3076 long style,
3077 const wxString& name )
ebd773c6 3078 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
af547d51
VZ
3079 m_colMinWidths(GRID_HASH_SIZE),
3080 m_rowMinHeights(GRID_HASH_SIZE)
2d66e025
MB
3081{
3082 Create();
58dd5b3b
MB
3083}
3084
3085
3086wxGrid::~wxGrid()
3087{
0a976765 3088 ClearAttrCache();
39bcce60 3089 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
3090
3091#ifdef DEBUG_ATTR_CACHE
3092 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3093 wxPrintf(_T("wxGrid attribute cache statistics: "
3094 "total: %u, hits: %u (%u%%)\n"),
3095 total, gs_nAttrCacheHits,
3096 total ? (gs_nAttrCacheHits*100) / total : 0);
3097#endif
3098
2796cce3
RD
3099 if (m_ownTable)
3100 delete m_table;
f2d76237
RD
3101
3102 delete m_typeRegistry;
b5808881 3103 delete m_selection;
58dd5b3b
MB
3104}
3105
2d66e025 3106
58dd5b3b
MB
3107//
3108// ----- internal init and update functions
3109//
3110
3111void wxGrid::Create()
f0102d2a 3112{
4634a5d6 3113 m_created = FALSE; // set to TRUE by CreateGrid
4634a5d6
MB
3114
3115 m_table = (wxGridTableBase *) NULL;
2796cce3 3116 m_ownTable = FALSE;
2c9a89e0
RD
3117
3118 m_cellEditCtrlEnabled = FALSE;
4634a5d6 3119
2796cce3
RD
3120 m_defaultCellAttr = new wxGridCellAttr;
3121 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
f2d76237
RD
3122
3123 // Set default cell attributes
3124 m_defaultCellAttr->SetFont(GetFont());
3125 m_defaultCellAttr->SetAlignment(wxLEFT, wxTOP);
3126 m_defaultCellAttr->SetTextColour(
3127 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
3128 m_defaultCellAttr->SetBackgroundColour(
3129 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
3130 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3131 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2796cce3
RD
3132
3133
4634a5d6
MB
3134 m_numRows = 0;
3135 m_numCols = 0;
3136 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 3137
18f9565d
MB
3138 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3139 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2d66e025 3140
c4608a8a 3141 // create the type registry
f2d76237 3142 m_typeRegistry = new wxGridTypeRegistry;
b5808881 3143 m_selection = 0;
f2d76237 3144 // subwindow components that make up the wxGrid
7807d81c
MB
3145 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3146 -1,
18f9565d
MB
3147 wxDefaultPosition,
3148 wxDefaultSize );
7807d81c 3149
60ff3b99
VZ
3150 m_rowLabelWin = new wxGridRowLabelWindow( this,
3151 -1,
18f9565d
MB
3152 wxDefaultPosition,
3153 wxDefaultSize );
2d66e025
MB
3154
3155 m_colLabelWin = new wxGridColLabelWindow( this,
3156 -1,
18f9565d
MB
3157 wxDefaultPosition,
3158 wxDefaultSize );
60ff3b99 3159
60ff3b99
VZ
3160 m_gridWin = new wxGridWindow( this,
3161 m_rowLabelWin,
3162 m_colLabelWin,
3163 -1,
18f9565d 3164 wxDefaultPosition,
2d66e025
MB
3165 wxDefaultSize );
3166
3167 SetTargetWindow( m_gridWin );
2d66e025 3168}
f85afd4e 3169
2d66e025 3170
b5808881
SN
3171bool wxGrid::CreateGrid( int numRows, int numCols,
3172 wxGrid::wxGridSelectionModes selmode )
2d66e025
MB
3173{
3174 if ( m_created )
3175 {
2796cce3 3176 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2d66e025
MB
3177 return FALSE;
3178 }
3179 else
3180 {
3181 m_numRows = numRows;
3182 m_numCols = numCols;
3183
3184 m_table = new wxGridStringTable( m_numRows, m_numCols );
3185 m_table->SetView( this );
2796cce3
RD
3186 m_ownTable = TRUE;
3187 Init();
043d16b2 3188 m_selection = new wxGridSelection( this, selmode );
2796cce3
RD
3189 m_created = TRUE;
3190 }
2796cce3
RD
3191 return m_created;
3192}
3193
f1567cdd
SN
3194void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
3195{
3196 if ( !m_created )
3197 {
3198 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3199 }
3200 else
3201 m_selection->SetSelectionMode( selmode );
3202}
3203
043d16b2
SN
3204bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
3205 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
3206{
3207 if ( m_created )
3208 {
fb295790
RD
3209 // RD: Actually, this should probably be allowed. I think it would be
3210 // nice to be able to switch multiple Tables in and out of a single
3211 // View at runtime. Is there anything in the implmentation that would
3212 // prevent this?
3213
d95b0c2b 3214 // At least, you now have to cope with m_selection
2796cce3
RD
3215 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3216 return FALSE;
3217 }
3218 else
3219 {
3220 m_numRows = table->GetNumberRows();
3221 m_numCols = table->GetNumberCols();
3222
3223 m_table = table;
3224 m_table->SetView( this );
3225 if (takeOwnership)
3226 m_ownTable = TRUE;
2d66e025 3227 Init();
043d16b2 3228 m_selection = new wxGridSelection( this, selmode );
2d66e025
MB
3229 m_created = TRUE;
3230 }
f85afd4e 3231
2d66e025 3232 return m_created;
f85afd4e
MB
3233}
3234
2d66e025 3235
f85afd4e
MB
3236void wxGrid::Init()
3237{
f85afd4e
MB
3238 if ( m_numRows <= 0 )
3239 m_numRows = WXGRID_DEFAULT_NUMBER_ROWS;
3240
3241 if ( m_numCols <= 0 )
3242 m_numCols = WXGRID_DEFAULT_NUMBER_COLS;
3243
3244 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3245 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3246
60ff3b99
VZ
3247 if ( m_rowLabelWin )
3248 {
3249 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
3250 }
3251 else
3252 {
3253 m_labelBackgroundColour = wxColour( _T("WHITE") );
3254 }
3255
3256 m_labelTextColour = wxColour( _T("BLACK") );
f85afd4e 3257
0a976765
VZ
3258 // init attr cache
3259 m_attrCache.row = -1;
3260
f85afd4e
MB
3261 // TODO: something better than this ?
3262 //
3263 m_labelFont = this->GetFont();
3264 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
8f177c8e 3265
f85afd4e
MB
3266 m_rowLabelHorizAlign = wxLEFT;
3267 m_rowLabelVertAlign = wxCENTRE;
3268
3269 m_colLabelHorizAlign = wxCENTRE;
3270 m_colLabelVertAlign = wxTOP;
3271
f85afd4e 3272 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
3273 m_defaultRowHeight = m_gridWin->GetCharHeight();
3274
d2fdd8d2 3275#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
3276 m_defaultRowHeight += 8;
3277#else
3278 m_defaultRowHeight += 4;
3279#endif
3280
2d66e025 3281 m_gridLineColour = wxColour( 128, 128, 255 );
f85afd4e 3282 m_gridLinesEnabled = TRUE;
8f177c8e 3283
58dd5b3b 3284 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 3285 m_winCapture = (wxWindow *)NULL;
6e8524b1
MB
3286 m_canDragRowSize = TRUE;
3287 m_canDragColSize = TRUE;
4cfa5de6 3288 m_canDragGridSize = TRUE;
f85afd4e
MB
3289 m_dragLastPos = -1;
3290 m_dragRowOrCol = -1;
3291 m_isDragging = FALSE;
07296f0b 3292 m_startDragPos = wxDefaultPosition;
f85afd4e 3293
07296f0b 3294 m_waitForSlowClick = FALSE;
025562fe 3295
f85afd4e
MB
3296 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
3297 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
3298
3299 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 3300
b5808881
SN
3301 m_selectingTopLeft = wxGridNoCellCoords;
3302 m_selectingBottomRight = wxGridNoCellCoords;
2796cce3
RD
3303 m_selectionBackground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
3304 m_selectionForeground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 3305
f85afd4e
MB
3306 m_editable = TRUE; // default for whole grid
3307
2d66e025
MB
3308 m_inOnKeyDown = FALSE;
3309 m_batchCount = 0;
3c79cf49 3310
266e8367
VZ
3311 m_extraWidth =
3312 m_extraHeight = 50;
3313
3c79cf49 3314 CalcDimensions();
7c1cb261
VZ
3315}
3316
3317// ----------------------------------------------------------------------------
3318// the idea is to call these functions only when necessary because they create
3319// quite big arrays which eat memory mostly unnecessary - in particular, if
3320// default widths/heights are used for all rows/columns, we may not use these
3321// arrays at all
3322//
3323// with some extra code, it should be possible to only store the
3324// widths/heights different from default ones but this will be done later...
3325// ----------------------------------------------------------------------------
3326
3327void wxGrid::InitRowHeights()
3328{
3329 m_rowHeights.Empty();
3330 m_rowBottoms.Empty();
3331
3332 m_rowHeights.Alloc( m_numRows );
3333 m_rowBottoms.Alloc( m_numRows );
3334
3335 int rowBottom = 0;
3336 for ( int i = 0; i < m_numRows; i++ )
3337 {
3338 m_rowHeights.Add( m_defaultRowHeight );
3339 rowBottom += m_defaultRowHeight;
3340 m_rowBottoms.Add( rowBottom );
3341 }
3342}
3343
3344void wxGrid::InitColWidths()
3345{
3346 m_colWidths.Empty();
3347 m_colRights.Empty();
3348
3349 m_colWidths.Alloc( m_numCols );
3350 m_colRights.Alloc( m_numCols );
3351 int colRight = 0;
3352 for ( int i = 0; i < m_numCols; i++ )
3353 {
3354 m_colWidths.Add( m_defaultColWidth );
3355 colRight += m_defaultColWidth;
3356 m_colRights.Add( colRight );
3357 }
3358}
3359
3360int wxGrid::GetColWidth(int col) const
3361{
3362 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
3363}
3364
3365int wxGrid::GetColLeft(int col) const
3366{
3367 return m_colRights.IsEmpty() ? col * m_defaultColWidth
3368 : m_colRights[col] - m_colWidths[col];
3369}
3370
3371int wxGrid::GetColRight(int col) const
3372{
3373 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3374 : m_colRights[col];
3375}
3376
3377int wxGrid::GetRowHeight(int row) const
3378{
3379 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3380}
2d66e025 3381
7c1cb261
VZ
3382int wxGrid::GetRowTop(int row) const
3383{
3384 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3385 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
3386}
3387
7c1cb261
VZ
3388int wxGrid::GetRowBottom(int row) const
3389{
3390 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3391 : m_rowBottoms[row];
3392}
f85afd4e
MB
3393
3394void wxGrid::CalcDimensions()
3395{
f85afd4e 3396 int cw, ch;
2d66e025 3397 GetClientSize( &cw, &ch );
f85afd4e 3398
2d66e025 3399 if ( m_numRows > 0 && m_numCols > 0 )
f85afd4e 3400 {
266e8367
VZ
3401 int right = GetColRight( m_numCols-1 ) + m_extraWidth;
3402 int bottom = GetRowBottom( m_numRows-1 ) + m_extraHeight;
60ff3b99 3403
2d66e025
MB
3404 // TODO: restore the scroll position that we had before sizing
3405 //
3406 int x, y;
3407 GetViewStart( &x, &y );
f0102d2a
VZ
3408 SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE,
3409 right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE,
be46e4a6 3410 x, y );
f85afd4e 3411 }
f85afd4e
MB
3412}
3413
3414
7807d81c
MB
3415void wxGrid::CalcWindowSizes()
3416{
3417 int cw, ch;
3418 GetClientSize( &cw, &ch );
b99be8fb 3419
7807d81c
MB
3420 if ( m_cornerLabelWin->IsShown() )
3421 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3422
3423 if ( m_colLabelWin->IsShown() )
3424 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3425
3426 if ( m_rowLabelWin->IsShown() )
3427 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3428
3429 if ( m_gridWin->IsShown() )
3430 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3431}
3432
3433
f85afd4e
MB
3434// this is called when the grid table sends a message to say that it
3435// has been redimensioned
3436//
3437bool wxGrid::Redimension( wxGridTableMessage& msg )
3438{
3439 int i;
8f177c8e 3440
7c1cb261
VZ
3441 // if we were using the default widths/heights so far, we must change them
3442 // now
3443 if ( m_colWidths.IsEmpty() )
3444 {
3445 InitColWidths();
3446 }
3447
3448 if ( m_rowHeights.IsEmpty() )
3449 {
3450 InitRowHeights();
3451 }
3452
f85afd4e
MB
3453 switch ( msg.GetId() )
3454 {
3455 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3456 {
3457 size_t pos = msg.GetCommandInt();
3458 int numRows = msg.GetCommandInt2();
3459 for ( i = 0; i < numRows; i++ )
3460 {
3461 m_rowHeights.Insert( m_defaultRowHeight, pos );
3462 m_rowBottoms.Insert( 0, pos );
3463 }
3464 m_numRows += numRows;
2d66e025
MB
3465
3466 int bottom = 0;
3467 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
60ff3b99 3468
2d66e025
MB
3469 for ( i = pos; i < m_numRows; i++ )
3470 {
3471 bottom += m_rowHeights[i];
3472 m_rowBottoms[i] = bottom;
3473 }
f85afd4e
MB
3474 CalcDimensions();
3475 }
3476 return TRUE;
3477
3478 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3479 {
3480 int numRows = msg.GetCommandInt();
3481 for ( i = 0; i < numRows; i++ )
3482 {
3483 m_rowHeights.Add( m_defaultRowHeight );
3484 m_rowBottoms.Add( 0 );
3485 }
2d66e025
MB
3486
3487 int oldNumRows = m_numRows;
f85afd4e 3488 m_numRows += numRows;
2d66e025
MB
3489
3490 int bottom = 0;
3491 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
60ff3b99 3492
2d66e025
MB
3493 for ( i = oldNumRows; i < m_numRows; i++ )
3494 {
3495 bottom += m_rowHeights[i];
3496 m_rowBottoms[i] = bottom;
3497 }
f85afd4e
MB
3498 CalcDimensions();
3499 }
3500 return TRUE;
3501
3502 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
3503 {
3504 size_t pos = msg.GetCommandInt();
3505 int numRows = msg.GetCommandInt2();
3506 for ( i = 0; i < numRows; i++ )
3507 {
3508 m_rowHeights.Remove( pos );
3509 m_rowBottoms.Remove( pos );
3510 }
3511 m_numRows -= numRows;
3512
f85afd4e
MB
3513 if ( !m_numRows )
3514 {
3515 m_numCols = 0;
3516 m_colWidths.Clear();
3517 m_colRights.Clear();
3518 m_currentCellCoords = wxGridNoCellCoords;
3519 }
2d66e025 3520 else
f85afd4e 3521 {
2d66e025
MB
3522 if ( m_currentCellCoords.GetRow() >= m_numRows )
3523 m_currentCellCoords.Set( 0, 0 );
3524
3525 int h = 0;
3526 for ( i = 0; i < m_numRows; i++ )
3527 {
3528 h += m_rowHeights[i];
3529 m_rowBottoms[i] = h;
3530 }
f85afd4e 3531 }
60ff3b99 3532
f85afd4e
MB
3533 CalcDimensions();
3534 }
3535 return TRUE;
3536
3537 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
3538 {
3539 size_t pos = msg.GetCommandInt();
3540 int numCols = msg.GetCommandInt2();
3541 for ( i = 0; i < numCols; i++ )
3542 {
3543 m_colWidths.Insert( m_defaultColWidth, pos );
3544 m_colRights.Insert( 0, pos );
3545 }
3546 m_numCols += numCols;
2d66e025
MB
3547
3548 int right = 0;
3549 if ( pos > 0 ) right = m_colRights[pos-1];
60ff3b99 3550
2d66e025
MB
3551 for ( i = pos; i < m_numCols; i++ )
3552 {
3553 right += m_colWidths[i];
3554 m_colRights[i] = right;
3555 }
f85afd4e
MB
3556 CalcDimensions();
3557 }
3558 return TRUE;
3559
3560 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
3561 {
3562 int numCols = msg.GetCommandInt();
3563 for ( i = 0; i < numCols; i++ )
3564 {
3565 m_colWidths.Add( m_defaultColWidth );
3566 m_colRights.Add( 0 );
3567 }
2d66e025
MB
3568
3569 int oldNumCols = m_numCols;
f85afd4e 3570 m_numCols += numCols;
2d66e025
MB
3571
3572 int right = 0;
3573 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
60ff3b99 3574
2d66e025
MB
3575 for ( i = oldNumCols; i < m_numCols; i++ )
3576 {
3577 right += m_colWidths[i];
3578 m_colRights[i] = right;
3579 }
f85afd4e
MB
3580 CalcDimensions();
3581 }
3582 return TRUE;
3583
3584 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
3585 {
3586 size_t pos = msg.GetCommandInt();
3587 int numCols = msg.GetCommandInt2();
3588 for ( i = 0; i < numCols; i++ )
3589 {
3590 m_colWidths.Remove( pos );
3591 m_colRights.Remove( pos );
3592 }
3593 m_numCols -= numCols;
f85afd4e
MB
3594
3595 if ( !m_numCols )
3596 {
3597#if 0 // leave the row alone here so that AppendCols will work subsequently
3598 m_numRows = 0;
3599 m_rowHeights.Clear();
3600 m_rowBottoms.Clear();
8f177c8e 3601#endif
f85afd4e
MB
3602 m_currentCellCoords = wxGridNoCellCoords;
3603 }
60ff3b99 3604 else
f85afd4e 3605 {
2d66e025
MB
3606 if ( m_currentCellCoords.GetCol() >= m_numCols )
3607 m_currentCellCoords.Set( 0, 0 );
3608
3609 int w = 0;
3610 for ( i = 0; i < m_numCols; i++ )
3611 {
3612 w += m_colWidths[i];
3613 m_colRights[i] = w;
3614 }
f85afd4e
MB
3615 }
3616 CalcDimensions();
3617 }
3618 return TRUE;
3619 }
3620
3621 return FALSE;
3622}
3623
3624
2d66e025 3625void wxGrid::CalcRowLabelsExposed( wxRegion& reg )
f85afd4e 3626{
2d66e025
MB
3627 wxRegionIterator iter( reg );
3628 wxRect r;
f85afd4e 3629
2d66e025 3630 m_rowLabelsExposed.Empty();
f85afd4e 3631
2d66e025
MB
3632 int top, bottom;
3633 while ( iter )
f85afd4e 3634 {
2d66e025 3635 r = iter.GetRect();
f85afd4e 3636
2d66e025
MB
3637 // TODO: remove this when we can...
3638 // There is a bug in wxMotif that gives garbage update
3639 // rectangles if you jump-scroll a long way by clicking the
3640 // scrollbar with middle button. This is a work-around
3641 //
3642#if defined(__WXMOTIF__)
3643 int cw, ch;
3644 m_gridWin->GetClientSize( &cw, &ch );
3645 if ( r.GetTop() > ch ) r.SetTop( 0 );
3646 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3647#endif
f85afd4e 3648
2d66e025
MB
3649 // logical bounds of update region
3650 //
3651 int dummy;
3652 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3653 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3654
3655 // find the row labels within these bounds
3656 //
3657 int row;
2d66e025
MB
3658 for ( row = 0; row < m_numRows; row++ )
3659 {
7c1cb261
VZ
3660 if ( GetRowBottom(row) < top )
3661 continue;
2d66e025 3662
6d55126d 3663 if ( GetRowTop(row) > bottom )
7c1cb261 3664 break;
60ff3b99 3665
2d66e025
MB
3666 m_rowLabelsExposed.Add( row );
3667 }
60ff3b99 3668
2d66e025 3669 iter++ ;
f85afd4e
MB
3670 }
3671}
3672
3673
2d66e025 3674void wxGrid::CalcColLabelsExposed( wxRegion& reg )
f85afd4e 3675{
2d66e025
MB
3676 wxRegionIterator iter( reg );
3677 wxRect r;
f85afd4e 3678
2d66e025 3679 m_colLabelsExposed.Empty();
f85afd4e 3680
2d66e025
MB
3681 int left, right;
3682 while ( iter )
f85afd4e 3683 {
2d66e025 3684 r = iter.GetRect();
f85afd4e 3685
2d66e025
MB
3686 // TODO: remove this when we can...
3687 // There is a bug in wxMotif that gives garbage update
3688 // rectangles if you jump-scroll a long way by clicking the
3689 // scrollbar with middle button. This is a work-around
3690 //
3691#if defined(__WXMOTIF__)
3692 int cw, ch;
3693 m_gridWin->GetClientSize( &cw, &ch );
3694 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3695 r.SetRight( wxMin( r.GetRight(), cw ) );
3696#endif
3697
3698 // logical bounds of update region
3699 //
3700 int dummy;
3701 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3702 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3703
3704 // find the cells within these bounds
3705 //
3706 int col;
2d66e025
MB
3707 for ( col = 0; col < m_numCols; col++ )
3708 {
7c1cb261
VZ
3709 if ( GetColRight(col) < left )
3710 continue;
60ff3b99 3711
7c1cb261
VZ
3712 if ( GetColLeft(col) > right )
3713 break;
2d66e025
MB
3714
3715 m_colLabelsExposed.Add( col );
3716 }
60ff3b99 3717
2d66e025 3718 iter++ ;
f85afd4e
MB
3719 }
3720}
3721
3722
2d66e025 3723void wxGrid::CalcCellsExposed( wxRegion& reg )
f85afd4e 3724{
2d66e025
MB
3725 wxRegionIterator iter( reg );
3726 wxRect r;
f85afd4e 3727
2d66e025
MB
3728 m_cellsExposed.Empty();
3729 m_rowsExposed.Empty();
3730 m_colsExposed.Empty();
f85afd4e 3731
2d66e025
MB
3732 int left, top, right, bottom;
3733 while ( iter )
3734 {
3735 r = iter.GetRect();
f85afd4e 3736
2d66e025
MB
3737 // TODO: remove this when we can...
3738 // There is a bug in wxMotif that gives garbage update
3739 // rectangles if you jump-scroll a long way by clicking the
3740 // scrollbar with middle button. This is a work-around
3741 //
3742#if defined(__WXMOTIF__)
f85afd4e 3743 int cw, ch;
2d66e025
MB
3744 m_gridWin->GetClientSize( &cw, &ch );
3745 if ( r.GetTop() > ch ) r.SetTop( 0 );
3746 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3747 r.SetRight( wxMin( r.GetRight(), cw ) );
3748 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3749#endif
8f177c8e 3750
2d66e025
MB
3751 // logical bounds of update region
3752 //
3753 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3754 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 3755
2d66e025 3756 // find the cells within these bounds
f85afd4e 3757 //
2d66e025 3758 int row, col;
2d66e025 3759 for ( row = 0; row < m_numRows; row++ )
f85afd4e 3760 {
7c1cb261
VZ
3761 if ( GetRowBottom(row) <= top )
3762 continue;
f85afd4e 3763
7c1cb261
VZ
3764 if ( GetRowTop(row) > bottom )
3765 break;
60ff3b99 3766
2d66e025 3767 m_rowsExposed.Add( row );
f85afd4e 3768
2d66e025
MB
3769 for ( col = 0; col < m_numCols; col++ )
3770 {
7c1cb261
VZ
3771 if ( GetColRight(col) <= left )
3772 continue;
60ff3b99 3773
7c1cb261
VZ
3774 if ( GetColLeft(col) > right )
3775 break;
60ff3b99 3776
7c1cb261
VZ
3777 if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
3778 m_colsExposed.Add( col );
2d66e025
MB
3779 m_cellsExposed.Add( wxGridCellCoords( row, col ) );
3780 }
3781 }
60ff3b99 3782
7c1cb261 3783 iter++;
f85afd4e
MB
3784 }
3785}
3786
3787
2d66e025 3788void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3789{
2d66e025
MB
3790 int x, y, row;
3791 wxPoint pos( event.GetPosition() );
3792 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3793
2d66e025 3794 if ( event.Dragging() )
f85afd4e
MB
3795 {
3796 m_isDragging = TRUE;
8f177c8e 3797
2d66e025 3798 if ( event.LeftIsDown() )
f85afd4e
MB
3799 {
3800 switch( m_cursorMode )
3801 {
f85afd4e
MB
3802 case WXGRID_CURSOR_RESIZE_ROW:
3803 {
2d66e025
MB
3804 int cw, ch, left, dummy;
3805 m_gridWin->GetClientSize( &cw, &ch );
3806 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 3807
2d66e025
MB
3808 wxClientDC dc( m_gridWin );
3809 PrepareDC( dc );
af547d51
VZ
3810 y = wxMax( y,
3811 GetRowTop(m_dragRowOrCol) +
3812 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 3813 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
3814 if ( m_dragLastPos >= 0 )
3815 {
2d66e025 3816 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 3817 }
2d66e025
MB
3818 dc.DrawLine( left, y, left+cw, y );
3819 m_dragLastPos = y;
f85afd4e
MB
3820 }
3821 break;
3822
3823 case WXGRID_CURSOR_SELECT_ROW:
e32352cf 3824 if ( (row = YToRow( y )) >= 0 )
aa5e1f75
SN
3825 {
3826 m_selection->SelectRow( row,
3827 event.ControlDown(),
3828 event.ShiftDown(),
3829 event.AltDown(),
3830 event.MetaDown() );
f85afd4e 3831 }
e2b42eeb
VZ
3832
3833 // default label to suppress warnings about "enumeration value
3834 // 'xxx' not handled in switch
3835 default:
3836 break;
f85afd4e
MB
3837 }
3838 }
3839 return;
3840 }
3841
3842 m_isDragging = FALSE;
8f177c8e 3843
60ff3b99 3844
6d004f67
MB
3845 // ------------ Entering or leaving the window
3846 //
3847 if ( event.Entering() || event.Leaving() )
3848 {
e2b42eeb 3849 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
3850 }
3851
e2b42eeb 3852
2d66e025 3853 // ------------ Left button pressed
f85afd4e 3854 //
6d004f67 3855 else if ( event.LeftDown() )
f85afd4e 3856 {
2d66e025
MB
3857 // don't send a label click event for a hit on the
3858 // edge of the row label - this is probably the user
3859 // wanting to resize the row
3860 //
3861 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 3862 {
2d66e025 3863 row = YToRow(y);
58dd5b3b 3864 if ( row >= 0 &&
b54ba671 3865 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 3866 {
aa5e1f75
SN
3867 if ( !event.ShiftDown() && !event.ControlDown() )
3868 ClearSelection();
3869 if ( event.ShiftDown() )
3870 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
3871 0,
3872 row,
3873 GetNumberCols() - 1,
3874 event.ControlDown(),
3875 event.ShiftDown(),
3876 event.AltDown(),
3877 event.MetaDown() );
3878 else
3879 m_selection->SelectRow( row,
3880 event.ControlDown(),
3881 event.ShiftDown(),
3882 event.AltDown(),
3883 event.MetaDown() );
e2b42eeb 3884 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 3885 }
2d66e025
MB
3886 }
3887 else
3888 {
3889 // starting to drag-resize a row
3890 //
6e8524b1
MB
3891 if ( CanDragRowSize() )
3892 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
3893 }
3894 }
f85afd4e 3895
f85afd4e 3896
2d66e025
MB
3897 // ------------ Left double click
3898 //
3899 else if (event.LeftDClick() )
3900 {
3901 if ( YToEdgeOfRow(y) < 0 )
3902 {
3903 row = YToRow(y);
b54ba671 3904 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
f85afd4e
MB
3905 }
3906 }
60ff3b99
VZ
3907
3908
2d66e025 3909 // ------------ Left button released
f85afd4e 3910 //
2d66e025 3911 else if ( event.LeftUp() )
f85afd4e 3912 {
2d66e025 3913 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 3914 {
6d004f67 3915 DoEndDragResizeRow();
60ff3b99 3916
6d004f67
MB
3917 // Note: we are ending the event *after* doing
3918 // default processing in this case
3919 //
b54ba671 3920 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 3921 }
f85afd4e 3922
e2b42eeb
VZ
3923 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3924 m_dragLastPos = -1;
2d66e025 3925 }
f85afd4e 3926
f85afd4e 3927
2d66e025
MB
3928 // ------------ Right button down
3929 //
3930 else if ( event.RightDown() )
3931 {
3932 row = YToRow(y);
b54ba671 3933 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
3934 {
3935 // no default action at the moment
f85afd4e
MB
3936 }
3937 }
60ff3b99
VZ
3938
3939
2d66e025 3940 // ------------ Right double click
f85afd4e 3941 //
2d66e025
MB
3942 else if ( event.RightDClick() )
3943 {
3944 row = YToRow(y);
b54ba671 3945 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
3946 {
3947 // no default action at the moment
3948 }
3949 }
60ff3b99
VZ
3950
3951
2d66e025 3952 // ------------ No buttons down and mouse moving
f85afd4e 3953 //
2d66e025 3954 else if ( event.Moving() )
f85afd4e 3955 {
2d66e025
MB
3956 m_dragRowOrCol = YToEdgeOfRow( y );
3957 if ( m_dragRowOrCol >= 0 )
8f177c8e 3958 {
2d66e025 3959 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 3960 {
e2b42eeb 3961 // don't capture the mouse yet
6e8524b1
MB
3962 if ( CanDragRowSize() )
3963 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
8f177c8e 3964 }
2d66e025 3965 }
6d004f67 3966 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3967 {
e2b42eeb 3968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
8f177c8e 3969 }
f85afd4e 3970 }
2d66e025
MB
3971}
3972
3973
3974void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3975{
3976 int x, y, col;
3977 wxPoint pos( event.GetPosition() );
3978 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3979
2d66e025 3980 if ( event.Dragging() )
f85afd4e 3981 {
2d66e025 3982 m_isDragging = TRUE;
8f177c8e 3983
2d66e025 3984 if ( event.LeftIsDown() )
8f177c8e 3985 {
2d66e025 3986 switch( m_cursorMode )
8f177c8e 3987 {
2d66e025 3988 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 3989 {
2d66e025
MB
3990 int cw, ch, dummy, top;
3991 m_gridWin->GetClientSize( &cw, &ch );
3992 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 3993
2d66e025
MB
3994 wxClientDC dc( m_gridWin );
3995 PrepareDC( dc );
43947979
VZ
3996
3997 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3998 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 3999 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
4000 if ( m_dragLastPos >= 0 )
4001 {
4002 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4003 }
4004 dc.DrawLine( x, top, x, top+ch );
4005 m_dragLastPos = x;
f85afd4e 4006 }
2d66e025 4007 break;
f85afd4e 4008
2d66e025 4009 case WXGRID_CURSOR_SELECT_COL:
e32352cf 4010 if ( (col = XToCol( x )) >= 0 )
aa5e1f75
SN
4011 {
4012 m_selection->SelectCol( col,
4013 event.ControlDown(),
4014 event.ShiftDown(),
4015 event.AltDown(),
4016 event.MetaDown() );
2d66e025 4017 }
e2b42eeb
VZ
4018
4019 // default label to suppress warnings about "enumeration value
4020 // 'xxx' not handled in switch
4021 default:
4022 break;
2d66e025 4023 }
f85afd4e 4024 }
2d66e025 4025 return;
f85afd4e 4026 }
2d66e025
MB
4027
4028 m_isDragging = FALSE;
4029
60ff3b99 4030
6d004f67
MB
4031 // ------------ Entering or leaving the window
4032 //
4033 if ( event.Entering() || event.Leaving() )
4034 {
e2b42eeb 4035 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
4036 }
4037
e2b42eeb 4038
2d66e025 4039 // ------------ Left button pressed
f85afd4e 4040 //
6d004f67 4041 else if ( event.LeftDown() )
f85afd4e 4042 {
2d66e025
MB
4043 // don't send a label click event for a hit on the
4044 // edge of the col label - this is probably the user
4045 // wanting to resize the col
4046 //
4047 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 4048 {
2d66e025 4049 col = XToCol(x);
58dd5b3b 4050 if ( col >= 0 &&
b54ba671 4051 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 4052 {
aa5e1f75
SN
4053 if ( !event.ShiftDown() && !event.ControlDown() )
4054 ClearSelection();
4055 if ( event.ShiftDown() )
4056 m_selection->SelectBlock( 0,
4057 m_currentCellCoords.GetCol(),
4058 GetNumberRows() - 1, col,
4059 event.ControlDown(),
4060 event.ShiftDown(),
4061 event.AltDown(),
4062 event.MetaDown() );
4063 else
4064 m_selection->SelectCol( col,
4065 event.ControlDown(),
4066 event.ShiftDown(),
4067 event.AltDown(),
4068 event.MetaDown() );
e2b42eeb 4069 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
f85afd4e 4070 }
2d66e025
MB
4071 }
4072 else
4073 {
4074 // starting to drag-resize a col
4075 //
6e8524b1
MB
4076 if ( CanDragColSize() )
4077 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
4078 }
4079 }
f85afd4e 4080
f85afd4e 4081
2d66e025
MB
4082 // ------------ Left double click
4083 //
4084 if ( event.LeftDClick() )
4085 {
4086 if ( XToEdgeOfCol(x) < 0 )
4087 {
4088 col = XToCol(x);
b54ba671 4089 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
2d66e025
MB
4090 }
4091 }
60ff3b99
VZ
4092
4093
2d66e025
MB
4094 // ------------ Left button released
4095 //
4096 else if ( event.LeftUp() )
4097 {
4098 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4099 {
6d004f67 4100 DoEndDragResizeCol();
e2b42eeb 4101
6d004f67
MB
4102 // Note: we are ending the event *after* doing
4103 // default processing in this case
4104 //
b54ba671 4105 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
2d66e025 4106 }
f85afd4e 4107
e2b42eeb 4108 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
2d66e025 4109 m_dragLastPos = -1;
60ff3b99
VZ
4110 }
4111
4112
2d66e025
MB
4113 // ------------ Right button down
4114 //
4115 else if ( event.RightDown() )
4116 {
4117 col = XToCol(x);
b54ba671 4118 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
4119 {
4120 // no default action at the moment
f85afd4e
MB
4121 }
4122 }
60ff3b99
VZ
4123
4124
2d66e025 4125 // ------------ Right double click
f85afd4e 4126 //
2d66e025
MB
4127 else if ( event.RightDClick() )
4128 {
4129 col = XToCol(x);
b54ba671 4130 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
4131 {
4132 // no default action at the moment
4133 }
4134 }
60ff3b99
VZ
4135
4136
2d66e025 4137 // ------------ No buttons down and mouse moving
f85afd4e 4138 //
2d66e025 4139 else if ( event.Moving() )
f85afd4e 4140 {
2d66e025
MB
4141 m_dragRowOrCol = XToEdgeOfCol( x );
4142 if ( m_dragRowOrCol >= 0 )
f85afd4e 4143 {
2d66e025 4144 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4145 {
e2b42eeb 4146 // don't capture the cursor yet
6e8524b1
MB
4147 if ( CanDragColSize() )
4148 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
f85afd4e 4149 }
2d66e025 4150 }
6d004f67 4151 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 4152 {
e2b42eeb 4153 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
8f177c8e 4154 }
f85afd4e
MB
4155 }
4156}
4157
4158
2d66e025 4159void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 4160{
2d66e025 4161 if ( event.LeftDown() )
f85afd4e 4162 {
2d66e025
MB
4163 // indicate corner label by having both row and
4164 // col args == -1
f85afd4e 4165 //
b54ba671 4166 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
4167 {
4168 SelectAll();
4169 }
f85afd4e
MB
4170 }
4171
2d66e025
MB
4172 else if ( event.LeftDClick() )
4173 {
b54ba671 4174 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 4175 }
8f177c8e 4176
2d66e025 4177 else if ( event.RightDown() )
f85afd4e 4178 {
b54ba671 4179 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 4180 {
2d66e025
MB
4181 // no default action at the moment
4182 }
4183 }
f85afd4e 4184
2d66e025
MB
4185 else if ( event.RightDClick() )
4186 {
b54ba671 4187 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
4188 {
4189 // no default action at the moment
4190 }
4191 }
4192}
f85afd4e 4193
e2b42eeb
VZ
4194void wxGrid::ChangeCursorMode(CursorMode mode,
4195 wxWindow *win,
4196 bool captureMouse)
4197{
4198#ifdef __WXDEBUG__
4199 static const wxChar *cursorModes[] =
4200 {
4201 _T("SELECT_CELL"),
4202 _T("RESIZE_ROW"),
4203 _T("RESIZE_COL"),
4204 _T("SELECT_ROW"),
4205 _T("SELECT_COL")
4206 };
4207
181bfffd
VZ
4208 wxLogTrace(_T("grid"),
4209 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
4210 win == m_colLabelWin ? _T("colLabelWin")
4211 : win ? _T("rowLabelWin")
4212 : _T("gridWin"),
4213 cursorModes[m_cursorMode], cursorModes[mode]);
4214#endif // __WXDEBUG__
4215
4216 if ( mode == m_cursorMode )
4217 return;
4218
4219 if ( !win )
4220 {
4221 // by default use the grid itself
4222 win = m_gridWin;
4223 }
4224
4225 if ( m_winCapture )
4226 {
4227 m_winCapture->ReleaseMouse();
4228 m_winCapture = (wxWindow *)NULL;
4229 }
4230
4231 m_cursorMode = mode;
4232
4233 switch ( m_cursorMode )
4234 {
4235 case WXGRID_CURSOR_RESIZE_ROW:
4236 win->SetCursor( m_rowResizeCursor );
4237 break;
4238
4239 case WXGRID_CURSOR_RESIZE_COL:
4240 win->SetCursor( m_colResizeCursor );
4241 break;
4242
4243 default:
4244 win->SetCursor( *wxSTANDARD_CURSOR );
4245 }
4246
4247 // we need to capture mouse when resizing
4248 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
4249 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
4250
4251 if ( captureMouse && resize )
4252 {
4253 win->CaptureMouse();
4254 m_winCapture = win;
4255 }
4256}
8f177c8e 4257
2d66e025
MB
4258void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
4259{
4260 int x, y;
4261 wxPoint pos( event.GetPosition() );
4262 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4263
2d66e025
MB
4264 wxGridCellCoords coords;
4265 XYToCell( x, y, coords );
60ff3b99 4266
2d66e025
MB
4267 if ( event.Dragging() )
4268 {
07296f0b
RD
4269 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4270
4271 // Don't start doing anything until the mouse has been drug at
4272 // least 3 pixels in any direction...
508011ce
VZ
4273 if (! m_isDragging)
4274 {
4275 if (m_startDragPos == wxDefaultPosition)
4276 {
07296f0b
RD
4277 m_startDragPos = pos;
4278 return;
4279 }
4280 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
4281 return;
4282 }
4283
2d66e025 4284 m_isDragging = TRUE;
2d66e025
MB
4285 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4286 {
f0102d2a
VZ
4287 // Hide the edit control, so it
4288 // won't interfer with drag-shrinking.
4289 if ( IsCellEditControlEnabled() )
a5777624 4290 {
f0102d2a 4291 HideCellEditControl();
a5777624
RD
4292 SaveEditControlValue();
4293 }
07296f0b
RD
4294
4295 // Have we captured the mouse yet?
508011ce
VZ
4296 if (! m_winCapture)
4297 {
07296f0b
RD
4298 m_winCapture = m_gridWin;
4299 m_winCapture->CaptureMouse();
4300 }
4301
2d66e025
MB
4302 if ( coords != wxGridNoCellCoords )
4303 {
aa5e1f75
SN
4304 if ( event.ControlDown() )
4305 {
4306 if ( m_selectingKeyboard == wxGridNoCellCoords)
4307 m_selectingKeyboard = coords;
4308 SelectBlock ( m_selectingKeyboard, coords );
4309 }
4310 else
4311 {
4312 if ( !IsSelection() )
4313 {
4314 SelectBlock( coords, coords );
4315 }
4316 else
4317 {
4318 SelectBlock( m_currentCellCoords, coords );
4319 }
f85afd4e 4320 }
07296f0b 4321
508011ce
VZ
4322 if (! IsVisible(coords))
4323 {
07296f0b
RD
4324 MakeCellVisible(coords);
4325 // TODO: need to introduce a delay or something here. The
e32352cf 4326 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 4327 }
2d66e025
MB
4328 }
4329 }
6d004f67
MB
4330 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4331 {
4332 int cw, ch, left, dummy;
4333 m_gridWin->GetClientSize( &cw, &ch );
4334 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 4335
6d004f67
MB
4336 wxClientDC dc( m_gridWin );
4337 PrepareDC( dc );
a95e38c0
VZ
4338 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
4339 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
4340 dc.SetLogicalFunction(wxINVERT);
4341 if ( m_dragLastPos >= 0 )
4342 {
4343 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4344 }
4345 dc.DrawLine( left, y, left+cw, y );
4346 m_dragLastPos = y;
4347 }
4348 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4349 {
4350 int cw, ch, dummy, top;
4351 m_gridWin->GetClientSize( &cw, &ch );
4352 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 4353
6d004f67
MB
4354 wxClientDC dc( m_gridWin );
4355 PrepareDC( dc );
43947979
VZ
4356 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4357 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
4358 dc.SetLogicalFunction(wxINVERT);
4359 if ( m_dragLastPos >= 0 )
4360 {
4361 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4362 }
4363 dc.DrawLine( x, top, x, top+ch );
4364 m_dragLastPos = x;
4365 }
e2b42eeb 4366
2d66e025
MB
4367 return;
4368 }
66242c80 4369
2d66e025 4370 m_isDragging = FALSE;
07296f0b
RD
4371 m_startDragPos = wxDefaultPosition;
4372
a5777624
RD
4373 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4374 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4375 // wxGTK
e2b42eeb 4376#if 0
a5777624
RD
4377 if ( event.Entering() || event.Leaving() )
4378 {
4379 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4380 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
4381 }
4382 else
e2b42eeb
VZ
4383#endif // 0
4384
a5777624
RD
4385 // ------------ Left button pressed
4386 //
4387 if ( event.LeftDown() && coords != wxGridNoCellCoords )
4388 {
b5808881
SN
4389 if ( !event.ShiftDown() && !event.ControlDown() )
4390 ClearSelection();
a5777624 4391 if ( event.ShiftDown() )
2d66e025 4392 {
5c8fc7c1
SN
4393 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4394 m_currentCellCoords.GetCol(),
4395 coords.GetRow(),
d95b0c2b
SN
4396 coords.GetCol(),
4397 event.ControlDown(),
4398 event.ShiftDown(),
4399 event.AltDown(),
4400 event.MetaDown() );
a5777624
RD
4401 }
4402 else if ( XToEdgeOfCol(x) < 0 &&
4403 YToEdgeOfRow(y) < 0 )
4404 {
4405 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
4406 coords.GetRow(),
4407 coords.GetCol(),
4408 event ) )
58dd5b3b 4409 {
a5777624
RD
4410 DisableCellEditControl();
4411 MakeCellVisible( coords );
4412
4413 // if this is the second click on this cell then start
4414 // the edit control
4415 if ( m_waitForSlowClick &&
4416 (coords == m_currentCellCoords) &&
4417 CanEnableCellControl())
58dd5b3b 4418 {
a5777624 4419 EnableCellEditControl();
e195a54c 4420
a5777624 4421 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
0b190b0f
VZ
4422 wxGridCellEditor *editor = attr->GetEditor(this,
4423 coords.GetRow(),
4424 coords.GetCol());
4425 editor->StartingClick();
4426 editor->DecRef();
a5777624 4427 attr->DecRef();
e195a54c 4428
a5777624
RD
4429 m_waitForSlowClick = FALSE;
4430 }
4431 else
4432 {
aa5e1f75
SN
4433 if ( event.ControlDown() )
4434 {
4435 m_selection->ToggleCellSelection( coords.GetRow(),
4436 coords.GetCol(),
4437 event.ControlDown(),
4438 event.ShiftDown(),
4439 event.AltDown(),
4440 event.MetaDown() );
4441 m_selectingTopLeft = wxGridNoCellCoords;
4442 m_selectingBottomRight = wxGridNoCellCoords;
4443 m_selectingKeyboard = coords;
4444 }
4445 else
4446 SetCurrentCell( coords );
a5777624 4447 m_waitForSlowClick = TRUE;
58dd5b3b
MB
4448 }
4449 }
f85afd4e 4450 }
a5777624 4451 }
f85afd4e 4452
f85afd4e 4453
a5777624
RD
4454 // ------------ Left double click
4455 //
4456 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
4457 {
4458 DisableCellEditControl();
4459
4460 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 4461 {
a5777624
RD
4462 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
4463 coords.GetRow(),
4464 coords.GetCol(),
4465 event );
58dd5b3b 4466 }
a5777624 4467 }
f85afd4e 4468
8f177c8e 4469
a5777624
RD
4470 // ------------ Left button released
4471 //
4472 else if ( event.LeftUp() )
4473 {
4474 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4475 {
b5808881
SN
4476 if ( m_selectingTopLeft != wxGridNoCellCoords &&
4477 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 4478 {
a5777624 4479 if (m_winCapture)
58dd5b3b 4480 {
a5777624
RD
4481 m_winCapture->ReleaseMouse();
4482 m_winCapture = NULL;
58dd5b3b 4483 }
5c8fc7c1
SN
4484 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
4485 m_selectingTopLeft.GetCol(),
4486 m_selectingBottomRight.GetRow(),
4487 m_selectingBottomRight.GetCol(),
d95b0c2b
SN
4488 event.ControlDown(),
4489 event.ShiftDown(),
4490 event.AltDown(),
4491 event.MetaDown() );
5c8fc7c1
SN
4492 m_selectingTopLeft = wxGridNoCellCoords;
4493 m_selectingBottomRight = wxGridNoCellCoords;
6d004f67 4494 }
2d66e025 4495
a5777624
RD
4496 // Show the edit control, if it has been hidden for
4497 // drag-shrinking.
4498 ShowCellEditControl();
4499 }
4500 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4501 {
4502 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4503 DoEndDragResizeRow();
e2b42eeb 4504
a5777624
RD
4505 // Note: we are ending the event *after* doing
4506 // default processing in this case
4507 //
4508 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4509 }
4510 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4511 {
4512 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4513 DoEndDragResizeCol();
e2b42eeb 4514
a5777624
RD
4515 // Note: we are ending the event *after* doing
4516 // default processing in this case
4517 //
4518 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 4519 }
f85afd4e 4520
a5777624
RD
4521 m_dragLastPos = -1;
4522 }
4523
f85afd4e 4524
a5777624
RD
4525 // ------------ Right button down
4526 //
4527 else if ( event.RightDown() && coords != wxGridNoCellCoords )
4528 {
4529 DisableCellEditControl();
4530 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
4531 coords.GetRow(),
4532 coords.GetCol(),
4533 event ) )
f85afd4e 4534 {
a5777624 4535 // no default action at the moment
60ff3b99 4536 }
a5777624 4537 }
2d66e025 4538
60ff3b99 4539
a5777624
RD
4540 // ------------ Right double click
4541 //
4542 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
4543 {
4544 DisableCellEditControl();
4545 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
4546 coords.GetRow(),
4547 coords.GetCol(),
4548 event ) )
f85afd4e 4549 {
a5777624 4550 // no default action at the moment
60ff3b99 4551 }
a5777624
RD
4552 }
4553
4554 // ------------ Moving and no button action
4555 //
4556 else if ( event.Moving() && !event.IsButton() )
4557 {
4558 int dragRow = YToEdgeOfRow( y );
4559 int dragCol = XToEdgeOfCol( x );
790cc417 4560
a5777624
RD
4561 // Dragging on the corner of a cell to resize in both
4562 // directions is not implemented yet...
790cc417 4563 //
a5777624 4564 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 4565 {
a5777624
RD
4566 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4567 return;
4568 }
6d004f67 4569
a5777624
RD
4570 if ( dragRow >= 0 )
4571 {
4572 m_dragRowOrCol = dragRow;
6d004f67 4573
a5777624 4574 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 4575 {
a5777624
RD
4576 if ( CanDragRowSize() && CanDragGridSize() )
4577 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 4578 }
e2b42eeb 4579
b94ae1ea
VZ
4580 if ( dragCol >= 0 )
4581 {
4582 m_dragRowOrCol = dragCol;
122603f1 4583 }
b94ae1ea 4584
a5777624
RD
4585 return;
4586 }
e2b42eeb 4587
a5777624
RD
4588 if ( dragCol >= 0 )
4589 {
4590 m_dragRowOrCol = dragCol;
e2b42eeb 4591
a5777624 4592 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 4593 {
a5777624
RD
4594 if ( CanDragColSize() && CanDragGridSize() )
4595 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67 4596 }
a5777624
RD
4597
4598 return;
6d004f67 4599 }
a5777624
RD
4600
4601 // Neither on a row or col edge
4602 //
4603 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4604 {
4605 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4606 }
4607 }
6d004f67
MB
4608}
4609
4610
4611void wxGrid::DoEndDragResizeRow()
4612{
4613 if ( m_dragLastPos >= 0 )
4614 {
4615 // erase the last line and resize the row
4616 //
4617 int cw, ch, left, dummy;
4618 m_gridWin->GetClientSize( &cw, &ch );
4619 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4620
4621 wxClientDC dc( m_gridWin );
4622 PrepareDC( dc );
4623 dc.SetLogicalFunction( wxINVERT );
4624 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4625 HideCellEditControl();
a5777624 4626 SaveEditControlValue();
6d004f67 4627
7c1cb261 4628 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67
MB
4629 SetRowSize( m_dragRowOrCol,
4630 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
e2b42eeb 4631
6d004f67
MB
4632 if ( !GetBatchCount() )
4633 {
4634 // Only needed to get the correct rect.y:
4635 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
4636 rect.x = 0;
4637 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
4638 rect.width = m_rowLabelWidth;
4639 rect.height = ch - rect.y;
4640 m_rowLabelWin->Refresh( TRUE, &rect );
4641 rect.width = cw;
4642 m_gridWin->Refresh( FALSE, &rect );
4643 }
4644
4645 ShowCellEditControl();
4646 }
4647}
4648
4649
4650void wxGrid::DoEndDragResizeCol()
4651{
4652 if ( m_dragLastPos >= 0 )
4653 {
4654 // erase the last line and resize the col
4655 //
4656 int cw, ch, dummy, top;
4657 m_gridWin->GetClientSize( &cw, &ch );
4658 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4659
4660 wxClientDC dc( m_gridWin );
4661 PrepareDC( dc );
4662 dc.SetLogicalFunction( wxINVERT );
4663 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4664 HideCellEditControl();
a5777624 4665 SaveEditControlValue();
6d004f67 4666
7c1cb261 4667 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 4668 SetColSize( m_dragRowOrCol,
43947979
VZ
4669 wxMax( m_dragLastPos - colLeft,
4670 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
4671
4672 if ( !GetBatchCount() )
4673 {
4674 // Only needed to get the correct rect.x:
4675 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
4676 rect.y = 0;
4677 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
4678 rect.width = cw - rect.x;
4679 rect.height = m_colLabelHeight;
4680 m_colLabelWin->Refresh( TRUE, &rect );
4681 rect.height = ch;
4682 m_gridWin->Refresh( FALSE, &rect );
790cc417 4683 }
e2b42eeb 4684
6d004f67 4685 ShowCellEditControl();
f85afd4e 4686 }
f85afd4e
MB
4687}
4688
4689
6d004f67 4690
2d66e025
MB
4691//
4692// ------ interaction with data model
4693//
4694bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 4695{
2d66e025 4696 switch ( msg.GetId() )
17732cec 4697 {
2d66e025
MB
4698 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4699 return GetModelValues();
17732cec 4700
2d66e025
MB
4701 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4702 return SetModelValues();
f85afd4e 4703
2d66e025
MB
4704 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4705 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4706 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4707 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4708 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4709 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4710 return Redimension( msg );
4711
4712 default:
4713 return FALSE;
f85afd4e 4714 }
2d66e025 4715}
f85afd4e 4716
f85afd4e 4717
f85afd4e 4718
2d66e025
MB
4719// The behaviour of this function depends on the grid table class
4720// Clear() function. For the default wxGridStringTable class the
4721// behavious is to replace all cell contents with wxEmptyString but
4722// not to change the number of rows or cols.
4723//
4724void wxGrid::ClearGrid()
4725{
4726 if ( m_table )
f85afd4e 4727 {
4cfa5de6
RD
4728 if (IsCellEditControlEnabled())
4729 DisableCellEditControl();
4730
2d66e025 4731 m_table->Clear();
1f1ce288 4732 if ( !GetBatchCount() ) m_gridWin->Refresh();
f85afd4e
MB
4733 }
4734}
4735
4736
2d66e025 4737bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4738{
2d66e025 4739 // TODO: something with updateLabels flag
8f177c8e 4740
2d66e025 4741 if ( !m_created )
f85afd4e 4742 {
bd3c6758 4743 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2d66e025
MB
4744 return FALSE;
4745 }
8f177c8e 4746
2d66e025
MB
4747 if ( m_table )
4748 {
b7fff980 4749 if (IsCellEditControlEnabled())
b54ba671 4750 DisableCellEditControl();
b7fff980 4751
2d66e025 4752 bool ok = m_table->InsertRows( pos, numRows );
f85afd4e 4753
2d66e025
MB
4754 // the table will have sent the results of the insert row
4755 // operation to this view object as a grid table message
4756 //
4757 if ( ok )
4758 {
4759 if ( m_numCols == 0 )
f85afd4e 4760 {
2d66e025
MB
4761 m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
4762 //
4763 // TODO: perhaps instead of appending the default number of cols
4764 // we should remember what the last non-zero number of cols was ?
4765 //
4766 }
8f177c8e 4767
2d66e025
MB
4768 if ( m_currentCellCoords == wxGridNoCellCoords )
4769 {
4770 // if we have just inserted cols into an empty grid the current
4771 // cell will be undefined...
4772 //
4773 SetCurrentCell( 0, 0 );
f85afd4e
MB
4774 }
4775
b5808881 4776 m_selection->UpdateRows( pos, numRows );
2d66e025 4777 if ( !GetBatchCount() ) Refresh();
f85afd4e 4778 }
2d66e025 4779
2d66e025
MB
4780 return ok;
4781 }
4782 else
4783 {
4784 return FALSE;
f85afd4e
MB
4785 }
4786}
4787
4788
2d66e025 4789bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4790{
2d66e025
MB
4791 // TODO: something with updateLabels flag
4792
4793 if ( !m_created )
f85afd4e 4794 {
bd3c6758 4795 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2d66e025
MB
4796 return FALSE;
4797 }
4798
4799 if ( m_table && m_table->AppendRows( numRows ) )
4800 {
4801 if ( m_currentCellCoords == wxGridNoCellCoords )
4802 {
4803 // if we have just inserted cols into an empty grid the current
4804 // cell will be undefined...
4805 //
4806 SetCurrentCell( 0, 0 );
4807 }
4808
4809 // the table will have sent the results of the append row
4810 // operation to this view object as a grid table message
4811 //
2d66e025
MB
4812 if ( !GetBatchCount() ) Refresh();
4813 return TRUE;
4814 }
4815 else
4816 {
4817 return FALSE;
f85afd4e
MB
4818 }
4819}
4820
2d66e025
MB
4821
4822bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4823{
2d66e025
MB
4824 // TODO: something with updateLabels flag
4825
4826 if ( !m_created )
f85afd4e 4827 {
bd3c6758 4828 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2d66e025
MB
4829 return FALSE;
4830 }
4831
b7fff980 4832 if ( m_table )
2d66e025 4833 {
b7fff980 4834 if (IsCellEditControlEnabled())
b54ba671 4835 DisableCellEditControl();
f85afd4e 4836
b7fff980
RD
4837 if (m_table->DeleteRows( pos, numRows ))
4838 {
4839
4840 // the table will have sent the results of the delete row
4841 // operation to this view object as a grid table message
4842 //
b5808881 4843 m_selection->UpdateRows( pos, -((int)numRows) );
b7fff980
RD
4844 if ( !GetBatchCount() ) Refresh();
4845 return TRUE;
4846 }
2d66e025 4847 }
b7fff980 4848 return FALSE;
2d66e025 4849}
f85afd4e 4850
f85afd4e 4851
2d66e025
MB
4852bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4853{
4854 // TODO: something with updateLabels flag
8f177c8e 4855
2d66e025
MB
4856 if ( !m_created )
4857 {
bd3c6758 4858 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2d66e025
MB
4859 return FALSE;
4860 }
f85afd4e 4861
2d66e025
MB
4862 if ( m_table )
4863 {
b7fff980 4864 if (IsCellEditControlEnabled())
b54ba671 4865 DisableCellEditControl();
b7fff980 4866
2d66e025
MB
4867 bool ok = m_table->InsertCols( pos, numCols );
4868
4869 // the table will have sent the results of the insert col
4870 // operation to this view object as a grid table message
4871 //
4872 if ( ok )
f85afd4e 4873 {
2d66e025 4874 if ( m_currentCellCoords == wxGridNoCellCoords )
f85afd4e 4875 {
2d66e025
MB
4876 // if we have just inserted cols into an empty grid the current
4877 // cell will be undefined...
4878 //
4879 SetCurrentCell( 0, 0 );
f85afd4e 4880 }
2d66e025 4881
b5808881 4882 m_selection->UpdateCols( pos, numCols );
2d66e025 4883 if ( !GetBatchCount() ) Refresh();
f85afd4e 4884 }
2d66e025 4885
2d66e025
MB
4886 return ok;
4887 }
4888 else
4889 {
4890 return FALSE;
f85afd4e
MB
4891 }
4892}
4893
2d66e025
MB
4894
4895bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 4896{
2d66e025
MB
4897 // TODO: something with updateLabels flag
4898
4899 if ( !m_created )
f85afd4e 4900 {
bd3c6758 4901 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2d66e025
MB
4902 return FALSE;
4903 }
f85afd4e 4904
2d66e025
MB
4905 if ( m_table && m_table->AppendCols( numCols ) )
4906 {
4907 // the table will have sent the results of the append col
4908 // operation to this view object as a grid table message
4909 //
4910 if ( m_currentCellCoords == wxGridNoCellCoords )
f85afd4e 4911 {
2d66e025
MB
4912 // if we have just inserted cols into an empty grid the current
4913 // cell will be undefined...
4914 //
4915 SetCurrentCell( 0, 0 );
f85afd4e 4916 }
8f177c8e 4917
2d66e025
MB
4918 if ( !GetBatchCount() ) Refresh();
4919 return TRUE;
f85afd4e 4920 }
2d66e025 4921 else
f85afd4e 4922 {
2d66e025 4923 return FALSE;
f85afd4e 4924 }
f85afd4e
MB
4925}
4926
4927
2d66e025 4928bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 4929{
2d66e025 4930 // TODO: something with updateLabels flag
8f177c8e 4931
2d66e025 4932 if ( !m_created )
f85afd4e 4933 {
bd3c6758 4934 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2d66e025 4935 return FALSE;
f85afd4e
MB
4936 }
4937
b7fff980 4938 if ( m_table )
2d66e025 4939 {
b7fff980 4940 if (IsCellEditControlEnabled())
b54ba671 4941 DisableCellEditControl();
8f177c8e 4942
b7fff980
RD
4943 if ( m_table->DeleteCols( pos, numCols ) )
4944 {
4945 // the table will have sent the results of the delete col
4946 // operation to this view object as a grid table message
4947 //
b5808881 4948 m_selection->UpdateCols( pos, -((int)numCols) );
b7fff980
RD
4949 if ( !GetBatchCount() ) Refresh();
4950 return TRUE;
4951 }
f85afd4e 4952 }
b7fff980 4953 return FALSE;
f85afd4e
MB
4954}
4955
4956
2d66e025
MB
4957
4958//
4959// ----- event handlers
f85afd4e 4960//
8f177c8e 4961
2d66e025
MB
4962// Generate a grid event based on a mouse event and
4963// return the result of ProcessEvent()
4964//
4965bool wxGrid::SendEvent( const wxEventType type,
4966 int row, int col,
4967 wxMouseEvent& mouseEv )
4968{
b54ba671 4969 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 4970 {
2d66e025
MB
4971 int rowOrCol = (row == -1 ? col : row);
4972
4973 wxGridSizeEvent gridEvt( GetId(),
4974 type,
4975 this,
4976 rowOrCol,
4977 mouseEv.GetX(), mouseEv.GetY(),
4978 mouseEv.ControlDown(),
4979 mouseEv.ShiftDown(),
4980 mouseEv.AltDown(),
4981 mouseEv.MetaDown() );
4982
4983 return GetEventHandler()->ProcessEvent(gridEvt);
f85afd4e 4984 }
b54ba671 4985 else if ( type == wxEVT_GRID_RANGE_SELECT )
2d66e025 4986 {
5c8fc7c1 4987 // Right now, it should _never_ end up here!
2d66e025
MB
4988 wxGridRangeSelectEvent gridEvt( GetId(),
4989 type,
4990 this,
b5808881
SN
4991 m_selectingTopLeft,
4992 m_selectingBottomRight,
5c8fc7c1 4993 TRUE,
2d66e025
MB
4994 mouseEv.ControlDown(),
4995 mouseEv.ShiftDown(),
4996 mouseEv.AltDown(),
4997 mouseEv.MetaDown() );
f85afd4e 4998
2d66e025
MB
4999 return GetEventHandler()->ProcessEvent(gridEvt);
5000 }
5001 else
5002 {
5003 wxGridEvent gridEvt( GetId(),
5004 type,
5005 this,
5006 row, col,
5007 mouseEv.GetX(), mouseEv.GetY(),
d7da9756 5008 FALSE,
2d66e025
MB
5009 mouseEv.ControlDown(),
5010 mouseEv.ShiftDown(),
5011 mouseEv.AltDown(),
5012 mouseEv.MetaDown() );
8f177c8e 5013
2d66e025
MB
5014 return GetEventHandler()->ProcessEvent(gridEvt);
5015 }
f85afd4e
MB
5016}
5017
5018
2d66e025
MB
5019// Generate a grid event of specified type and return the result
5020// of ProcessEvent().
f85afd4e 5021//
2d66e025
MB
5022bool wxGrid::SendEvent( const wxEventType type,
5023 int row, int col )
f85afd4e 5024{
b54ba671 5025 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 5026 {
2d66e025 5027 int rowOrCol = (row == -1 ? col : row);
f85afd4e 5028
2d66e025
MB
5029 wxGridSizeEvent gridEvt( GetId(),
5030 type,
5031 this,
5032 rowOrCol );
5033
5034 return GetEventHandler()->ProcessEvent(gridEvt);
5035 }
5036 else
5037 {
5038 wxGridEvent gridEvt( GetId(),
5039 type,
5040 this,
5041 row, col );
8f177c8e 5042
2d66e025
MB
5043 return GetEventHandler()->ProcessEvent(gridEvt);
5044 }
f85afd4e
MB
5045}
5046
5047
2d66e025 5048void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 5049{
2d66e025 5050 wxPaintDC dc( this );
8f177c8e 5051}
f85afd4e
MB
5052
5053
18f9565d
MB
5054// This is just here to make sure that CalcDimensions gets called when
5055// the grid view is resized... then the size event is skipped to allow
5056// the box sizers to handle everything
5057//
aa5e1f75 5058void wxGrid::OnSize( wxSizeEvent& WXUNUSED(event) )
f85afd4e 5059{
7807d81c 5060 CalcWindowSizes();
2d66e025 5061 CalcDimensions();
f85afd4e
MB
5062}
5063
f85afd4e 5064
2d66e025 5065void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 5066{
2d66e025 5067 if ( m_inOnKeyDown )
f85afd4e 5068 {
2d66e025
MB
5069 // shouldn't be here - we are going round in circles...
5070 //
07296f0b 5071 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
5072 }
5073
2d66e025 5074 m_inOnKeyDown = TRUE;
f85afd4e 5075
2d66e025
MB
5076 // propagate the event up and see if it gets processed
5077 //
5078 wxWindow *parent = GetParent();
5079 wxKeyEvent keyEvt( event );
5080 keyEvt.SetEventObject( parent );
5081
5082 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 5083 {
9b4aede2 5084
2d66e025
MB
5085 // try local handlers
5086 //
d95b0c2b
SN
5087 if ( !event.ShiftDown() &&
5088 m_selectingKeyboard != wxGridNoCellCoords )
5089 {
aa5e1f75
SN
5090 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5091 m_selectingBottomRight != wxGridNoCellCoords )
5092 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5093 m_selectingTopLeft.GetCol(),
5094 m_selectingBottomRight.GetRow(),
5095 m_selectingBottomRight.GetCol(),
5096 event.ControlDown(),
5097 event.ShiftDown(),
5098 event.AltDown(),
5099 event.MetaDown() );
d95b0c2b
SN
5100 m_selectingTopLeft = wxGridNoCellCoords;
5101 m_selectingBottomRight = wxGridNoCellCoords;
5102 m_selectingKeyboard = wxGridNoCellCoords;
5103 }
5104
2d66e025
MB
5105 switch ( event.KeyCode() )
5106 {
5107 case WXK_UP:
5108 if ( event.ControlDown() )
5109 {
5c8fc7c1 5110 MoveCursorUpBlock( event.ShiftDown() );
2d66e025
MB
5111 }
5112 else
5113 {
5c8fc7c1 5114 MoveCursorUp( event.ShiftDown() );
2d66e025
MB
5115 }
5116 break;
f85afd4e 5117
2d66e025
MB
5118 case WXK_DOWN:
5119 if ( event.ControlDown() )
5120 {
5c8fc7c1 5121 MoveCursorDownBlock( event.ShiftDown() );
2d66e025
MB
5122 }
5123 else
5124 {
5c8fc7c1 5125 MoveCursorDown( event.ShiftDown() );
2d66e025
MB
5126 }
5127 break;
8f177c8e 5128
2d66e025
MB
5129 case WXK_LEFT:
5130 if ( event.ControlDown() )
5131 {
5c8fc7c1 5132 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025
MB
5133 }
5134 else
5135 {
5c8fc7c1 5136 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
5137 }
5138 break;
5139
5140 case WXK_RIGHT:
5141 if ( event.ControlDown() )
5142 {
5c8fc7c1 5143 MoveCursorRightBlock( event.ShiftDown() );
2d66e025
MB
5144 }
5145 else
5146 {
5c8fc7c1 5147 MoveCursorRight( event.ShiftDown() );
2d66e025
MB
5148 }
5149 break;
b99be8fb 5150
2d66e025 5151 case WXK_RETURN:
58dd5b3b
MB
5152 if ( event.ControlDown() )
5153 {
5154 event.Skip(); // to let the edit control have the return
5155 }
5156 else
5157 {
5c8fc7c1 5158 MoveCursorDown( event.ShiftDown() );
58dd5b3b 5159 }
2d66e025
MB
5160 break;
5161
5c8fc7c1 5162 case WXK_ESCAPE:
e32352cf 5163 ClearSelection();
5c8fc7c1
SN
5164 break;
5165
2c9a89e0
RD
5166 case WXK_TAB:
5167 if (event.ShiftDown())
5c8fc7c1 5168 MoveCursorLeft( FALSE );
2c9a89e0 5169 else
5c8fc7c1 5170 MoveCursorRight( FALSE );
2c9a89e0
RD
5171 break;
5172
2d66e025
MB
5173 case WXK_HOME:
5174 if ( event.ControlDown() )
5175 {
5176 MakeCellVisible( 0, 0 );
5177 SetCurrentCell( 0, 0 );
5178 }
5179 else
5180 {
5181 event.Skip();
5182 }
5183 break;
5184
5185 case WXK_END:
5186 if ( event.ControlDown() )
5187 {
5188 MakeCellVisible( m_numRows-1, m_numCols-1 );
5189 SetCurrentCell( m_numRows-1, m_numCols-1 );
5190 }
5191 else
5192 {
5193 event.Skip();
5194 }
5195 break;
5196
5197 case WXK_PRIOR:
5198 MovePageUp();
5199 break;
5200
5201 case WXK_NEXT:
5202 MovePageDown();
5203 break;
5204
07296f0b 5205 case WXK_SPACE:
aa5e1f75
SN
5206 if ( event.ControlDown() )
5207 {
5208 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
5209 m_currentCellCoords.GetCol(),
5210 event.ControlDown(),
5211 event.ShiftDown(),
5212 event.AltDown(),
5213 event.MetaDown() );
5214 break;
5215 }
07296f0b
RD
5216 if ( !IsEditable() )
5217 {
5c8fc7c1 5218 MoveCursorRight( FALSE );
07296f0b
RD
5219 break;
5220 }
5221 // Otherwise fall through to default
5222
2d66e025 5223 default:
c239a4bb
VZ
5224 // alphanumeric keys or F2 (special key just for this) enable
5225 // the cell edit control
5c8fc7c1
SN
5226 // On just Shift/Control I get values for event.KeyCode()
5227 // that are outside the range where isalnum's behaviour is
5228 // well defined, so do an additional sanity check.
b94ae1ea
VZ
5229 if ( !(event.AltDown() ||
5230 event.MetaDown() ||
5231 event.ControlDown()) &&
4a64bee4 5232 ((isalnum((int)event.KeyCode()) &&
5c8fc7c1
SN
5233 (event.KeyCode() < 256 && event.KeyCode() >= 0)) ||
5234 event.KeyCode() == WXK_F2) &&
b94ae1ea
VZ
5235 !IsCellEditControlEnabled() &&
5236 CanEnableCellControl() )
b54ba671
VZ
5237 {
5238 EnableCellEditControl();
f2d76237
RD
5239 int row = m_currentCellCoords.GetRow();
5240 int col = m_currentCellCoords.GetCol();
5241 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f
VZ
5242 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5243 editor->StartingKey(event);
5244 editor->DecRef();
2c9a89e0
RD
5245 attr->DecRef();
5246 }
b3a7510d
VZ
5247 else
5248 {
b94ae1ea
VZ
5249 // let others process char events with modifiers or all
5250 // char events for readonly cells
b3a7510d
VZ
5251 event.Skip();
5252 }
025562fe 5253 break;
2d66e025 5254 }
f85afd4e
MB
5255 }
5256
2d66e025 5257 m_inOnKeyDown = FALSE;
f85afd4e
MB
5258}
5259
2796cce3 5260void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
5261{
5262}
07296f0b 5263
2d66e025 5264void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 5265{
2a7750d9 5266 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 5267 {
2d66e025 5268 HideCellEditControl();
b54ba671 5269 DisableCellEditControl();
07296f0b
RD
5270
5271 // Clear the old current cell highlight
5272 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
d1c0b4f9
VZ
5273
5274 // Otherwise refresh redraws the highlight!
5275 m_currentCellCoords = coords;
5276
07296f0b 5277 m_gridWin->Refresh( FALSE, &r );
66242c80 5278 }
8f177c8e 5279
2d66e025
MB
5280 m_currentCellCoords = coords;
5281
2a7750d9
MB
5282 wxClientDC dc(m_gridWin);
5283 PrepareDC(dc);
5284
5285 wxGridCellAttr* attr = GetCellAttr(coords);
5286 DrawCellHighlight(dc, attr);
5287 attr->DecRef();
283b7808 5288
b5808881
SN
5289#if 0
5290 // SN: For my extended selection code, automatic
5291 // deselection is definitely not a good idea.
4634a5d6
MB
5292 if ( IsSelection() )
5293 {
5294 wxRect r( SelectionToDeviceRect() );
5295 ClearSelection();
5296 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
5297 }
b5808881 5298#endif
66242c80
MB
5299}
5300
2d66e025
MB
5301
5302//
5303// ------ functions to get/send data (see also public functions)
5304//
5305
5306bool wxGrid::GetModelValues()
66242c80 5307{
2d66e025 5308 if ( m_table )
66242c80 5309 {
2d66e025 5310 // all we need to do is repaint the grid
66242c80 5311 //
2d66e025 5312 m_gridWin->Refresh();
66242c80
MB
5313 return TRUE;
5314 }
8f177c8e 5315
66242c80
MB
5316 return FALSE;
5317}
5318
2d66e025
MB
5319
5320bool wxGrid::SetModelValues()
f85afd4e 5321{
2d66e025 5322 int row, col;
8f177c8e 5323
2d66e025
MB
5324 if ( m_table )
5325 {
5326 for ( row = 0; row < m_numRows; row++ )
f85afd4e 5327 {
2d66e025 5328 for ( col = 0; col < m_numCols; col++ )
f85afd4e 5329 {
2d66e025 5330 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
5331 }
5332 }
8f177c8e 5333
f85afd4e
MB
5334 return TRUE;
5335 }
5336
5337 return FALSE;
5338}
5339
2d66e025
MB
5340
5341
5342// Note - this function only draws cells that are in the list of
5343// exposed cells (usually set from the update region by
5344// CalcExposedCells)
5345//
5346void wxGrid::DrawGridCellArea( wxDC& dc )
f85afd4e 5347{
2d66e025 5348 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5349
2d66e025
MB
5350 size_t i;
5351 size_t numCells = m_cellsExposed.GetCount();
60ff3b99 5352
2d66e025 5353 for ( i = 0; i < numCells; i++ )
f85afd4e 5354 {
2d66e025
MB
5355 DrawCell( dc, m_cellsExposed[i] );
5356 }
5357}
8f177c8e 5358
8f177c8e 5359
7c8a8ad5
MB
5360void wxGrid::DrawGridSpace( wxDC& dc )
5361{
5362 if ( m_numRows && m_numCols )
5363 {
5364 int cw, ch;
5365 m_gridWin->GetClientSize( &cw, &ch );
5366
5367 int right, bottom;
5368 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5369
5370 if ( right > GetColRight(m_numCols-1) ||
5371 bottom > GetRowBottom(m_numRows-1) )
5372 {
5373 int left, top;
5374 CalcUnscrolledPosition( 0, 0, &left, &top );
5375
5376 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
5377 dc.SetPen( *wxTRANSPARENT_PEN );
5378
5379 if ( right > GetColRight(m_numCols-1) )
a5777624 5380 dc.DrawRectangle( GetColRight(m_numCols-1), top,
7c8a8ad5
MB
5381 right - GetColRight(m_numCols-1), ch );
5382
5383 if ( bottom > GetRowBottom(m_numRows-1) )
a5777624 5384 dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
7c8a8ad5
MB
5385 cw, bottom - GetRowBottom(m_numRows-1) );
5386 }
5387 }
5388}
5389
5390
2d66e025
MB
5391void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
5392{
ab79958a
VZ
5393 int row = coords.GetRow();
5394 int col = coords.GetCol();
60ff3b99 5395
7c1cb261 5396 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
5397 return;
5398
5399 // we draw the cell border ourselves
9496deb5 5400#if !WXGRID_DRAW_LINES
2d66e025
MB
5401 if ( m_gridLinesEnabled )
5402 DrawCellBorder( dc, coords );
796df70a 5403#endif
f85afd4e 5404
189d0213
VZ
5405 wxGridCellAttr* attr = GetCellAttr(row, col);
5406
5407 bool isCurrent = coords == m_currentCellCoords;
508011ce 5408
ab79958a 5409 wxRect rect;
7c1cb261
VZ
5410 rect.x = GetColLeft(col);
5411 rect.y = GetRowTop(row);
5412 rect.width = GetColWidth(col) - 1;
5413 rect.height = GetRowHeight(row) - 1;
f85afd4e 5414
189d0213
VZ
5415 // if the editor is shown, we should use it and not the renderer
5416 if ( isCurrent && IsCellEditControlEnabled() )
5417 {
0b190b0f
VZ
5418 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5419 editor->PaintBackground(rect, attr);
5420 editor->DecRef();
189d0213
VZ
5421 }
5422 else
5423 {
5424 // but all the rest is drawn by the cell renderer and hence may be
5425 // customized
0b190b0f
VZ
5426 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
5427 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
5428 renderer->DecRef();
189d0213 5429 }
07296f0b 5430
283b7808
VZ
5431 attr->DecRef();
5432}
07296f0b 5433
283b7808 5434void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
5435{
5436 int row = m_currentCellCoords.GetRow();
5437 int col = m_currentCellCoords.GetCol();
5438
7c1cb261 5439 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
5440 return;
5441
5442 wxRect rect;
7c1cb261
VZ
5443 rect.x = GetColLeft(col);
5444 rect.y = GetRowTop(row);
5445 rect.width = GetColWidth(col) - 1;
5446 rect.height = GetRowHeight(row) - 1;
07296f0b 5447
b3a7510d
VZ
5448 // hmmm... what could we do here to show that the cell is disabled?
5449 // for now, I just draw a thinner border than for the other ones, but
5450 // it doesn't look really good
5451 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
5452 dc.SetBrush(*wxTRANSPARENT_BRUSH);
5453
5454 dc.DrawRectangle(rect);
07296f0b 5455
283b7808 5456#if 0
b3a7510d 5457 // VZ: my experiments with 3d borders...
283b7808 5458
b3a7510d 5459 // how to properly set colours for arbitrary bg?
283b7808
VZ
5460 wxCoord x1 = rect.x,
5461 y1 = rect.y,
00cf1208
RR
5462 x2 = rect.x + rect.width -1,
5463 y2 = rect.y + rect.height -1;
283b7808
VZ
5464
5465 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
5466 dc.DrawLine(x1, y1, x2, y1);
5467 dc.DrawLine(x1, y1, x1, y2);
283b7808 5468
283b7808 5469 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
00cf1208 5470 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
283b7808
VZ
5471
5472 dc.SetPen(*wxBLACK_PEN);
5473 dc.DrawLine(x1, y2, x2, y2);
00cf1208 5474 dc.DrawLine(x2, y1, x2, y2+1);
b3a7510d 5475#endif // 0
2d66e025 5476}
f85afd4e 5477
f2d76237 5478
2d66e025
MB
5479void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5480{
2d66e025
MB
5481 int row = coords.GetRow();
5482 int col = coords.GetCol();
7c1cb261
VZ
5483 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5484 return;
5485
5486 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
60ff3b99 5487
2d66e025
MB
5488 // right hand border
5489 //
7c1cb261
VZ
5490 dc.DrawLine( GetColRight(col), GetRowTop(row),
5491 GetColRight(col), GetRowBottom(row) );
2d66e025
MB
5492
5493 // bottom border
5494 //
7c1cb261
VZ
5495 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
5496 GetColRight(col), GetRowBottom(row) );
f85afd4e
MB
5497}
5498
b3a7510d
VZ
5499void wxGrid::DrawHighlight(wxDC& dc)
5500{
2a7750d9
MB
5501 // This if block was previously in wxGrid::OnPaint but that doesn't
5502 // seem to get called under wxGTK - MB
5503 //
5504 if ( m_currentCellCoords == wxGridNoCellCoords &&
5505 m_numRows && m_numCols )
5506 {
5507 m_currentCellCoords.Set(0, 0);
5508 }
5509
99306db2
VZ
5510 if ( IsCellEditControlEnabled() )
5511 {
5512 // don't show highlight when the edit control is shown
5513 return;
5514 }
5515
b3a7510d
VZ
5516 // if the active cell was repainted, repaint its highlight too because it
5517 // might have been damaged by the grid lines
5518 size_t count = m_cellsExposed.GetCount();
5519 for ( size_t n = 0; n < count; n++ )
5520 {
5521 if ( m_cellsExposed[n] == m_currentCellCoords )
5522 {
5523 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5524 DrawCellHighlight(dc, attr);
5525 attr->DecRef();
5526
5527 break;
5528 }
5529 }
5530}
2d66e025 5531
2d66e025
MB
5532// TODO: remove this ???
5533// This is used to redraw all grid lines e.g. when the grid line colour
5534// has been changed
5535//
c78b3acd 5536void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED_GTK(reg) )
f85afd4e 5537{
60ff3b99
VZ
5538 if ( !m_gridLinesEnabled ||
5539 !m_numRows ||
2d66e025 5540 !m_numCols ) return;
f85afd4e 5541
2d66e025 5542 int top, bottom, left, right;
796df70a 5543
8e217128 5544#ifndef __WXGTK__
508011ce
VZ
5545 if (reg.IsEmpty())
5546 {
796df70a
SN
5547 int cw, ch;
5548 m_gridWin->GetClientSize(&cw, &ch);
5549
5550 // virtual coords of visible area
5551 //
5552 CalcUnscrolledPosition( 0, 0, &left, &top );
5553 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5554 }
508011ce
VZ
5555 else
5556 {
796df70a
SN
5557 wxCoord x, y, w, h;
5558 reg.GetBox(x, y, w, h);
5559 CalcUnscrolledPosition( x, y, &left, &top );
5560 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
5561 }
8e217128
RR
5562#else
5563 int cw, ch;
5564 m_gridWin->GetClientSize(&cw, &ch);
5565 CalcUnscrolledPosition( 0, 0, &left, &top );
5566 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5567#endif
f85afd4e 5568
9496deb5
MB
5569 // avoid drawing grid lines past the last row and col
5570 //
7c1cb261
VZ
5571 right = wxMin( right, GetColRight(m_numCols - 1) );
5572 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 5573
2d66e025 5574 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
f85afd4e 5575
2d66e025 5576 // horizontal grid lines
f85afd4e 5577 //
60ff3b99 5578 int i;
9496deb5 5579 for ( i = 0; i < m_numRows; i++ )
f85afd4e 5580 {
6d55126d 5581 int bot = GetRowBottom(i) - 1;
7c1cb261 5582
6d55126d 5583 if ( bot > bottom )
2d66e025
MB
5584 {
5585 break;
5586 }
7c1cb261 5587
6d55126d 5588 if ( bot >= top )
2d66e025 5589 {
6d55126d 5590 dc.DrawLine( left, bot, right, bot );
2d66e025 5591 }
f85afd4e
MB
5592 }
5593
f85afd4e 5594
2d66e025
MB
5595 // vertical grid lines
5596 //
9496deb5 5597 for ( i = 0; i < m_numCols; i++ )
f85afd4e 5598 {
7c1cb261
VZ
5599 int colRight = GetColRight(i) - 1;
5600 if ( colRight > right )
2d66e025
MB
5601 {
5602 break;
5603 }
7c1cb261
VZ
5604
5605 if ( colRight >= left )
2d66e025 5606 {
7c1cb261 5607 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
5608 }
5609 }
5610}
f85afd4e 5611
8f177c8e 5612
2d66e025
MB
5613void wxGrid::DrawRowLabels( wxDC& dc )
5614{
5615 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5616
2d66e025
MB
5617 size_t i;
5618 size_t numLabels = m_rowLabelsExposed.GetCount();
60ff3b99 5619
2d66e025
MB
5620 for ( i = 0; i < numLabels; i++ )
5621 {
5622 DrawRowLabel( dc, m_rowLabelsExposed[i] );
60ff3b99 5623 }
f85afd4e
MB
5624}
5625
5626
2d66e025 5627void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 5628{
7c1cb261
VZ
5629 if ( GetRowHeight(row) <= 0 )
5630 return;
60ff3b99 5631
7c1cb261
VZ
5632 int rowTop = GetRowTop(row),
5633 rowBottom = GetRowBottom(row) - 1;
b99be8fb 5634
2d66e025 5635 dc.SetPen( *wxBLACK_PEN );
b0d6ff2f 5636 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7c1cb261 5637 m_rowLabelWidth-1, rowBottom );
b99be8fb 5638
7c1cb261 5639 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
b99be8fb 5640
2d66e025 5641 dc.SetPen( *wxWHITE_PEN );
7c1cb261 5642 dc.DrawLine( 0, rowTop, 0, rowBottom );
b0d6ff2f 5643 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
60ff3b99 5644
f85afd4e 5645 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
5646 dc.SetTextForeground( GetLabelTextColour() );
5647 dc.SetFont( GetLabelFont() );
8f177c8e 5648
f85afd4e 5649 int hAlign, vAlign;
2d66e025 5650 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 5651
2d66e025
MB
5652 wxRect rect;
5653 rect.SetX( 2 );
7c1cb261 5654 rect.SetY( GetRowTop(row) + 2 );
2d66e025 5655 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 5656 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 5657 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
5658}
5659
5660
2d66e025 5661void wxGrid::DrawColLabels( wxDC& dc )
f85afd4e 5662{
2d66e025 5663 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5664
2d66e025
MB
5665 size_t i;
5666 size_t numLabels = m_colLabelsExposed.GetCount();
60ff3b99 5667
2d66e025 5668 for ( i = 0; i < numLabels; i++ )
f85afd4e 5669 {
2d66e025 5670 DrawColLabel( dc, m_colLabelsExposed[i] );
60ff3b99 5671 }
f85afd4e
MB
5672}
5673
5674
2d66e025 5675void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 5676{
7c1cb261
VZ
5677 if ( GetColWidth(col) <= 0 )
5678 return;
60ff3b99 5679
7c1cb261
VZ
5680 int colLeft = GetColLeft(col),
5681 colRight = GetColRight(col) - 1;
b99be8fb 5682
2d66e025 5683 dc.SetPen( *wxBLACK_PEN );
7c1cb261
VZ
5684 dc.DrawLine( colRight, 0,
5685 colRight, m_colLabelHeight-1 );
b99be8fb 5686
b0d6ff2f 5687 dc.DrawLine( colLeft, m_colLabelHeight-1,
7c1cb261 5688 colRight, m_colLabelHeight-1 );
b99be8fb 5689
f85afd4e 5690 dc.SetPen( *wxWHITE_PEN );
b0d6ff2f 5691 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
7c1cb261 5692 dc.DrawLine( colLeft, 0, colRight, 0 );
f85afd4e 5693
b0d6ff2f
MB
5694 dc.SetBackgroundMode( wxTRANSPARENT );
5695 dc.SetTextForeground( GetLabelTextColour() );
5696 dc.SetFont( GetLabelFont() );
8f177c8e 5697
f85afd4e 5698 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
5699 dc.SetTextForeground( GetLabelTextColour() );
5700 dc.SetFont( GetLabelFont() );
8f177c8e 5701
f85afd4e 5702 int hAlign, vAlign;
2d66e025 5703 GetColLabelAlignment( &hAlign, &vAlign );
60ff3b99 5704
2d66e025 5705 wxRect rect;
7c1cb261 5706 rect.SetX( colLeft + 2 );
2d66e025 5707 rect.SetY( 2 );
7c1cb261 5708 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 5709 rect.SetHeight( m_colLabelHeight - 4 );
60ff3b99 5710 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
f85afd4e
MB
5711}
5712
5713
2d66e025
MB
5714void wxGrid::DrawTextRectangle( wxDC& dc,
5715 const wxString& value,
5716 const wxRect& rect,
5717 int horizAlign,
5718 int vertAlign )
f85afd4e 5719{
2d66e025
MB
5720 long textWidth, textHeight;
5721 long lineWidth, lineHeight;
5722 wxArrayString lines;
f85afd4e 5723
2d66e025
MB
5724 dc.SetClippingRegion( rect );
5725 StringToLines( value, lines );
5726 if ( lines.GetCount() )
5727 {
5728 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5729 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
f85afd4e 5730
2d66e025
MB
5731 float x, y;
5732 switch ( horizAlign )
5733 {
5734 case wxRIGHT:
5735 x = rect.x + (rect.width - textWidth - 1);
5736 break;
f85afd4e 5737
2d66e025
MB
5738 case wxCENTRE:
5739 x = rect.x + ((rect.width - textWidth)/2);
5740 break;
f85afd4e 5741
2d66e025
MB
5742 case wxLEFT:
5743 default:
5744 x = rect.x + 1;
5745 break;
5746 }
f85afd4e 5747
2d66e025
MB
5748 switch ( vertAlign )
5749 {
5750 case wxBOTTOM:
5751 y = rect.y + (rect.height - textHeight - 1);
5752 break;
f85afd4e
MB
5753
5754 case wxCENTRE:
8f177c8e 5755 y = rect.y + ((rect.height - textHeight)/2);
f85afd4e
MB
5756 break;
5757
5758 case wxTOP:
5759 default:
8f177c8e 5760 y = rect.y + 1;
f85afd4e
MB
5761 break;
5762 }
5763
b540eb2b 5764 for ( size_t i = 0; i < lines.GetCount(); i++ )
f85afd4e 5765 {
4a64bee4 5766 dc.DrawText( lines[i], (int)x, (int)y );
f85afd4e
MB
5767 y += lineHeight;
5768 }
5769 }
8f177c8e 5770
f85afd4e 5771 dc.DestroyClippingRegion();
f85afd4e
MB
5772}
5773
5774
5775// Split multi line text up into an array of strings. Any existing
5776// contents of the string array are preserved.
5777//
5778void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5779{
f85afd4e
MB
5780 int startPos = 0;
5781 int pos;
6d004f67 5782 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 5783 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 5784
2433bb2e 5785 while ( startPos < (int)tVal.Length() )
f85afd4e 5786 {
2433bb2e 5787 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
5788 if ( pos < 0 )
5789 {
5790 break;
5791 }
5792 else if ( pos == 0 )
5793 {
5794 lines.Add( wxEmptyString );
5795 }
5796 else
5797 {
2433bb2e 5798 lines.Add( value.Mid(startPos, pos) );
f85afd4e
MB
5799 }
5800 startPos += pos+1;
5801 }
b540eb2b 5802 if ( startPos < (int)value.Length() )
f85afd4e
MB
5803 {
5804 lines.Add( value.Mid( startPos ) );
5805 }
5806}
5807
5808
5809void wxGrid::GetTextBoxSize( wxDC& dc,
5810 wxArrayString& lines,
5811 long *width, long *height )
5812{
5813 long w = 0;
5814 long h = 0;
5815 long lineW, lineH;
5816
b540eb2b 5817 size_t i;
f85afd4e
MB
5818 for ( i = 0; i < lines.GetCount(); i++ )
5819 {
5820 dc.GetTextExtent( lines[i], &lineW, &lineH );
5821 w = wxMax( w, lineW );
5822 h += lineH;
5823 }
5824
5825 *width = w;
5826 *height = h;
5827}
5828
5829
5830//
2d66e025 5831// ------ Edit control functions
f85afd4e
MB
5832//
5833
2d66e025
MB
5834
5835void wxGrid::EnableEditing( bool edit )
f85afd4e 5836{
2d66e025
MB
5837 // TODO: improve this ?
5838 //
5839 if ( edit != m_editable )
f85afd4e 5840 {
2d66e025 5841 m_editable = edit;
1f1ce288 5842
b54ba671
VZ
5843 // FIXME IMHO this won't disable the edit control if edit == FALSE
5844 // because of the check in the beginning of
5845 // EnableCellEditControl() just below (VZ)
2c9a89e0 5846 EnableCellEditControl(m_editable);
f85afd4e 5847 }
f85afd4e
MB
5848}
5849
5850
2d66e025 5851void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 5852{
2c9a89e0
RD
5853 if (! m_editable)
5854 return;
5855
dcfe4c3d
SN
5856 if ( m_currentCellCoords == wxGridNoCellCoords )
5857 SetCurrentCell( 0, 0 );
60ff3b99 5858
2c9a89e0
RD
5859 if ( enable != m_cellEditCtrlEnabled )
5860 {
b54ba671
VZ
5861 // TODO allow the app to Veto() this event?
5862 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5863
dcfe4c3d 5864 if ( enable )
f85afd4e 5865 {
b54ba671
VZ
5866 // this should be checked by the caller!
5867 wxASSERT_MSG( CanEnableCellControl(),
5868 _T("can't enable editing for this cell!") );
5869
5870 // do it before ShowCellEditControl()
dcfe4c3d 5871 m_cellEditCtrlEnabled = enable;
b54ba671 5872
2d66e025 5873 ShowCellEditControl();
2d66e025
MB
5874 }
5875 else
5876 {
2d66e025
MB
5877 HideCellEditControl();
5878 SaveEditControlValue();
b54ba671
VZ
5879
5880 // do it after HideCellEditControl()
dcfe4c3d 5881 m_cellEditCtrlEnabled = enable;
f85afd4e 5882 }
f85afd4e 5883 }
f85afd4e 5884}
f85afd4e 5885
b54ba671 5886bool wxGrid::IsCurrentCellReadOnly() const
283b7808 5887{
b54ba671
VZ
5888 // const_cast
5889 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5890 bool readonly = attr->IsReadOnly();
5891 attr->DecRef();
283b7808 5892
b54ba671
VZ
5893 return readonly;
5894}
283b7808 5895
b54ba671
VZ
5896bool wxGrid::CanEnableCellControl() const
5897{
5898 return m_editable && !IsCurrentCellReadOnly();
5899}
5900
5901bool wxGrid::IsCellEditControlEnabled() const
5902{
5903 // the cell edit control might be disable for all cells or just for the
5904 // current one if it's read only
5905 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
283b7808
VZ
5906}
5907
2d66e025 5908void wxGrid::ShowCellEditControl()
f85afd4e 5909{
2d66e025 5910 if ( IsCellEditControlEnabled() )
f85afd4e 5911 {
2d66e025
MB
5912 if ( !IsVisible( m_currentCellCoords ) )
5913 {
5914 return;
5915 }
5916 else
5917 {
2c9a89e0
RD
5918 wxRect rect = CellToRect( m_currentCellCoords );
5919 int row = m_currentCellCoords.GetRow();
5920 int col = m_currentCellCoords.GetCol();
2d66e025
MB
5921
5922 // convert to scrolled coords
5923 //
99306db2 5924 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
508011ce 5925
99306db2
VZ
5926 // done in PaintBackground()
5927#if 0
5928 // erase the highlight and the cell contents because the editor
5929 // might not cover the entire cell
5930 wxClientDC dc( m_gridWin );
5931 PrepareDC( dc );
5932 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5933 dc.SetPen(*wxTRANSPARENT_PEN);
5934 dc.DrawRectangle(rect);
5935#endif // 0
d2fdd8d2 5936
99306db2
VZ
5937 // cell is shifted by one pixel
5938 rect.x--;
5939 rect.y--;
f0102d2a 5940
508011ce 5941 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 5942 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
5943 if ( !editor->IsCreated() )
5944 {
2c9a89e0
RD
5945 editor->Create(m_gridWin, -1,
5946 new wxGridCellEditorEvtHandler(this, editor));
2d66e025
MB
5947 }
5948
b0e282b3 5949 editor->Show( TRUE, attr );
0b190b0f 5950
2c9a89e0 5951 editor->SetSize( rect );
99306db2 5952
3da93aae 5953 editor->BeginEdit(row, col, this);
0b190b0f
VZ
5954
5955 editor->DecRef();
2c9a89e0
RD
5956 attr->DecRef();
5957 }
f85afd4e
MB
5958 }
5959}
5960
5961
2d66e025 5962void wxGrid::HideCellEditControl()
f85afd4e 5963{
2d66e025 5964 if ( IsCellEditControlEnabled() )
f85afd4e 5965 {
2c9a89e0
RD
5966 int row = m_currentCellCoords.GetRow();
5967 int col = m_currentCellCoords.GetCol();
5968
3da93aae 5969 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f
VZ
5970 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5971 editor->Show( FALSE );
5972 editor->DecRef();
2c9a89e0
RD
5973 attr->DecRef();
5974 m_gridWin->SetFocus();
f85afd4e 5975 }
2d66e025 5976}
8f177c8e 5977
2d66e025 5978
2d66e025
MB
5979void wxGrid::SaveEditControlValue()
5980{
3da93aae
VZ
5981 if ( IsCellEditControlEnabled() )
5982 {
2c9a89e0
RD
5983 int row = m_currentCellCoords.GetRow();
5984 int col = m_currentCellCoords.GetCol();
8f177c8e 5985
3da93aae 5986 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 5987 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 5988 bool changed = editor->EndEdit(row, col, this);
2d66e025 5989
0b190b0f 5990 editor->DecRef();
2c9a89e0 5991 attr->DecRef();
2d66e025 5992
3da93aae
VZ
5993 if (changed)
5994 {
b54ba671 5995 SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025
MB
5996 m_currentCellCoords.GetRow(),
5997 m_currentCellCoords.GetCol() );
5998 }
f85afd4e
MB
5999 }
6000}
6001
2d66e025
MB
6002
6003//
60ff3b99
VZ
6004// ------ Grid location functions
6005// Note that all of these functions work with the logical coordinates of
2d66e025
MB
6006// grid cells and labels so you will need to convert from device
6007// coordinates for mouse events etc.
6008//
6009
6010void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 6011{
58dd5b3b
MB
6012 int row = YToRow(y);
6013 int col = XToCol(x);
6014
6015 if ( row == -1 || col == -1 )
6016 {
6017 coords = wxGridNoCellCoords;
6018 }
6019 else
6020 {
6021 coords.Set( row, col );
6022 }
2d66e025 6023}
f85afd4e 6024
8f177c8e 6025
2d66e025
MB
6026int wxGrid::YToRow( int y )
6027{
6028 int i;
8f177c8e 6029
2d66e025 6030 for ( i = 0; i < m_numRows; i++ )
f85afd4e 6031 {
7c1cb261
VZ
6032 if ( y < GetRowBottom(i) )
6033 return i;
f85afd4e 6034 }
2d66e025 6035
4ee5fc9c 6036 return -1;
f85afd4e
MB
6037}
6038
2d66e025
MB
6039
6040int wxGrid::XToCol( int x )
f85afd4e 6041{
2d66e025 6042 int i;
8f177c8e 6043
2d66e025 6044 for ( i = 0; i < m_numCols; i++ )
f85afd4e 6045 {
7c1cb261
VZ
6046 if ( x < GetColRight(i) )
6047 return i;
f85afd4e
MB
6048 }
6049
4ee5fc9c 6050 return -1;
2d66e025 6051}
8f177c8e 6052
2d66e025
MB
6053
6054// return the row number that that the y coord is near the edge of, or
6055// -1 if not near an edge
6056//
6057int wxGrid::YToEdgeOfRow( int y )
6058{
6059 int i, d;
6060
6061 for ( i = 0; i < m_numRows; i++ )
6062 {
7c1cb261 6063 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 6064 {
7c1cb261
VZ
6065 d = abs( y - GetRowBottom(i) );
6066 if ( d < WXGRID_LABEL_EDGE_ZONE )
6067 return i;
f85afd4e 6068 }
f85afd4e 6069 }
2d66e025
MB
6070
6071 return -1;
6072}
6073
6074
6075// return the col number that that the x coord is near the edge of, or
6076// -1 if not near an edge
6077//
6078int wxGrid::XToEdgeOfCol( int x )
6079{
6080 int i, d;
6081
6082 for ( i = 0; i < m_numCols; i++ )
f85afd4e 6083 {
7c1cb261 6084 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 6085 {
7c1cb261
VZ
6086 d = abs( x - GetColRight(i) );
6087 if ( d < WXGRID_LABEL_EDGE_ZONE )
6088 return i;
2d66e025 6089 }
f85afd4e 6090 }
2d66e025
MB
6091
6092 return -1;
f85afd4e
MB
6093}
6094
2d66e025
MB
6095
6096wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 6097{
2d66e025 6098 wxRect rect( -1, -1, -1, -1 );
f85afd4e 6099
2d66e025
MB
6100 if ( row >= 0 && row < m_numRows &&
6101 col >= 0 && col < m_numCols )
f85afd4e 6102 {
7c1cb261
VZ
6103 rect.x = GetColLeft(col);
6104 rect.y = GetRowTop(row);
6105 rect.width = GetColWidth(col);
6106 rect.height = GetRowHeight(row);
f85afd4e
MB
6107 }
6108
2d66e025
MB
6109 return rect;
6110}
6111
6112
6113bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
6114{
6115 // get the cell rectangle in logical coords
6116 //
6117 wxRect r( CellToRect( row, col ) );
60ff3b99 6118
2d66e025
MB
6119 // convert to device coords
6120 //
6121 int left, top, right, bottom;
6122 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6123 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6124
2d66e025
MB
6125 // check against the client area of the grid window
6126 //
6127 int cw, ch;
6128 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6129
2d66e025 6130 if ( wholeCellVisible )
f85afd4e 6131 {
2d66e025 6132 // is the cell wholly visible ?
8f177c8e 6133 //
2d66e025
MB
6134 return ( left >= 0 && right <= cw &&
6135 top >= 0 && bottom <= ch );
6136 }
6137 else
6138 {
6139 // is the cell partly visible ?
6140 //
6141 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
6142 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
6143 }
6144}
6145
6146
6147// make the specified cell location visible by doing a minimal amount
6148// of scrolling
6149//
6150void wxGrid::MakeCellVisible( int row, int col )
6151{
6152 int i;
60ff3b99 6153 int xpos = -1, ypos = -1;
2d66e025
MB
6154
6155 if ( row >= 0 && row < m_numRows &&
6156 col >= 0 && col < m_numCols )
6157 {
6158 // get the cell rectangle in logical coords
6159 //
6160 wxRect r( CellToRect( row, col ) );
60ff3b99 6161
2d66e025
MB
6162 // convert to device coords
6163 //
6164 int left, top, right, bottom;
6165 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6166 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6167
2d66e025
MB
6168 int cw, ch;
6169 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6170
2d66e025 6171 if ( top < 0 )
3f296516 6172 {
2d66e025 6173 ypos = r.GetTop();
3f296516 6174 }
2d66e025
MB
6175 else if ( bottom > ch )
6176 {
6177 int h = r.GetHeight();
6178 ypos = r.GetTop();
6179 for ( i = row-1; i >= 0; i-- )
6180 {
7c1cb261
VZ
6181 int rowHeight = GetRowHeight(i);
6182 if ( h + rowHeight > ch )
6183 break;
2d66e025 6184
7c1cb261
VZ
6185 h += rowHeight;
6186 ypos -= rowHeight;
2d66e025 6187 }
f0102d2a
VZ
6188
6189 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6190 // have rounding errors (this is important, because if we do, we
6191 // might not scroll at all and some cells won't be redrawn)
6192 ypos += GRID_SCROLL_LINE / 2;
2d66e025
MB
6193 }
6194
6195 if ( left < 0 )
6196 {
6197 xpos = r.GetLeft();
6198 }
6199 else if ( right > cw )
6200 {
6201 int w = r.GetWidth();
6202 xpos = r.GetLeft();
6203 for ( i = col-1; i >= 0; i-- )
6204 {
7c1cb261
VZ
6205 int colWidth = GetColWidth(i);
6206 if ( w + colWidth > cw )
6207 break;
2d66e025 6208
7c1cb261
VZ
6209 w += colWidth;
6210 xpos -= colWidth;
2d66e025 6211 }
f0102d2a
VZ
6212
6213 // see comment for ypos above
6214 xpos += GRID_SCROLL_LINE / 2;
2d66e025
MB
6215 }
6216
6217 if ( xpos != -1 || ypos != -1 )
6218 {
f0102d2a
VZ
6219 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
6220 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
2d66e025
MB
6221 Scroll( xpos, ypos );
6222 AdjustScrollbars();
6223 }
6224 }
6225}
6226
6227
6228//
6229// ------ Grid cursor movement functions
6230//
6231
5c8fc7c1 6232bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025
MB
6233{
6234 if ( m_currentCellCoords != wxGridNoCellCoords &&
6235 m_currentCellCoords.GetRow() > 0 )
6236 {
5c8fc7c1 6237 if ( expandSelection )
d95b0c2b
SN
6238 {
6239 if ( m_selectingKeyboard == wxGridNoCellCoords )
6240 m_selectingKeyboard = m_currentCellCoords;
6241 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
aa5e1f75
SN
6242 MakeCellVisible( m_selectingKeyboard.GetRow(),
6243 m_selectingKeyboard.GetCol() );
d95b0c2b
SN
6244 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6245 }
6246 else
aa5e1f75
SN
6247 {
6248 ClearSelection();
6249 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
6250 m_currentCellCoords.GetCol() );
d95b0c2b
SN
6251 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
6252 m_currentCellCoords.GetCol() );
aa5e1f75 6253 }
8f177c8e 6254 return TRUE;
f85afd4e 6255 }
2d66e025
MB
6256
6257 return FALSE;
6258}
6259
6260
5c8fc7c1 6261bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025 6262{
2d66e025
MB
6263 if ( m_currentCellCoords != wxGridNoCellCoords &&
6264 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 6265 {
5c8fc7c1 6266 if ( expandSelection )
d95b0c2b
SN
6267 {
6268 if ( m_selectingKeyboard == wxGridNoCellCoords )
6269 m_selectingKeyboard = m_currentCellCoords;
6270 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
aa5e1f75
SN
6271 MakeCellVisible( m_selectingKeyboard.GetRow(),
6272 m_selectingKeyboard.GetCol() );
d95b0c2b
SN
6273 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6274 }
6275 else
aa5e1f75
SN
6276 {
6277 ClearSelection();
6278 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
6279 m_currentCellCoords.GetCol() );
d95b0c2b
SN
6280 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
6281 m_currentCellCoords.GetCol() );
aa5e1f75 6282 }
2d66e025 6283 return TRUE;
f85afd4e 6284 }
2d66e025
MB
6285
6286 return FALSE;
f85afd4e
MB
6287}
6288
2d66e025 6289
5c8fc7c1 6290bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 6291{
2d66e025
MB
6292 if ( m_currentCellCoords != wxGridNoCellCoords &&
6293 m_currentCellCoords.GetCol() > 0 )
6294 {
5c8fc7c1 6295 if ( expandSelection )
d95b0c2b
SN
6296 {
6297 if ( m_selectingKeyboard == wxGridNoCellCoords )
6298 m_selectingKeyboard = m_currentCellCoords;
6299 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
aa5e1f75
SN
6300 MakeCellVisible( m_selectingKeyboard.GetRow(),
6301 m_selectingKeyboard.GetCol() );
d95b0c2b
SN
6302 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6303 }
6304 else
aa5e1f75
SN
6305 {
6306 ClearSelection();
6307 MakeCellVisible( m_currentCellCoords.GetRow(),
6308 m_currentCellCoords.GetCol() - 1 );
d95b0c2b
SN
6309 SetCurrentCell( m_currentCellCoords.GetRow(),
6310 m_currentCellCoords.GetCol() - 1 );
aa5e1f75 6311 }
2d66e025
MB
6312 return TRUE;
6313 }
6314
6315 return FALSE;
6316}
6317
6318
5c8fc7c1 6319bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025
MB
6320{
6321 if ( m_currentCellCoords != wxGridNoCellCoords &&
6322 m_currentCellCoords.GetCol() < m_numCols - 1 )
f85afd4e 6323 {
5c8fc7c1 6324 if ( expandSelection )
d95b0c2b
SN
6325 {
6326 if ( m_selectingKeyboard == wxGridNoCellCoords )
6327 m_selectingKeyboard = m_currentCellCoords;
6328 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
aa5e1f75
SN
6329 MakeCellVisible( m_selectingKeyboard.GetRow(),
6330 m_selectingKeyboard.GetCol() );
d95b0c2b
SN
6331 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6332 }
6333 else
aa5e1f75
SN
6334 {
6335 ClearSelection();
6336 MakeCellVisible( m_currentCellCoords.GetRow(),
6337 m_currentCellCoords.GetCol() + 1 );
6338 SetCurrentCell( m_currentCellCoords.GetRow(),
d95b0c2b 6339 m_currentCellCoords.GetCol() + 1 );
aa5e1f75 6340 }
2d66e025 6341 return TRUE;
f85afd4e 6342 }
8f177c8e 6343
2d66e025
MB
6344 return FALSE;
6345}
6346
6347
6348bool wxGrid::MovePageUp()
6349{
6350 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 6351
2d66e025
MB
6352 int row = m_currentCellCoords.GetRow();
6353 if ( row > 0 )
f85afd4e 6354 {
2d66e025
MB
6355 int cw, ch;
6356 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6357
7c1cb261 6358 int y = GetRowTop(row);
2d66e025
MB
6359 int newRow = YToRow( y - ch + 1 );
6360 if ( newRow == -1 )
6361 {
6362 newRow = 0;
6363 }
60ff3b99 6364 else if ( newRow == row )
2d66e025
MB
6365 {
6366 newRow = row - 1;
6367 }
8f177c8e 6368
2d66e025
MB
6369 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
6370 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 6371
f85afd4e
MB
6372 return TRUE;
6373 }
2d66e025
MB
6374
6375 return FALSE;
6376}
6377
6378bool wxGrid::MovePageDown()
6379{
6380 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 6381
2d66e025
MB
6382 int row = m_currentCellCoords.GetRow();
6383 if ( row < m_numRows )
f85afd4e 6384 {
2d66e025
MB
6385 int cw, ch;
6386 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6387
7c1cb261 6388 int y = GetRowTop(row);
2d66e025
MB
6389 int newRow = YToRow( y + ch );
6390 if ( newRow == -1 )
6391 {
6392 newRow = m_numRows - 1;
6393 }
60ff3b99 6394 else if ( newRow == row )
2d66e025
MB
6395 {
6396 newRow = row + 1;
6397 }
6398
6399 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
6400 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 6401
2d66e025 6402 return TRUE;
f85afd4e 6403 }
2d66e025
MB
6404
6405 return FALSE;
f85afd4e 6406}
8f177c8e 6407
5c8fc7c1 6408bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
6409{
6410 if ( m_table &&
6411 m_currentCellCoords != wxGridNoCellCoords &&
6412 m_currentCellCoords.GetRow() > 0 )
6413 {
6414 int row = m_currentCellCoords.GetRow();
6415 int col = m_currentCellCoords.GetCol();
6416
6417 if ( m_table->IsEmptyCell(row, col) )
6418 {
6419 // starting in an empty cell: find the next block of
6420 // non-empty cells
6421 //
6422 while ( row > 0 )
6423 {
6424 row-- ;
6425 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6426 }
6427 }
6428 else if ( m_table->IsEmptyCell(row-1, col) )
6429 {
6430 // starting at the top of a block: find the next block
6431 //
6432 row--;
6433 while ( row > 0 )
6434 {
6435 row-- ;
6436 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6437 }
6438 }
6439 else
6440 {
6441 // starting within a block: find the top of the block
6442 //
6443 while ( row > 0 )
6444 {
6445 row-- ;
6446 if ( m_table->IsEmptyCell(row, col) )
6447 {
6448 row++ ;
6449 break;
6450 }
6451 }
6452 }
f85afd4e 6453
2d66e025 6454 MakeCellVisible( row, col );
5c8fc7c1 6455 if ( expandSelection )
d95b0c2b
SN
6456 {
6457 m_selectingKeyboard = wxGridCellCoords( row, col );
6458 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6459 }
6460 else
aa5e1f75
SN
6461 {
6462 ClearSelection();
6463 SetCurrentCell( row, col );
6464 }
2d66e025
MB
6465 return TRUE;
6466 }
f85afd4e 6467
2d66e025
MB
6468 return FALSE;
6469}
f85afd4e 6470
5c8fc7c1 6471bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 6472{
2d66e025
MB
6473 if ( m_table &&
6474 m_currentCellCoords != wxGridNoCellCoords &&
6475 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 6476 {
2d66e025
MB
6477 int row = m_currentCellCoords.GetRow();
6478 int col = m_currentCellCoords.GetCol();
6479
6480 if ( m_table->IsEmptyCell(row, col) )
6481 {
6482 // starting in an empty cell: find the next block of
6483 // non-empty cells
6484 //
6485 while ( row < m_numRows-1 )
6486 {
6487 row++ ;
6488 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6489 }
6490 }
6491 else if ( m_table->IsEmptyCell(row+1, col) )
6492 {
6493 // starting at the bottom of a block: find the next block
6494 //
6495 row++;
6496 while ( row < m_numRows-1 )
6497 {
6498 row++ ;
6499 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6500 }
6501 }
6502 else
6503 {
6504 // starting within a block: find the bottom of the block
6505 //
6506 while ( row < m_numRows-1 )
6507 {
6508 row++ ;
6509 if ( m_table->IsEmptyCell(row, col) )
6510 {
6511 row-- ;
6512 break;
6513 }
6514 }
6515 }
6516
6517 MakeCellVisible( row, col );
5c8fc7c1 6518 if ( expandSelection )
d95b0c2b
SN
6519 {
6520 m_selectingKeyboard = wxGridCellCoords( row, col );
6521 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6522 }
6523 else
aa5e1f75
SN
6524 {
6525 ClearSelection();
6526 SetCurrentCell( row, col );
6527 }
2d66e025
MB
6528
6529 return TRUE;
f85afd4e 6530 }
f85afd4e 6531
2d66e025
MB
6532 return FALSE;
6533}
f85afd4e 6534
5c8fc7c1 6535bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 6536{
2d66e025
MB
6537 if ( m_table &&
6538 m_currentCellCoords != wxGridNoCellCoords &&
6539 m_currentCellCoords.GetCol() > 0 )
f85afd4e 6540 {
2d66e025
MB
6541 int row = m_currentCellCoords.GetRow();
6542 int col = m_currentCellCoords.GetCol();
6543
6544 if ( m_table->IsEmptyCell(row, col) )
6545 {
6546 // starting in an empty cell: find the next block of
6547 // non-empty cells
6548 //
6549 while ( col > 0 )
6550 {
6551 col-- ;
6552 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6553 }
6554 }
6555 else if ( m_table->IsEmptyCell(row, col-1) )
6556 {
6557 // starting at the left of a block: find the next block
6558 //
6559 col--;
6560 while ( col > 0 )
6561 {
6562 col-- ;
6563 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6564 }
6565 }
6566 else
6567 {
6568 // starting within a block: find the left of the block
6569 //
6570 while ( col > 0 )
6571 {
6572 col-- ;
6573 if ( m_table->IsEmptyCell(row, col) )
6574 {
6575 col++ ;
6576 break;
6577 }
6578 }
6579 }
f85afd4e 6580
2d66e025 6581 MakeCellVisible( row, col );
5c8fc7c1 6582 if ( expandSelection )
d95b0c2b
SN
6583 {
6584 m_selectingKeyboard = wxGridCellCoords( row, col );
6585 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6586 }
6587 else
aa5e1f75
SN
6588 {
6589 ClearSelection();
6590 SetCurrentCell( row, col );
6591 }
8f177c8e 6592
2d66e025 6593 return TRUE;
f85afd4e 6594 }
2d66e025
MB
6595
6596 return FALSE;
f85afd4e
MB
6597}
6598
5c8fc7c1 6599bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 6600{
2d66e025
MB
6601 if ( m_table &&
6602 m_currentCellCoords != wxGridNoCellCoords &&
6603 m_currentCellCoords.GetCol() < m_numCols-1 )
f85afd4e 6604 {
2d66e025
MB
6605 int row = m_currentCellCoords.GetRow();
6606 int col = m_currentCellCoords.GetCol();
8f177c8e 6607
2d66e025
MB
6608 if ( m_table->IsEmptyCell(row, col) )
6609 {
6610 // starting in an empty cell: find the next block of
6611 // non-empty cells
6612 //
6613 while ( col < m_numCols-1 )
6614 {
6615 col++ ;
6616 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6617 }
6618 }
6619 else if ( m_table->IsEmptyCell(row, col+1) )
6620 {
6621 // starting at the right of a block: find the next block
6622 //
6623 col++;
6624 while ( col < m_numCols-1 )
6625 {
6626 col++ ;
6627 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6628 }
6629 }
6630 else
6631 {
6632 // starting within a block: find the right of the block
6633 //
6634 while ( col < m_numCols-1 )
6635 {
6636 col++ ;
6637 if ( m_table->IsEmptyCell(row, col) )
6638 {
6639 col-- ;
6640 break;
6641 }
6642 }
6643 }
8f177c8e 6644
2d66e025 6645 MakeCellVisible( row, col );
5c8fc7c1 6646 if ( expandSelection )
d95b0c2b
SN
6647 {
6648 m_selectingKeyboard = wxGridCellCoords( row, col );
6649 SelectBlock( m_currentCellCoords, m_selectingKeyboard );
6650 }
6651 else
aa5e1f75
SN
6652 {
6653 ClearSelection();
6654 SetCurrentCell( row, col );
6655 }
f85afd4e 6656
2d66e025 6657 return TRUE;
f85afd4e 6658 }
2d66e025
MB
6659
6660 return FALSE;
f85afd4e
MB
6661}
6662
6663
2d66e025 6664
f85afd4e 6665//
2d66e025 6666// ------ Label values and formatting
f85afd4e
MB
6667//
6668
6669void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
6670{
6671 *horiz = m_rowLabelHorizAlign;
6672 *vert = m_rowLabelVertAlign;
6673}
6674
6675void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
6676{
6677 *horiz = m_colLabelHorizAlign;
6678 *vert = m_colLabelVertAlign;
6679}
6680
6681wxString wxGrid::GetRowLabelValue( int row )
6682{
6683 if ( m_table )
6684 {
6685 return m_table->GetRowLabelValue( row );
6686 }
6687 else
6688 {
6689 wxString s;
6690 s << row;
6691 return s;
6692 }
6693}
6694
6695wxString wxGrid::GetColLabelValue( int col )
6696{
6697 if ( m_table )
6698 {
6699 return m_table->GetColLabelValue( col );
6700 }
6701 else
6702 {
6703 wxString s;
6704 s << col;
6705 return s;
6706 }
6707}
6708
2e8cd977 6709
f85afd4e
MB
6710void wxGrid::SetRowLabelSize( int width )
6711{
2e8cd977
MB
6712 width = wxMax( width, 0 );
6713 if ( width != m_rowLabelWidth )
6714 {
2e8cd977
MB
6715 if ( width == 0 )
6716 {
6717 m_rowLabelWin->Show( FALSE );
7807d81c 6718 m_cornerLabelWin->Show( FALSE );
2e8cd977 6719 }
7807d81c 6720 else if ( m_rowLabelWidth == 0 )
2e8cd977 6721 {
7807d81c
MB
6722 m_rowLabelWin->Show( TRUE );
6723 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 6724 }
b99be8fb 6725
2e8cd977 6726 m_rowLabelWidth = width;
7807d81c
MB
6727 CalcWindowSizes();
6728 Refresh( TRUE );
2e8cd977 6729 }
f85afd4e
MB
6730}
6731
2e8cd977 6732
f85afd4e
MB
6733void wxGrid::SetColLabelSize( int height )
6734{
7807d81c 6735 height = wxMax( height, 0 );
2e8cd977
MB
6736 if ( height != m_colLabelHeight )
6737 {
2e8cd977
MB
6738 if ( height == 0 )
6739 {
2e8cd977 6740 m_colLabelWin->Show( FALSE );
7807d81c 6741 m_cornerLabelWin->Show( FALSE );
2e8cd977 6742 }
7807d81c 6743 else if ( m_colLabelHeight == 0 )
2e8cd977 6744 {
7807d81c
MB
6745 m_colLabelWin->Show( TRUE );
6746 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 6747 }
7807d81c 6748
2e8cd977 6749 m_colLabelHeight = height;
7807d81c
MB
6750 CalcWindowSizes();
6751 Refresh( TRUE );
2e8cd977 6752 }
f85afd4e
MB
6753}
6754
2e8cd977 6755
f85afd4e
MB
6756void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6757{
2d66e025
MB
6758 if ( m_labelBackgroundColour != colour )
6759 {
6760 m_labelBackgroundColour = colour;
6761 m_rowLabelWin->SetBackgroundColour( colour );
6762 m_colLabelWin->SetBackgroundColour( colour );
6763 m_cornerLabelWin->SetBackgroundColour( colour );
6764
6765 if ( !GetBatchCount() )
6766 {
6767 m_rowLabelWin->Refresh();
6768 m_colLabelWin->Refresh();
6769 m_cornerLabelWin->Refresh();
6770 }
6771 }
f85afd4e
MB
6772}
6773
6774void wxGrid::SetLabelTextColour( const wxColour& colour )
6775{
2d66e025
MB
6776 if ( m_labelTextColour != colour )
6777 {
6778 m_labelTextColour = colour;
6779 if ( !GetBatchCount() )
6780 {
6781 m_rowLabelWin->Refresh();
6782 m_colLabelWin->Refresh();
6783 }
6784 }
f85afd4e
MB
6785}
6786
6787void wxGrid::SetLabelFont( const wxFont& font )
6788{
6789 m_labelFont = font;
2d66e025
MB
6790 if ( !GetBatchCount() )
6791 {
6792 m_rowLabelWin->Refresh();
6793 m_colLabelWin->Refresh();
6794 }
f85afd4e
MB
6795}
6796
6797void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6798{
6799 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6800 {
6801 m_rowLabelHorizAlign = horiz;
6802 }
8f177c8e 6803
f85afd4e
MB
6804 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6805 {
6806 m_rowLabelVertAlign = vert;
6807 }
6808
2d66e025
MB
6809 if ( !GetBatchCount() )
6810 {
6811 m_rowLabelWin->Refresh();
60ff3b99 6812 }
f85afd4e
MB
6813}
6814
6815void wxGrid::SetColLabelAlignment( int horiz, int vert )
6816{
6817 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6818 {
6819 m_colLabelHorizAlign = horiz;
6820 }
8f177c8e 6821
f85afd4e
MB
6822 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6823 {
6824 m_colLabelVertAlign = vert;
6825 }
6826
2d66e025
MB
6827 if ( !GetBatchCount() )
6828 {
2d66e025 6829 m_colLabelWin->Refresh();
60ff3b99 6830 }
f85afd4e
MB
6831}
6832
6833void wxGrid::SetRowLabelValue( int row, const wxString& s )
6834{
6835 if ( m_table )
6836 {
6837 m_table->SetRowLabelValue( row, s );
2d66e025
MB
6838 if ( !GetBatchCount() )
6839 {
70c7a608
SN
6840 wxRect rect = CellToRect( row, 0);
6841 if ( rect.height > 0 )
6842 {
cb309039 6843 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
70c7a608
SN
6844 rect.x = m_left;
6845 rect.width = m_rowLabelWidth;
6846 m_rowLabelWin->Refresh( TRUE, &rect );
6847 }
2d66e025 6848 }
f85afd4e
MB
6849 }
6850}
6851
6852void wxGrid::SetColLabelValue( int col, const wxString& s )
6853{
6854 if ( m_table )
6855 {
6856 m_table->SetColLabelValue( col, s );
2d66e025
MB
6857 if ( !GetBatchCount() )
6858 {
70c7a608
SN
6859 wxRect rect = CellToRect( 0, col );
6860 if ( rect.width > 0 )
6861 {
cb309039 6862 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
70c7a608
SN
6863 rect.y = m_top;
6864 rect.height = m_colLabelHeight;
6865 m_colLabelWin->Refresh( TRUE, &rect );
6866 }
2d66e025 6867 }
f85afd4e
MB
6868 }
6869}
6870
6871void wxGrid::SetGridLineColour( const wxColour& colour )
6872{
2d66e025
MB
6873 if ( m_gridLineColour != colour )
6874 {
6875 m_gridLineColour = colour;
60ff3b99 6876
2d66e025
MB
6877 wxClientDC dc( m_gridWin );
6878 PrepareDC( dc );
c6a51dcd 6879 DrawAllGridLines( dc, wxRegion() );
2d66e025 6880 }
f85afd4e
MB
6881}
6882
6883void wxGrid::EnableGridLines( bool enable )
6884{
6885 if ( enable != m_gridLinesEnabled )
6886 {
6887 m_gridLinesEnabled = enable;
2d66e025
MB
6888
6889 if ( !GetBatchCount() )
6890 {
6891 if ( enable )
6892 {
6893 wxClientDC dc( m_gridWin );
6894 PrepareDC( dc );
c6a51dcd 6895 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
6896 }
6897 else
6898 {
6899 m_gridWin->Refresh();
6900 }
6901 }
f85afd4e
MB
6902 }
6903}
6904
6905
6906int wxGrid::GetDefaultRowSize()
6907{
6908 return m_defaultRowHeight;
6909}
6910
6911int wxGrid::GetRowSize( int row )
6912{
b99be8fb
VZ
6913 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
6914
7c1cb261 6915 return GetRowHeight(row);
f85afd4e
MB
6916}
6917
6918int wxGrid::GetDefaultColSize()
6919{
6920 return m_defaultColWidth;
6921}
6922
6923int wxGrid::GetColSize( int col )
6924{
b99be8fb
VZ
6925 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
6926
7c1cb261 6927 return GetColWidth(col);
f85afd4e
MB
6928}
6929
2e9a6788
VZ
6930// ============================================================================
6931// access to the grid attributes: each of them has a default value in the grid
6932// itself and may be overidden on a per-cell basis
6933// ============================================================================
6934
6935// ----------------------------------------------------------------------------
6936// setting default attributes
6937// ----------------------------------------------------------------------------
6938
6939void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6940{
2796cce3 6941 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
6942#ifdef __WXGTK__
6943 m_gridWin->SetBackgroundColour(col);
6944#endif
2e9a6788
VZ
6945}
6946
6947void wxGrid::SetDefaultCellTextColour( const wxColour& col )
6948{
2796cce3 6949 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
6950}
6951
6952void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
6953{
2796cce3 6954 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
6955}
6956
6957void wxGrid::SetDefaultCellFont( const wxFont& font )
6958{
2796cce3
RD
6959 m_defaultCellAttr->SetFont(font);
6960}
6961
0ba143c9
RD
6962void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
6963{
6964 m_defaultCellAttr->SetRenderer(renderer);
6965}
2e9a6788 6966
0ba143c9
RD
6967void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
6968{
6969 m_defaultCellAttr->SetEditor(editor);
6970}
9b4aede2 6971
2e9a6788
VZ
6972// ----------------------------------------------------------------------------
6973// access to the default attrbiutes
6974// ----------------------------------------------------------------------------
6975
f85afd4e
MB
6976wxColour wxGrid::GetDefaultCellBackgroundColour()
6977{
2796cce3 6978 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
6979}
6980
2e9a6788
VZ
6981wxColour wxGrid::GetDefaultCellTextColour()
6982{
2796cce3 6983 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
6984}
6985
6986wxFont wxGrid::GetDefaultCellFont()
6987{
2796cce3 6988 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
6989}
6990
6991void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
6992{
2796cce3 6993 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
6994}
6995
0ba143c9
RD
6996wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
6997{
0b190b0f 6998 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 6999}
ab79958a 7000
0ba143c9
RD
7001wxGridCellEditor *wxGrid::GetDefaultEditor() const
7002{
28a77bc4 7003 return m_defaultCellAttr->GetEditor(NULL,0,0);
0ba143c9 7004}
9b4aede2 7005
2e9a6788
VZ
7006// ----------------------------------------------------------------------------
7007// access to cell attributes
7008// ----------------------------------------------------------------------------
7009
b99be8fb 7010wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 7011{
0a976765 7012 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7013 wxColour colour = attr->GetBackgroundColour();
39bcce60 7014 attr->DecRef();
b99be8fb 7015 return colour;
f85afd4e
MB
7016}
7017
b99be8fb 7018wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 7019{
0a976765 7020 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7021 wxColour colour = attr->GetTextColour();
39bcce60 7022 attr->DecRef();
b99be8fb 7023 return colour;
f85afd4e
MB
7024}
7025
b99be8fb 7026wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 7027{
0a976765 7028 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7029 wxFont font = attr->GetFont();
39bcce60 7030 attr->DecRef();
b99be8fb 7031 return font;
f85afd4e
MB
7032}
7033
b99be8fb 7034void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 7035{
0a976765 7036 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7037 attr->GetAlignment(horiz, vert);
39bcce60 7038 attr->DecRef();
2e9a6788
VZ
7039}
7040
2796cce3
RD
7041wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
7042{
7043 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7044 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 7045 attr->DecRef();
0b190b0f 7046
2796cce3
RD
7047 return renderer;
7048}
7049
9b4aede2
RD
7050wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
7051{
7052 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7053 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 7054 attr->DecRef();
0b190b0f 7055
9b4aede2
RD
7056 return editor;
7057}
7058
283b7808
VZ
7059bool wxGrid::IsReadOnly(int row, int col) const
7060{
7061 wxGridCellAttr* attr = GetCellAttr(row, col);
7062 bool isReadOnly = attr->IsReadOnly();
7063 attr->DecRef();
7064 return isReadOnly;
7065}
7066
2e9a6788 7067// ----------------------------------------------------------------------------
758cbedf 7068// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
7069// ----------------------------------------------------------------------------
7070
7071bool wxGrid::CanHaveAttributes()
7072{
7073 if ( !m_table )
7074 {
7075 return FALSE;
7076 }
7077
f2d76237 7078 return m_table->CanHaveAttributes();
2e9a6788
VZ
7079}
7080
0a976765
VZ
7081void wxGrid::ClearAttrCache()
7082{
7083 if ( m_attrCache.row != -1 )
7084 {
39bcce60 7085 wxSafeDecRef(m_attrCache.attr);
0a976765
VZ
7086 m_attrCache.row = -1;
7087 }
7088}
7089
7090void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
7091{
7092 wxGrid *self = (wxGrid *)this; // const_cast
7093
7094 self->ClearAttrCache();
7095 self->m_attrCache.row = row;
7096 self->m_attrCache.col = col;
7097 self->m_attrCache.attr = attr;
39bcce60 7098 wxSafeIncRef(attr);
0a976765
VZ
7099}
7100
7101bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
7102{
7103 if ( row == m_attrCache.row && col == m_attrCache.col )
7104 {
7105 *attr = m_attrCache.attr;
39bcce60 7106 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
7107
7108#ifdef DEBUG_ATTR_CACHE
7109 gs_nAttrCacheHits++;
7110#endif
7111
7112 return TRUE;
7113 }
7114 else
7115 {
7116#ifdef DEBUG_ATTR_CACHE
7117 gs_nAttrCacheMisses++;
7118#endif
7119 return FALSE;
7120 }
7121}
7122
2e9a6788
VZ
7123wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
7124{
0a976765
VZ
7125 wxGridCellAttr *attr;
7126 if ( !LookupAttr(row, col, &attr) )
2e9a6788 7127 {
0a976765
VZ
7128 attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
7129 CacheAttr(row, col, attr);
7130 }
508011ce
VZ
7131 if (attr)
7132 {
2796cce3 7133 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
7134 }
7135 else
7136 {
2796cce3
RD
7137 attr = m_defaultCellAttr;
7138 attr->IncRef();
7139 }
2e9a6788 7140
0a976765
VZ
7141 return attr;
7142}
7143
7144wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
7145{
7146 wxGridCellAttr *attr;
7147 if ( !LookupAttr(row, col, &attr) || !attr )
7148 {
7149 wxASSERT_MSG( m_table,
7150 _T("we may only be called if CanHaveAttributes() "
7151 "returned TRUE and then m_table should be !NULL") );
7152
7153 attr = m_table->GetAttr(row, col);
7154 if ( !attr )
7155 {
7156 attr = new wxGridCellAttr;
7157
7158 // artificially inc the ref count to match DecRef() in caller
7159 attr->IncRef();
7160
7161 m_table->SetAttr(attr, row, col);
7162 }
2e9a6788 7163
0a976765 7164 CacheAttr(row, col, attr);
2e9a6788 7165 }
2796cce3 7166 attr->SetDefAttr(m_defaultCellAttr);
2e9a6788
VZ
7167 return attr;
7168}
7169
0b190b0f
VZ
7170// ----------------------------------------------------------------------------
7171// setting column attributes (wrappers around SetColAttr)
7172// ----------------------------------------------------------------------------
7173
7174void wxGrid::SetColFormatBool(int col)
7175{
7176 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
7177}
7178
7179void wxGrid::SetColFormatNumber(int col)
7180{
7181 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
7182}
7183
7184void wxGrid::SetColFormatFloat(int col, int width, int precision)
7185{
7186 wxString typeName = wxGRID_VALUE_FLOAT;
7187 if ( (width != -1) || (precision != -1) )
7188 {
7189 typeName << _T(':') << width << _T(',') << precision;
7190 }
7191
7192 SetColFormatCustom(col, typeName);
7193}
7194
7195void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
7196{
7197 wxGridCellAttr *attr = new wxGridCellAttr;
7198 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
7199 attr->SetRenderer(renderer);
7200
7201 SetColAttr(col, attr);
7202}
7203
758cbedf
VZ
7204// ----------------------------------------------------------------------------
7205// setting cell attributes: this is forwarded to the table
7206// ----------------------------------------------------------------------------
7207
7208void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
7209{
7210 if ( CanHaveAttributes() )
7211 {
7212 m_table->SetRowAttr(attr, row);
7213 }
7214 else
7215 {
39bcce60 7216 wxSafeDecRef(attr);
758cbedf
VZ
7217 }
7218}
7219
7220void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
7221{
7222 if ( CanHaveAttributes() )
7223 {
7224 m_table->SetColAttr(attr, col);
7225 }
7226 else
7227 {
39bcce60 7228 wxSafeDecRef(attr);
758cbedf
VZ
7229 }
7230}
7231
2e9a6788
VZ
7232void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
7233{
7234 if ( CanHaveAttributes() )
7235 {
0a976765 7236 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7237 attr->SetBackgroundColour(colour);
7238 attr->DecRef();
7239 }
7240}
7241
7242void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
7243{
7244 if ( CanHaveAttributes() )
7245 {
0a976765 7246 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7247 attr->SetTextColour(colour);
7248 attr->DecRef();
7249 }
7250}
7251
7252void wxGrid::SetCellFont( int row, int col, const wxFont& font )
7253{
7254 if ( CanHaveAttributes() )
7255 {
0a976765 7256 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7257 attr->SetFont(font);
7258 attr->DecRef();
7259 }
7260}
7261
7262void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
7263{
7264 if ( CanHaveAttributes() )
7265 {
0a976765 7266 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7267 attr->SetAlignment(horiz, vert);
7268 attr->DecRef();
ab79958a
VZ
7269 }
7270}
7271
7272void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
7273{
7274 if ( CanHaveAttributes() )
7275 {
0a976765 7276 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
7277 attr->SetRenderer(renderer);
7278 attr->DecRef();
9b4aede2
RD
7279 }
7280}
7281
7282void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
7283{
7284 if ( CanHaveAttributes() )
7285 {
7286 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7287 attr->SetEditor(editor);
7288 attr->DecRef();
283b7808
VZ
7289 }
7290}
7291
7292void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
7293{
7294 if ( CanHaveAttributes() )
7295 {
7296 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7297 attr->SetReadOnly(isReadOnly);
7298 attr->DecRef();
2e9a6788 7299 }
f85afd4e
MB
7300}
7301
f2d76237
RD
7302// ----------------------------------------------------------------------------
7303// Data type registration
7304// ----------------------------------------------------------------------------
7305
7306void wxGrid::RegisterDataType(const wxString& typeName,
7307 wxGridCellRenderer* renderer,
7308 wxGridCellEditor* editor)
7309{
7310 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
7311}
7312
7313
99306db2 7314wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
7315{
7316 wxString typeName = m_table->GetTypeName(row, col);
7317 return GetDefaultEditorForType(typeName);
7318}
7319
99306db2 7320wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
7321{
7322 wxString typeName = m_table->GetTypeName(row, col);
7323 return GetDefaultRendererForType(typeName);
7324}
7325
99306db2
VZ
7326wxGridCellEditor*
7327wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 7328{
c4608a8a 7329 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
7330 if ( index == wxNOT_FOUND )
7331 {
7332 wxFAIL_MSG(wxT("Unknown data type name"));
7333
f2d76237
RD
7334 return NULL;
7335 }
0b190b0f 7336
f2d76237
RD
7337 return m_typeRegistry->GetEditor(index);
7338}
7339
99306db2
VZ
7340wxGridCellRenderer*
7341wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 7342{
c4608a8a 7343 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
7344 if ( index == wxNOT_FOUND )
7345 {
c4608a8a 7346 wxFAIL_MSG(wxT("Unknown data type name"));
0b190b0f 7347
c4608a8a 7348 return NULL;
e72b4213 7349 }
0b190b0f 7350
c4608a8a 7351 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
7352}
7353
7354
2e9a6788
VZ
7355// ----------------------------------------------------------------------------
7356// row/col size
7357// ----------------------------------------------------------------------------
7358
6e8524b1
MB
7359void wxGrid::EnableDragRowSize( bool enable )
7360{
7361 m_canDragRowSize = enable;
7362}
7363
7364
7365void wxGrid::EnableDragColSize( bool enable )
7366{
7367 m_canDragColSize = enable;
7368}
7369
4cfa5de6
RD
7370void wxGrid::EnableDragGridSize( bool enable )
7371{
7372 m_canDragGridSize = enable;
7373}
7374
6e8524b1 7375
f85afd4e
MB
7376void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
7377{
7378 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
7379
7380 if ( resizeExistingRows )
7381 {
7c1cb261
VZ
7382 InitRowHeights();
7383
f85afd4e 7384 CalcDimensions();
f85afd4e
MB
7385 }
7386}
7387
7388void wxGrid::SetRowSize( int row, int height )
7389{
b99be8fb 7390 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 7391
7c1cb261
VZ
7392 if ( m_rowHeights.IsEmpty() )
7393 {
7394 // need to really create the array
7395 InitRowHeights();
7396 }
60ff3b99 7397
b99be8fb
VZ
7398 int h = wxMax( 0, height );
7399 int diff = h - m_rowHeights[row];
60ff3b99 7400
b99be8fb 7401 m_rowHeights[row] = h;
7c1cb261 7402 int i;
b99be8fb 7403 for ( i = row; i < m_numRows; i++ )
f85afd4e 7404 {
b99be8fb 7405 m_rowBottoms[i] += diff;
f85afd4e 7406 }
b99be8fb 7407 CalcDimensions();
f85afd4e
MB
7408}
7409
7410void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
7411{
7412 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
7413
7414 if ( resizeExistingCols )
7415 {
7c1cb261
VZ
7416 InitColWidths();
7417
f85afd4e 7418 CalcDimensions();
f85afd4e
MB
7419 }
7420}
7421
7422void wxGrid::SetColSize( int col, int width )
7423{
b99be8fb 7424 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 7425
43947979
VZ
7426 // should we check that it's bigger than GetColMinimalWidth(col) here?
7427
7c1cb261
VZ
7428 if ( m_colWidths.IsEmpty() )
7429 {
7430 // need to really create the array
7431 InitColWidths();
7432 }
f85afd4e 7433
b99be8fb
VZ
7434 int w = wxMax( 0, width );
7435 int diff = w - m_colWidths[col];
7436 m_colWidths[col] = w;
60ff3b99 7437
7c1cb261 7438 int i;
b99be8fb 7439 for ( i = col; i < m_numCols; i++ )
f85afd4e 7440 {
b99be8fb 7441 m_colRights[i] += diff;
f85afd4e 7442 }
b99be8fb 7443 CalcDimensions();
f85afd4e
MB
7444}
7445
2d66e025 7446
43947979
VZ
7447void wxGrid::SetColMinimalWidth( int col, int width )
7448{
af547d51
VZ
7449 m_colMinWidths.Put(col, width);
7450}
7451
7452void wxGrid::SetRowMinimalHeight( int row, int width )
7453{
7454 m_rowMinHeights.Put(row, width);
43947979
VZ
7455}
7456
7457int wxGrid::GetColMinimalWidth(int col) const
7458{
af547d51
VZ
7459 long value = m_colMinWidths.Get(col);
7460 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
7461}
7462
7463int wxGrid::GetRowMinimalHeight(int row) const
7464{
7465 long value = m_rowMinHeights.Get(row);
7466 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
43947979
VZ
7467}
7468
57c086ef
VZ
7469// ----------------------------------------------------------------------------
7470// auto sizing
7471// ----------------------------------------------------------------------------
7472
af547d51 7473void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
7474{
7475 wxClientDC dc(m_gridWin);
7476
a95e38c0
VZ
7477 // init both of them to avoid compiler warnings, even if weo nly need one
7478 int row = -1,
7479 col = -1;
af547d51
VZ
7480 if ( column )
7481 col = colOrRow;
7482 else
7483 row = colOrRow;
7484
7485 wxCoord extent, extentMax = 0;
7486 int max = column ? m_numRows : m_numCols;
39bcce60 7487 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 7488 {
af547d51
VZ
7489 if ( column )
7490 row = rowOrCol;
7491 else
7492 col = rowOrCol;
7493
65e4e78e 7494 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7495 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
7496 if ( renderer )
7497 {
af547d51
VZ
7498 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
7499 extent = column ? size.x : size.y;
7500 if ( extent > extentMax )
65e4e78e 7501 {
af547d51 7502 extentMax = extent;
65e4e78e 7503 }
0b190b0f
VZ
7504
7505 renderer->DecRef();
65e4e78e
VZ
7506 }
7507
7508 attr->DecRef();
7509 }
7510
af547d51
VZ
7511 // now also compare with the column label extent
7512 wxCoord w, h;
65e4e78e 7513 dc.SetFont( GetLabelFont() );
294d195c
MB
7514
7515 if ( column )
7516 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
7517 else
7518 dc.GetTextExtent( GetRowLabelValue(col), &w, &h );
7519
af547d51
VZ
7520 extent = column ? w : h;
7521 if ( extent > extentMax )
65e4e78e 7522 {
af547d51 7523 extentMax = extent;
65e4e78e
VZ
7524 }
7525
af547d51 7526 if ( !extentMax )
65e4e78e 7527 {
af547d51
VZ
7528 // empty column - give default extent (notice that if extentMax is less
7529 // than default extent but != 0, it's ok)
7530 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
7531 }
7532 else
7533 {
a95e38c0
VZ
7534 if ( column )
7535 {
7536 // leave some space around text
7537 extentMax += 10;
7538 }
65e4e78e
VZ
7539 }
7540
af547d51
VZ
7541 if ( column )
7542 SetColSize(col, extentMax);
7543 else
39bcce60 7544 SetRowSize(row, extentMax);
af547d51 7545
65e4e78e
VZ
7546 if ( setAsMin )
7547 {
af547d51
VZ
7548 if ( column )
7549 SetColMinimalWidth(col, extentMax);
7550 else
39bcce60 7551 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
7552 }
7553}
7554
266e8367 7555int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 7556{
57c086ef
VZ
7557 int width = m_rowLabelWidth;
7558
65e4e78e
VZ
7559 for ( int col = 0; col < m_numCols; col++ )
7560 {
266e8367
VZ
7561 if ( !calcOnly )
7562 {
7563 AutoSizeColumn(col, setAsMin);
7564 }
57c086ef
VZ
7565
7566 width += GetColWidth(col);
65e4e78e 7567 }
57c086ef 7568
266e8367 7569 return width;
65e4e78e
VZ
7570}
7571
266e8367 7572int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
7573{
7574 int height = m_colLabelHeight;
7575
7576 for ( int row = 0; row < m_numRows; row++ )
7577 {
af547d51
VZ
7578 if ( !calcOnly )
7579 {
7580 AutoSizeRow(row, setAsMin);
7581 }
57c086ef
VZ
7582
7583 height += GetRowHeight(row);
7584 }
7585
266e8367 7586 return height;
57c086ef
VZ
7587}
7588
7589void wxGrid::AutoSize()
7590{
266e8367
VZ
7591 // set the size too
7592 SetSize(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
7593}
7594
7595wxSize wxGrid::DoGetBestSize() const
7596{
7597 // don't set sizes, only calculate them
7598 wxGrid *self = (wxGrid *)this; // const_cast
7599
7600 return wxSize(self->SetOrCalcColumnSizes(TRUE),
7601 self->SetOrCalcRowSizes(TRUE));
7602}
7603
7604void wxGrid::Fit()
7605{
7606 AutoSize();
57c086ef
VZ
7607}
7608
6fc0f38f
SN
7609
7610wxPen& wxGrid::GetDividerPen() const
7611{
7612 return wxNullPen;
7613}
7614
57c086ef
VZ
7615// ----------------------------------------------------------------------------
7616// cell value accessor functions
7617// ----------------------------------------------------------------------------
f85afd4e
MB
7618
7619void wxGrid::SetCellValue( int row, int col, const wxString& s )
7620{
7621 if ( m_table )
7622 {
7623 m_table->SetValue( row, col, s.c_str() );
2d66e025
MB
7624 if ( !GetBatchCount() )
7625 {
7626 wxClientDC dc( m_gridWin );
7627 PrepareDC( dc );
7628 DrawCell( dc, wxGridCellCoords(row, col) );
7629 }
60ff3b99 7630
f85afd4e 7631 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6
RD
7632 m_currentCellCoords.GetCol() == col &&
7633 IsCellEditControlEnabled())
f85afd4e 7634 {
4cfa5de6
RD
7635 HideCellEditControl();
7636 ShowCellEditControl(); // will reread data from table
f85afd4e 7637 }
f85afd4e
MB
7638 }
7639}
7640
7641
7642//
2d66e025 7643// ------ Block, row and col selection
f85afd4e
MB
7644//
7645
7646void wxGrid::SelectRow( int row, bool addToSelected )
7647{
b5808881 7648 if ( IsSelection() && !addToSelected )
e32352cf 7649 ClearSelection();
70c7a608 7650
b5808881 7651 m_selection->SelectRow( row );
f85afd4e
MB
7652}
7653
7654
7655void wxGrid::SelectCol( int col, bool addToSelected )
7656{
b5808881 7657 if ( IsSelection() && !addToSelected )
e32352cf 7658 ClearSelection();
f85afd4e 7659
b5808881 7660 m_selection->SelectCol( col );
f85afd4e
MB
7661}
7662
7663
7664void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7665{
7666 int temp;
da6af900 7667 wxGridCellCoords updateTopLeft, updateBottomRight;
8f177c8e 7668
f85afd4e
MB
7669 if ( topRow > bottomRow )
7670 {
7671 temp = topRow;
7672 topRow = bottomRow;
7673 bottomRow = temp;
7674 }
7675
7676 if ( leftCol > rightCol )
7677 {
7678 temp = leftCol;
7679 leftCol = rightCol;
7680 rightCol = temp;
7681 }
f0102d2a 7682
70c7a608
SN
7683 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7684 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
da6af900 7685
b5808881
SN
7686 if ( m_selectingTopLeft != updateTopLeft ||
7687 m_selectingBottomRight != updateBottomRight )
da6af900 7688 {
70c7a608
SN
7689 // Compute two optimal update rectangles:
7690 // Either one rectangle is a real subset of the
7691 // other, or they are (almost) disjoint!
7692 wxRect rect[4];
ee6694a7
VZ
7693 bool need_refresh[4];
7694 need_refresh[0] =
7695 need_refresh[1] =
7696 need_refresh[2] =
7697 need_refresh[3] = FALSE;
70c7a608
SN
7698 int i;
7699
7700 // Store intermediate values
b5808881
SN
7701 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7702 wxCoord oldTop = m_selectingTopLeft.GetRow();
7703 wxCoord oldRight = m_selectingBottomRight.GetCol();
7704 wxCoord oldBottom = m_selectingBottomRight.GetRow();
70c7a608
SN
7705
7706 // Determine the outer/inner coordinates.
7707 if (oldLeft > leftCol)
f0102d2a 7708 {
70c7a608
SN
7709 temp = oldLeft;
7710 oldLeft = leftCol;
7711 leftCol = temp;
cb309039 7712 }
70c7a608 7713 if (oldTop > topRow )
f0102d2a 7714 {
70c7a608
SN
7715 temp = oldTop;
7716 oldTop = topRow;
7717 topRow = temp;
cb309039
SN
7718 }
7719 if (oldRight < rightCol )
70c7a608
SN
7720 {
7721 temp = oldRight;
7722 oldRight = rightCol;
7723 rightCol = temp;
cb309039
SN
7724 }
7725 if (oldBottom < bottomRow)
7726 {
70c7a608
SN
7727 temp = oldBottom;
7728 oldBottom = bottomRow;
7729 bottomRow = temp;
cb309039 7730 }
70c7a608
SN
7731
7732 // Now, either the stuff marked old is the outer
7733 // rectangle or we don't have a situation where one
7734 // is contained in the other.
b99be8fb 7735
cb309039
SN
7736 if ( oldLeft < leftCol )
7737 {
7738 need_refresh[0] = TRUE;
7739 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7740 oldLeft ),
b99be8fb 7741 wxGridCellCoords ( oldBottom,
cb309039
SN
7742 leftCol - 1 ) );
7743 }
7744
7745 if ( oldTop < topRow )
7746 {
7747 need_refresh[1] = TRUE;
7748 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7749 leftCol ),
b99be8fb 7750 wxGridCellCoords ( topRow - 1,
cb309039
SN
7751 rightCol ) );
7752 }
7753
7754 if ( oldRight > rightCol )
7755 {
7756 need_refresh[2] = TRUE;
7757 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7758 rightCol + 1 ),
7759 wxGridCellCoords ( oldBottom,
7760 oldRight ) );
7761 }
7762
7763 if ( oldBottom > bottomRow )
7764 {
7765 need_refresh[3] = TRUE;
7766 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
7767 leftCol ),
7768 wxGridCellCoords ( oldBottom,
7769 rightCol ) );
7770 }
70c7a608
SN
7771
7772
7773 // Change Selection
b5808881
SN
7774 m_selectingTopLeft = updateTopLeft;
7775 m_selectingBottomRight = updateBottomRight;
b99be8fb 7776
70c7a608
SN
7777 // various Refresh() calls
7778 for (i = 0; i < 4; i++ )
7779 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
c6a51dcd 7780 m_gridWin->Refresh( FALSE, &(rect[i]) );
da6af900 7781 }
f85afd4e 7782
5c8fc7c1
SN
7783 // never generate an event as it will be generated from
7784 // wxGridSelection::SelectBlock!
f85afd4e
MB
7785}
7786
7787void wxGrid::SelectAll()
7788{
b5808881
SN
7789 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
7790}
f85afd4e 7791
b5808881
SN
7792bool wxGrid::IsSelection()
7793{
7794 return ( m_selection->IsSelection() ||
7795 ( m_selectingTopLeft != wxGridNoCellCoords &&
7796 m_selectingBottomRight != wxGridNoCellCoords ) );
f85afd4e
MB
7797}
7798
b5808881
SN
7799bool wxGrid::IsInSelection( int row, int col )
7800{
7801 return ( m_selection->IsInSelection( row, col ) ||
7802 ( row >= m_selectingTopLeft.GetRow() &&
7803 col >= m_selectingTopLeft.GetCol() &&
7804 row <= m_selectingBottomRight.GetRow() &&
7805 col <= m_selectingBottomRight.GetCol() ) );
7806}
f85afd4e
MB
7807
7808void wxGrid::ClearSelection()
7809{
b5808881
SN
7810 m_selectingTopLeft = wxGridNoCellCoords;
7811 m_selectingBottomRight = wxGridNoCellCoords;
7812 m_selection->ClearSelection();
8f177c8e 7813}
f85afd4e
MB
7814
7815
da6af900 7816// This function returns the rectangle that encloses the given block
2d66e025
MB
7817// in device coords clipped to the client size of the grid window.
7818//
58dd5b3b
MB
7819wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
7820 const wxGridCellCoords &bottomRight )
f85afd4e 7821{
58dd5b3b 7822 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
7823 wxRect cellRect;
7824
58dd5b3b
MB
7825 cellRect = CellToRect( topLeft );
7826 if ( cellRect != wxGridNoCellRect )
f85afd4e 7827 {
58dd5b3b
MB
7828 rect = cellRect;
7829 }
7830 else
7831 {
7832 rect = wxRect( 0, 0, 0, 0 );
7833 }
60ff3b99 7834
58dd5b3b
MB
7835 cellRect = CellToRect( bottomRight );
7836 if ( cellRect != wxGridNoCellRect )
7837 {
7838 rect += cellRect;
2d66e025
MB
7839 }
7840 else
7841 {
7842 return wxGridNoCellRect;
f85afd4e
MB
7843 }
7844
58dd5b3b
MB
7845 // convert to scrolled coords
7846 //
7847 int left, top, right, bottom;
7848 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
7849 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
7850
7851 int cw, ch;
7852 m_gridWin->GetClientSize( &cw, &ch );
7853
7854 rect.SetLeft( wxMax(0, left) );
7855 rect.SetTop( wxMax(0, top) );
7856 rect.SetRight( wxMin(cw, right) );
7857 rect.SetBottom( wxMin(ch, bottom) );
7858
f85afd4e
MB
7859 return rect;
7860}
7861
7862
58dd5b3b 7863
f85afd4e
MB
7864//
7865// ------ Grid event classes
7866//
7867
7868IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent )
7869
7870wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 7871 int row, int col, int x, int y, bool sel,
f85afd4e
MB
7872 bool control, bool shift, bool alt, bool meta )
7873 : wxNotifyEvent( type, id )
7874{
7875 m_row = row;
7876 m_col = col;
7877 m_x = x;
7878 m_y = y;
5c8fc7c1 7879 m_selecting = sel;
f85afd4e
MB
7880 m_control = control;
7881 m_shift = shift;
7882 m_alt = alt;
7883 m_meta = meta;
8f177c8e 7884
f85afd4e
MB
7885 SetEventObject(obj);
7886}
7887
7888
7889IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent )
7890
7891wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
7892 int rowOrCol, int x, int y,
7893 bool control, bool shift, bool alt, bool meta )
7894 : wxNotifyEvent( type, id )
7895{
7896 m_rowOrCol = rowOrCol;
7897 m_x = x;
7898 m_y = y;
7899 m_control = control;
7900 m_shift = shift;
7901 m_alt = alt;
7902 m_meta = meta;
8f177c8e 7903
f85afd4e
MB
7904 SetEventObject(obj);
7905}
7906
7907
7908IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent )
7909
7910wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
7911 const wxGridCellCoords& topLeft,
7912 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
7913 bool sel, bool control,
7914 bool shift, bool alt, bool meta )
8f177c8e 7915 : wxNotifyEvent( type, id )
f85afd4e
MB
7916{
7917 m_topLeft = topLeft;
7918 m_bottomRight = bottomRight;
5c8fc7c1 7919 m_selecting = sel;
f85afd4e
MB
7920 m_control = control;
7921 m_shift = shift;
7922 m_alt = alt;
7923 m_meta = meta;
7924
7925 SetEventObject(obj);
7926}
7927
7928
7929#endif // ifndef wxUSE_NEW_GRID