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