]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Visualge V3.0 fix
[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;
7c1cb261
VZ
2946}
2947
2948// ----------------------------------------------------------------------------
2949// the idea is to call these functions only when necessary because they create
2950// quite big arrays which eat memory mostly unnecessary - in particular, if
2951// default widths/heights are used for all rows/columns, we may not use these
2952// arrays at all
2953//
2954// with some extra code, it should be possible to only store the
2955// widths/heights different from default ones but this will be done later...
2956// ----------------------------------------------------------------------------
2957
2958void wxGrid::InitRowHeights()
2959{
2960 m_rowHeights.Empty();
2961 m_rowBottoms.Empty();
2962
2963 m_rowHeights.Alloc( m_numRows );
2964 m_rowBottoms.Alloc( m_numRows );
2965
2966 int rowBottom = 0;
2967 for ( int i = 0; i < m_numRows; i++ )
2968 {
2969 m_rowHeights.Add( m_defaultRowHeight );
2970 rowBottom += m_defaultRowHeight;
2971 m_rowBottoms.Add( rowBottom );
2972 }
2973}
2974
2975void wxGrid::InitColWidths()
2976{
2977 m_colWidths.Empty();
2978 m_colRights.Empty();
2979
2980 m_colWidths.Alloc( m_numCols );
2981 m_colRights.Alloc( m_numCols );
2982 int colRight = 0;
2983 for ( int i = 0; i < m_numCols; i++ )
2984 {
2985 m_colWidths.Add( m_defaultColWidth );
2986 colRight += m_defaultColWidth;
2987 m_colRights.Add( colRight );
2988 }
2989}
2990
2991int wxGrid::GetColWidth(int col) const
2992{
2993 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
2994}
2995
2996int wxGrid::GetColLeft(int col) const
2997{
2998 return m_colRights.IsEmpty() ? col * m_defaultColWidth
2999 : m_colRights[col] - m_colWidths[col];
3000}
3001
3002int wxGrid::GetColRight(int col) const
3003{
3004 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3005 : m_colRights[col];
3006}
3007
3008int wxGrid::GetRowHeight(int row) const
3009{
3010 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3011}
2d66e025 3012
7c1cb261
VZ
3013int wxGrid::GetRowTop(int row) const
3014{
3015 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3016 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
3017}
3018
7c1cb261
VZ
3019int wxGrid::GetRowBottom(int row) const
3020{
3021 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3022 : m_rowBottoms[row];
3023}
f85afd4e
MB
3024
3025void wxGrid::CalcDimensions()
3026{
f85afd4e 3027 int cw, ch;
2d66e025 3028 GetClientSize( &cw, &ch );
f85afd4e 3029
2d66e025 3030 if ( m_numRows > 0 && m_numCols > 0 )
f85afd4e 3031 {
7c1cb261
VZ
3032 int right = GetColRight( m_numCols-1 ) + 50;
3033 int bottom = GetRowBottom( m_numRows-1 ) + 50;
60ff3b99 3034
2d66e025
MB
3035 // TODO: restore the scroll position that we had before sizing
3036 //
3037 int x, y;
3038 GetViewStart( &x, &y );
f0102d2a
VZ
3039 SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE,
3040 right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE,
be46e4a6 3041 x, y );
f85afd4e 3042 }
f85afd4e
MB
3043}
3044
3045
7807d81c
MB
3046void wxGrid::CalcWindowSizes()
3047{
3048 int cw, ch;
3049 GetClientSize( &cw, &ch );
b99be8fb 3050
7807d81c
MB
3051 if ( m_cornerLabelWin->IsShown() )
3052 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3053
3054 if ( m_colLabelWin->IsShown() )
3055 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3056
3057 if ( m_rowLabelWin->IsShown() )
3058 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3059
3060 if ( m_gridWin->IsShown() )
3061 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3062}
3063
3064
f85afd4e
MB
3065// this is called when the grid table sends a message to say that it
3066// has been redimensioned
3067//
3068bool wxGrid::Redimension( wxGridTableMessage& msg )
3069{
3070 int i;
8f177c8e 3071
7c1cb261
VZ
3072 // if we were using the default widths/heights so far, we must change them
3073 // now
3074 if ( m_colWidths.IsEmpty() )
3075 {
3076 InitColWidths();
3077 }
3078
3079 if ( m_rowHeights.IsEmpty() )
3080 {
3081 InitRowHeights();
3082 }
3083
f85afd4e
MB
3084 switch ( msg.GetId() )
3085 {
3086 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3087 {
3088 size_t pos = msg.GetCommandInt();
3089 int numRows = msg.GetCommandInt2();
3090 for ( i = 0; i < numRows; i++ )
3091 {
3092 m_rowHeights.Insert( m_defaultRowHeight, pos );
3093 m_rowBottoms.Insert( 0, pos );
3094 }
3095 m_numRows += numRows;
2d66e025
MB
3096
3097 int bottom = 0;
3098 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
60ff3b99 3099
2d66e025
MB
3100 for ( i = pos; i < m_numRows; i++ )
3101 {
3102 bottom += m_rowHeights[i];
3103 m_rowBottoms[i] = bottom;
3104 }
f85afd4e
MB
3105 CalcDimensions();
3106 }
3107 return TRUE;
3108
3109 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3110 {
3111 int numRows = msg.GetCommandInt();
3112 for ( i = 0; i < numRows; i++ )
3113 {
3114 m_rowHeights.Add( m_defaultRowHeight );
3115 m_rowBottoms.Add( 0 );
3116 }
2d66e025
MB
3117
3118 int oldNumRows = m_numRows;
f85afd4e 3119 m_numRows += numRows;
2d66e025
MB
3120
3121 int bottom = 0;
3122 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
60ff3b99 3123
2d66e025
MB
3124 for ( i = oldNumRows; i < m_numRows; i++ )
3125 {
3126 bottom += m_rowHeights[i];
3127 m_rowBottoms[i] = bottom;
3128 }
f85afd4e
MB
3129 CalcDimensions();
3130 }
3131 return TRUE;
3132
3133 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
3134 {
3135 size_t pos = msg.GetCommandInt();
3136 int numRows = msg.GetCommandInt2();
3137 for ( i = 0; i < numRows; i++ )
3138 {
3139 m_rowHeights.Remove( pos );
3140 m_rowBottoms.Remove( pos );
3141 }
3142 m_numRows -= numRows;
3143
f85afd4e
MB
3144 if ( !m_numRows )
3145 {
3146 m_numCols = 0;
3147 m_colWidths.Clear();
3148 m_colRights.Clear();
3149 m_currentCellCoords = wxGridNoCellCoords;
3150 }
2d66e025 3151 else
f85afd4e 3152 {
2d66e025
MB
3153 if ( m_currentCellCoords.GetRow() >= m_numRows )
3154 m_currentCellCoords.Set( 0, 0 );
3155
3156 int h = 0;
3157 for ( i = 0; i < m_numRows; i++ )
3158 {
3159 h += m_rowHeights[i];
3160 m_rowBottoms[i] = h;
3161 }
f85afd4e 3162 }
60ff3b99 3163
f85afd4e
MB
3164 CalcDimensions();
3165 }
3166 return TRUE;
3167
3168 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
3169 {
3170 size_t pos = msg.GetCommandInt();
3171 int numCols = msg.GetCommandInt2();
3172 for ( i = 0; i < numCols; i++ )
3173 {
3174 m_colWidths.Insert( m_defaultColWidth, pos );
3175 m_colRights.Insert( 0, pos );
3176 }
3177 m_numCols += numCols;
2d66e025
MB
3178
3179 int right = 0;
3180 if ( pos > 0 ) right = m_colRights[pos-1];
60ff3b99 3181
2d66e025
MB
3182 for ( i = pos; i < m_numCols; i++ )
3183 {
3184 right += m_colWidths[i];
3185 m_colRights[i] = right;
3186 }
f85afd4e
MB
3187 CalcDimensions();
3188 }
3189 return TRUE;
3190
3191 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
3192 {
3193 int numCols = msg.GetCommandInt();
3194 for ( i = 0; i < numCols; i++ )
3195 {
3196 m_colWidths.Add( m_defaultColWidth );
3197 m_colRights.Add( 0 );
3198 }
2d66e025
MB
3199
3200 int oldNumCols = m_numCols;
f85afd4e 3201 m_numCols += numCols;
2d66e025
MB
3202
3203 int right = 0;
3204 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
60ff3b99 3205
2d66e025
MB
3206 for ( i = oldNumCols; i < m_numCols; i++ )
3207 {
3208 right += m_colWidths[i];
3209 m_colRights[i] = right;
3210 }
f85afd4e
MB
3211 CalcDimensions();
3212 }
3213 return TRUE;
3214
3215 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
3216 {
3217 size_t pos = msg.GetCommandInt();
3218 int numCols = msg.GetCommandInt2();
3219 for ( i = 0; i < numCols; i++ )
3220 {
3221 m_colWidths.Remove( pos );
3222 m_colRights.Remove( pos );
3223 }
3224 m_numCols -= numCols;
f85afd4e
MB
3225
3226 if ( !m_numCols )
3227 {
3228#if 0 // leave the row alone here so that AppendCols will work subsequently
3229 m_numRows = 0;
3230 m_rowHeights.Clear();
3231 m_rowBottoms.Clear();
8f177c8e 3232#endif
f85afd4e
MB
3233 m_currentCellCoords = wxGridNoCellCoords;
3234 }
60ff3b99 3235 else
f85afd4e 3236 {
2d66e025
MB
3237 if ( m_currentCellCoords.GetCol() >= m_numCols )
3238 m_currentCellCoords.Set( 0, 0 );
3239
3240 int w = 0;
3241 for ( i = 0; i < m_numCols; i++ )
3242 {
3243 w += m_colWidths[i];
3244 m_colRights[i] = w;
3245 }
f85afd4e
MB
3246 }
3247 CalcDimensions();
3248 }
3249 return TRUE;
3250 }
3251
3252 return FALSE;
3253}
3254
3255
2d66e025 3256void wxGrid::CalcRowLabelsExposed( wxRegion& reg )
f85afd4e 3257{
2d66e025
MB
3258 wxRegionIterator iter( reg );
3259 wxRect r;
f85afd4e 3260
2d66e025 3261 m_rowLabelsExposed.Empty();
f85afd4e 3262
2d66e025
MB
3263 int top, bottom;
3264 while ( iter )
f85afd4e 3265 {
2d66e025 3266 r = iter.GetRect();
f85afd4e 3267
2d66e025
MB
3268 // TODO: remove this when we can...
3269 // There is a bug in wxMotif that gives garbage update
3270 // rectangles if you jump-scroll a long way by clicking the
3271 // scrollbar with middle button. This is a work-around
3272 //
3273#if defined(__WXMOTIF__)
3274 int cw, ch;
3275 m_gridWin->GetClientSize( &cw, &ch );
3276 if ( r.GetTop() > ch ) r.SetTop( 0 );
3277 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3278#endif
f85afd4e 3279
2d66e025
MB
3280 // logical bounds of update region
3281 //
3282 int dummy;
3283 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3284 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3285
3286 // find the row labels within these bounds
3287 //
3288 int row;
2d66e025
MB
3289 for ( row = 0; row < m_numRows; row++ )
3290 {
7c1cb261
VZ
3291 if ( GetRowBottom(row) < top )
3292 continue;
2d66e025 3293
6d55126d 3294 if ( GetRowTop(row) > bottom )
7c1cb261 3295 break;
60ff3b99 3296
2d66e025
MB
3297 m_rowLabelsExposed.Add( row );
3298 }
60ff3b99 3299
2d66e025 3300 iter++ ;
f85afd4e
MB
3301 }
3302}
3303
3304
2d66e025 3305void wxGrid::CalcColLabelsExposed( wxRegion& reg )
f85afd4e 3306{
2d66e025
MB
3307 wxRegionIterator iter( reg );
3308 wxRect r;
f85afd4e 3309
2d66e025 3310 m_colLabelsExposed.Empty();
f85afd4e 3311
2d66e025
MB
3312 int left, right;
3313 while ( iter )
f85afd4e 3314 {
2d66e025 3315 r = iter.GetRect();
f85afd4e 3316
2d66e025
MB
3317 // TODO: remove this when we can...
3318 // There is a bug in wxMotif that gives garbage update
3319 // rectangles if you jump-scroll a long way by clicking the
3320 // scrollbar with middle button. This is a work-around
3321 //
3322#if defined(__WXMOTIF__)
3323 int cw, ch;
3324 m_gridWin->GetClientSize( &cw, &ch );
3325 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3326 r.SetRight( wxMin( r.GetRight(), cw ) );
3327#endif
3328
3329 // logical bounds of update region
3330 //
3331 int dummy;
3332 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3333 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3334
3335 // find the cells within these bounds
3336 //
3337 int col;
2d66e025
MB
3338 for ( col = 0; col < m_numCols; col++ )
3339 {
7c1cb261
VZ
3340 if ( GetColRight(col) < left )
3341 continue;
60ff3b99 3342
7c1cb261
VZ
3343 if ( GetColLeft(col) > right )
3344 break;
2d66e025
MB
3345
3346 m_colLabelsExposed.Add( col );
3347 }
60ff3b99 3348
2d66e025 3349 iter++ ;
f85afd4e
MB
3350 }
3351}
3352
3353
2d66e025 3354void wxGrid::CalcCellsExposed( wxRegion& reg )
f85afd4e 3355{
2d66e025
MB
3356 wxRegionIterator iter( reg );
3357 wxRect r;
f85afd4e 3358
2d66e025
MB
3359 m_cellsExposed.Empty();
3360 m_rowsExposed.Empty();
3361 m_colsExposed.Empty();
f85afd4e 3362
2d66e025
MB
3363 int left, top, right, bottom;
3364 while ( iter )
3365 {
3366 r = iter.GetRect();
f85afd4e 3367
2d66e025
MB
3368 // TODO: remove this when we can...
3369 // There is a bug in wxMotif that gives garbage update
3370 // rectangles if you jump-scroll a long way by clicking the
3371 // scrollbar with middle button. This is a work-around
3372 //
3373#if defined(__WXMOTIF__)
f85afd4e 3374 int cw, ch;
2d66e025
MB
3375 m_gridWin->GetClientSize( &cw, &ch );
3376 if ( r.GetTop() > ch ) r.SetTop( 0 );
3377 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3378 r.SetRight( wxMin( r.GetRight(), cw ) );
3379 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3380#endif
8f177c8e 3381
2d66e025
MB
3382 // logical bounds of update region
3383 //
3384 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3385 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 3386
2d66e025 3387 // find the cells within these bounds
f85afd4e 3388 //
2d66e025 3389 int row, col;
2d66e025 3390 for ( row = 0; row < m_numRows; row++ )
f85afd4e 3391 {
7c1cb261
VZ
3392 if ( GetRowBottom(row) <= top )
3393 continue;
f85afd4e 3394
7c1cb261
VZ
3395 if ( GetRowTop(row) > bottom )
3396 break;
60ff3b99 3397
2d66e025 3398 m_rowsExposed.Add( row );
f85afd4e 3399
2d66e025
MB
3400 for ( col = 0; col < m_numCols; col++ )
3401 {
7c1cb261
VZ
3402 if ( GetColRight(col) <= left )
3403 continue;
60ff3b99 3404
7c1cb261
VZ
3405 if ( GetColLeft(col) > right )
3406 break;
60ff3b99 3407
7c1cb261
VZ
3408 if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
3409 m_colsExposed.Add( col );
2d66e025
MB
3410 m_cellsExposed.Add( wxGridCellCoords( row, col ) );
3411 }
3412 }
60ff3b99 3413
7c1cb261 3414 iter++;
f85afd4e
MB
3415 }
3416}
3417
3418
2d66e025 3419void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3420{
2d66e025
MB
3421 int x, y, row;
3422 wxPoint pos( event.GetPosition() );
3423 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3424
2d66e025 3425 if ( event.Dragging() )
f85afd4e
MB
3426 {
3427 m_isDragging = TRUE;
8f177c8e 3428
2d66e025 3429 if ( event.LeftIsDown() )
f85afd4e
MB
3430 {
3431 switch( m_cursorMode )
3432 {
f85afd4e
MB
3433 case WXGRID_CURSOR_RESIZE_ROW:
3434 {
2d66e025
MB
3435 int cw, ch, left, dummy;
3436 m_gridWin->GetClientSize( &cw, &ch );
3437 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 3438
2d66e025
MB
3439 wxClientDC dc( m_gridWin );
3440 PrepareDC( dc );
7c1cb261 3441 y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
d2fdd8d2 3442 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
3443 if ( m_dragLastPos >= 0 )
3444 {
2d66e025 3445 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 3446 }
2d66e025
MB
3447 dc.DrawLine( left, y, left+cw, y );
3448 m_dragLastPos = y;
f85afd4e
MB
3449 }
3450 break;
3451
3452 case WXGRID_CURSOR_SELECT_ROW:
f85afd4e
MB
3453 if ( (row = YToRow( y )) >= 0 &&
3454 !IsInSelection( row, 0 ) )
3455 {
3456 SelectRow( row, TRUE );
3457 }
e2b42eeb
VZ
3458
3459 // default label to suppress warnings about "enumeration value
3460 // 'xxx' not handled in switch
3461 default:
3462 break;
f85afd4e
MB
3463 }
3464 }
3465 return;
3466 }
3467
3468 m_isDragging = FALSE;
8f177c8e 3469
60ff3b99 3470
6d004f67
MB
3471 // ------------ Entering or leaving the window
3472 //
3473 if ( event.Entering() || event.Leaving() )
3474 {
e2b42eeb 3475 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
3476 }
3477
e2b42eeb 3478
2d66e025 3479 // ------------ Left button pressed
f85afd4e 3480 //
6d004f67 3481 else if ( event.LeftDown() )
f85afd4e 3482 {
2d66e025
MB
3483 // don't send a label click event for a hit on the
3484 // edge of the row label - this is probably the user
3485 // wanting to resize the row
3486 //
3487 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 3488 {
2d66e025 3489 row = YToRow(y);
58dd5b3b 3490 if ( row >= 0 &&
b54ba671 3491 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 3492 {
2d66e025 3493 SelectRow( row, event.ShiftDown() );
e2b42eeb 3494 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 3495 }
2d66e025
MB
3496 }
3497 else
3498 {
3499 // starting to drag-resize a row
3500 //
6e8524b1
MB
3501 if ( CanDragRowSize() )
3502 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
3503 }
3504 }
f85afd4e 3505
f85afd4e 3506
2d66e025
MB
3507 // ------------ Left double click
3508 //
3509 else if (event.LeftDClick() )
3510 {
3511 if ( YToEdgeOfRow(y) < 0 )
3512 {
3513 row = YToRow(y);
b54ba671 3514 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
f85afd4e
MB
3515 }
3516 }
60ff3b99
VZ
3517
3518
2d66e025 3519 // ------------ Left button released
f85afd4e 3520 //
2d66e025 3521 else if ( event.LeftUp() )
f85afd4e 3522 {
2d66e025 3523 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 3524 {
6d004f67 3525 DoEndDragResizeRow();
60ff3b99 3526
6d004f67
MB
3527 // Note: we are ending the event *after* doing
3528 // default processing in this case
3529 //
b54ba671 3530 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 3531 }
f85afd4e 3532
e2b42eeb
VZ
3533 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3534 m_dragLastPos = -1;
2d66e025 3535 }
f85afd4e 3536
f85afd4e 3537
2d66e025
MB
3538 // ------------ Right button down
3539 //
3540 else if ( event.RightDown() )
3541 {
3542 row = YToRow(y);
b54ba671 3543 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
3544 {
3545 // no default action at the moment
f85afd4e
MB
3546 }
3547 }
60ff3b99
VZ
3548
3549
2d66e025 3550 // ------------ Right double click
f85afd4e 3551 //
2d66e025
MB
3552 else if ( event.RightDClick() )
3553 {
3554 row = YToRow(y);
b54ba671 3555 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
3556 {
3557 // no default action at the moment
3558 }
3559 }
60ff3b99
VZ
3560
3561
2d66e025 3562 // ------------ No buttons down and mouse moving
f85afd4e 3563 //
2d66e025 3564 else if ( event.Moving() )
f85afd4e 3565 {
2d66e025
MB
3566 m_dragRowOrCol = YToEdgeOfRow( y );
3567 if ( m_dragRowOrCol >= 0 )
8f177c8e 3568 {
2d66e025 3569 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 3570 {
e2b42eeb 3571 // don't capture the mouse yet
6e8524b1
MB
3572 if ( CanDragRowSize() )
3573 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
8f177c8e 3574 }
2d66e025 3575 }
6d004f67 3576 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3577 {
e2b42eeb 3578 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
8f177c8e 3579 }
f85afd4e 3580 }
2d66e025
MB
3581}
3582
3583
3584void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3585{
3586 int x, y, col;
3587 wxPoint pos( event.GetPosition() );
3588 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3589
2d66e025 3590 if ( event.Dragging() )
f85afd4e 3591 {
2d66e025 3592 m_isDragging = TRUE;
8f177c8e 3593
2d66e025 3594 if ( event.LeftIsDown() )
8f177c8e 3595 {
2d66e025 3596 switch( m_cursorMode )
8f177c8e 3597 {
2d66e025 3598 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 3599 {
2d66e025
MB
3600 int cw, ch, dummy, top;
3601 m_gridWin->GetClientSize( &cw, &ch );
3602 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 3603
2d66e025
MB
3604 wxClientDC dc( m_gridWin );
3605 PrepareDC( dc );
43947979
VZ
3606
3607 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3608 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 3609 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
3610 if ( m_dragLastPos >= 0 )
3611 {
3612 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3613 }
3614 dc.DrawLine( x, top, x, top+ch );
3615 m_dragLastPos = x;
f85afd4e 3616 }
2d66e025 3617 break;
f85afd4e 3618
2d66e025 3619 case WXGRID_CURSOR_SELECT_COL:
2d66e025
MB
3620 if ( (col = XToCol( x )) >= 0 &&
3621 !IsInSelection( 0, col ) )
3622 {
3623 SelectCol( col, TRUE );
3624 }
e2b42eeb
VZ
3625
3626 // default label to suppress warnings about "enumeration value
3627 // 'xxx' not handled in switch
3628 default:
3629 break;
2d66e025 3630 }
f85afd4e 3631 }
2d66e025 3632 return;
f85afd4e 3633 }
2d66e025
MB
3634
3635 m_isDragging = FALSE;
3636
60ff3b99 3637
6d004f67
MB
3638 // ------------ Entering or leaving the window
3639 //
3640 if ( event.Entering() || event.Leaving() )
3641 {
e2b42eeb 3642 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
3643 }
3644
e2b42eeb 3645
2d66e025 3646 // ------------ Left button pressed
f85afd4e 3647 //
6d004f67 3648 else if ( event.LeftDown() )
f85afd4e 3649 {
2d66e025
MB
3650 // don't send a label click event for a hit on the
3651 // edge of the col label - this is probably the user
3652 // wanting to resize the col
3653 //
3654 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 3655 {
2d66e025 3656 col = XToCol(x);
58dd5b3b 3657 if ( col >= 0 &&
b54ba671 3658 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 3659 {
2d66e025 3660 SelectCol( col, event.ShiftDown() );
e2b42eeb 3661 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
f85afd4e 3662 }
2d66e025
MB
3663 }
3664 else
3665 {
3666 // starting to drag-resize a col
3667 //
6e8524b1
MB
3668 if ( CanDragColSize() )
3669 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
3670 }
3671 }
f85afd4e 3672
f85afd4e 3673
2d66e025
MB
3674 // ------------ Left double click
3675 //
3676 if ( event.LeftDClick() )
3677 {
3678 if ( XToEdgeOfCol(x) < 0 )
3679 {
3680 col = XToCol(x);
b54ba671 3681 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
2d66e025
MB
3682 }
3683 }
60ff3b99
VZ
3684
3685
2d66e025
MB
3686 // ------------ Left button released
3687 //
3688 else if ( event.LeftUp() )
3689 {
3690 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3691 {
6d004f67 3692 DoEndDragResizeCol();
e2b42eeb 3693
6d004f67
MB
3694 // Note: we are ending the event *after* doing
3695 // default processing in this case
3696 //
b54ba671 3697 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
2d66e025 3698 }
f85afd4e 3699
e2b42eeb 3700 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
2d66e025 3701 m_dragLastPos = -1;
60ff3b99
VZ
3702 }
3703
3704
2d66e025
MB
3705 // ------------ Right button down
3706 //
3707 else if ( event.RightDown() )
3708 {
3709 col = XToCol(x);
b54ba671 3710 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
3711 {
3712 // no default action at the moment
f85afd4e
MB
3713 }
3714 }
60ff3b99
VZ
3715
3716
2d66e025 3717 // ------------ Right double click
f85afd4e 3718 //
2d66e025
MB
3719 else if ( event.RightDClick() )
3720 {
3721 col = XToCol(x);
b54ba671 3722 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
3723 {
3724 // no default action at the moment
3725 }
3726 }
60ff3b99
VZ
3727
3728
2d66e025 3729 // ------------ No buttons down and mouse moving
f85afd4e 3730 //
2d66e025 3731 else if ( event.Moving() )
f85afd4e 3732 {
2d66e025
MB
3733 m_dragRowOrCol = XToEdgeOfCol( x );
3734 if ( m_dragRowOrCol >= 0 )
f85afd4e 3735 {
2d66e025 3736 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 3737 {
e2b42eeb 3738 // don't capture the cursor yet
6e8524b1
MB
3739 if ( CanDragColSize() )
3740 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
f85afd4e 3741 }
2d66e025 3742 }
6d004f67 3743 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3744 {
e2b42eeb 3745 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
8f177c8e 3746 }
f85afd4e
MB
3747 }
3748}
3749
3750
2d66e025 3751void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3752{
2d66e025 3753 if ( event.LeftDown() )
f85afd4e 3754 {
2d66e025
MB
3755 // indicate corner label by having both row and
3756 // col args == -1
f85afd4e 3757 //
b54ba671 3758 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
3759 {
3760 SelectAll();
3761 }
f85afd4e
MB
3762 }
3763
2d66e025
MB
3764 else if ( event.LeftDClick() )
3765 {
b54ba671 3766 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 3767 }
8f177c8e 3768
2d66e025 3769 else if ( event.RightDown() )
f85afd4e 3770 {
b54ba671 3771 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 3772 {
2d66e025
MB
3773 // no default action at the moment
3774 }
3775 }
f85afd4e 3776
2d66e025
MB
3777 else if ( event.RightDClick() )
3778 {
b54ba671 3779 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
3780 {
3781 // no default action at the moment
3782 }
3783 }
3784}
f85afd4e 3785
e2b42eeb
VZ
3786void wxGrid::ChangeCursorMode(CursorMode mode,
3787 wxWindow *win,
3788 bool captureMouse)
3789{
3790#ifdef __WXDEBUG__
3791 static const wxChar *cursorModes[] =
3792 {
3793 _T("SELECT_CELL"),
3794 _T("RESIZE_ROW"),
3795 _T("RESIZE_COL"),
3796 _T("SELECT_ROW"),
3797 _T("SELECT_COL")
3798 };
3799
181bfffd
VZ
3800 wxLogTrace(_T("grid"),
3801 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
3802 win == m_colLabelWin ? _T("colLabelWin")
3803 : win ? _T("rowLabelWin")
3804 : _T("gridWin"),
3805 cursorModes[m_cursorMode], cursorModes[mode]);
3806#endif // __WXDEBUG__
3807
3808 if ( mode == m_cursorMode )
3809 return;
3810
3811 if ( !win )
3812 {
3813 // by default use the grid itself
3814 win = m_gridWin;
3815 }
3816
3817 if ( m_winCapture )
3818 {
3819 m_winCapture->ReleaseMouse();
3820 m_winCapture = (wxWindow *)NULL;
3821 }
3822
3823 m_cursorMode = mode;
3824
3825 switch ( m_cursorMode )
3826 {
3827 case WXGRID_CURSOR_RESIZE_ROW:
3828 win->SetCursor( m_rowResizeCursor );
3829 break;
3830
3831 case WXGRID_CURSOR_RESIZE_COL:
3832 win->SetCursor( m_colResizeCursor );
3833 break;
3834
3835 default:
3836 win->SetCursor( *wxSTANDARD_CURSOR );
3837 }
3838
3839 // we need to capture mouse when resizing
3840 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
3841 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
3842
3843 if ( captureMouse && resize )
3844 {
3845 win->CaptureMouse();
3846 m_winCapture = win;
3847 }
3848}
8f177c8e 3849
2d66e025
MB
3850void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
3851{
3852 int x, y;
3853 wxPoint pos( event.GetPosition() );
3854 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3855
2d66e025
MB
3856 wxGridCellCoords coords;
3857 XYToCell( x, y, coords );
60ff3b99 3858
2d66e025
MB
3859 if ( event.Dragging() )
3860 {
07296f0b
RD
3861 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3862
3863 // Don't start doing anything until the mouse has been drug at
3864 // least 3 pixels in any direction...
508011ce
VZ
3865 if (! m_isDragging)
3866 {
3867 if (m_startDragPos == wxDefaultPosition)
3868 {
07296f0b
RD
3869 m_startDragPos = pos;
3870 return;
3871 }
3872 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
3873 return;
3874 }
3875
2d66e025 3876 m_isDragging = TRUE;
2d66e025
MB
3877 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3878 {
f0102d2a
VZ
3879 // Hide the edit control, so it
3880 // won't interfer with drag-shrinking.
3881 if ( IsCellEditControlEnabled() )
a5777624 3882 {
f0102d2a 3883 HideCellEditControl();
a5777624
RD
3884 SaveEditControlValue();
3885 }
07296f0b
RD
3886
3887 // Have we captured the mouse yet?
508011ce
VZ
3888 if (! m_winCapture)
3889 {
07296f0b
RD
3890 m_winCapture = m_gridWin;
3891 m_winCapture->CaptureMouse();
3892 }
3893
2d66e025
MB
3894 if ( coords != wxGridNoCellCoords )
3895 {
3896 if ( !IsSelection() )
f85afd4e 3897 {
2d66e025 3898 SelectBlock( coords, coords );
f85afd4e
MB
3899 }
3900 else
3901 {
f0102d2a 3902 SelectBlock( m_currentCellCoords, coords );
f85afd4e 3903 }
07296f0b 3904
508011ce
VZ
3905 if (! IsVisible(coords))
3906 {
07296f0b
RD
3907 MakeCellVisible(coords);
3908 // TODO: need to introduce a delay or something here. The
3909 // scrolling is way to fast, at least on MSW.
3910 }
2d66e025
MB
3911 }
3912 }
6d004f67
MB
3913 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3914 {
3915 int cw, ch, left, dummy;
3916 m_gridWin->GetClientSize( &cw, &ch );
3917 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 3918
6d004f67
MB
3919 wxClientDC dc( m_gridWin );
3920 PrepareDC( dc );
7c1cb261 3921 y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
6d004f67
MB
3922 dc.SetLogicalFunction(wxINVERT);
3923 if ( m_dragLastPos >= 0 )
3924 {
3925 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
3926 }
3927 dc.DrawLine( left, y, left+cw, y );
3928 m_dragLastPos = y;
3929 }
3930 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3931 {
3932 int cw, ch, dummy, top;
3933 m_gridWin->GetClientSize( &cw, &ch );
3934 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 3935
6d004f67
MB
3936 wxClientDC dc( m_gridWin );
3937 PrepareDC( dc );
43947979
VZ
3938 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3939 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
3940 dc.SetLogicalFunction(wxINVERT);
3941 if ( m_dragLastPos >= 0 )
3942 {
3943 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3944 }
3945 dc.DrawLine( x, top, x, top+ch );
3946 m_dragLastPos = x;
3947 }
e2b42eeb 3948
2d66e025
MB
3949 return;
3950 }
66242c80 3951
2d66e025 3952 m_isDragging = FALSE;
07296f0b
RD
3953 m_startDragPos = wxDefaultPosition;
3954
a5777624
RD
3955 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3956 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3957 // wxGTK
e2b42eeb 3958#if 0
a5777624
RD
3959 if ( event.Entering() || event.Leaving() )
3960 {
3961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3962 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
3963 }
3964 else
e2b42eeb
VZ
3965#endif // 0
3966
a5777624
RD
3967 // ------------ Left button pressed
3968 //
3969 if ( event.LeftDown() && coords != wxGridNoCellCoords )
3970 {
3971 if ( event.ShiftDown() )
2d66e025 3972 {
a5777624
RD
3973 SelectBlock( m_currentCellCoords, coords );
3974 }
3975 else if ( XToEdgeOfCol(x) < 0 &&
3976 YToEdgeOfRow(y) < 0 )
3977 {
3978 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
3979 coords.GetRow(),
3980 coords.GetCol(),
3981 event ) )
58dd5b3b 3982 {
a5777624
RD
3983 DisableCellEditControl();
3984 MakeCellVisible( coords );
3985
3986 // if this is the second click on this cell then start
3987 // the edit control
3988 if ( m_waitForSlowClick &&
3989 (coords == m_currentCellCoords) &&
3990 CanEnableCellControl())
58dd5b3b 3991 {
a5777624 3992 EnableCellEditControl();
e195a54c 3993
a5777624
RD
3994 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
3995 attr->GetEditor(this, coords.GetRow(), coords.GetCol())->StartingClick();
3996 attr->DecRef();
e195a54c 3997
a5777624
RD
3998 m_waitForSlowClick = FALSE;
3999 }
4000 else
4001 {
4002 SetCurrentCell( coords );
4003 m_waitForSlowClick = TRUE;
58dd5b3b
MB
4004 }
4005 }
f85afd4e 4006 }
a5777624 4007 }
f85afd4e 4008
f85afd4e 4009
a5777624
RD
4010 // ------------ Left double click
4011 //
4012 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
4013 {
4014 DisableCellEditControl();
4015
4016 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 4017 {
a5777624
RD
4018 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
4019 coords.GetRow(),
4020 coords.GetCol(),
4021 event );
58dd5b3b 4022 }
a5777624 4023 }
f85afd4e 4024
8f177c8e 4025
a5777624
RD
4026 // ------------ Left button released
4027 //
4028 else if ( event.LeftUp() )
4029 {
4030 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4031 {
a5777624 4032 if ( IsSelection() )
52068ea5 4033 {
a5777624 4034 if (m_winCapture)
58dd5b3b 4035 {
a5777624
RD
4036 m_winCapture->ReleaseMouse();
4037 m_winCapture = NULL;
58dd5b3b 4038 }
a5777624 4039 SendEvent( wxEVT_GRID_RANGE_SELECT, -1, -1, event );
6d004f67 4040 }
2d66e025 4041
a5777624
RD
4042 // Show the edit control, if it has been hidden for
4043 // drag-shrinking.
4044 ShowCellEditControl();
4045 }
4046 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4047 {
4048 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4049 DoEndDragResizeRow();
e2b42eeb 4050
a5777624
RD
4051 // Note: we are ending the event *after* doing
4052 // default processing in this case
4053 //
4054 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4055 }
4056 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4057 {
4058 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4059 DoEndDragResizeCol();
e2b42eeb 4060
a5777624
RD
4061 // Note: we are ending the event *after* doing
4062 // default processing in this case
4063 //
4064 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 4065 }
f85afd4e 4066
a5777624
RD
4067 m_dragLastPos = -1;
4068 }
4069
f85afd4e 4070
a5777624
RD
4071 // ------------ Right button down
4072 //
4073 else if ( event.RightDown() && coords != wxGridNoCellCoords )
4074 {
4075 DisableCellEditControl();
4076 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
4077 coords.GetRow(),
4078 coords.GetCol(),
4079 event ) )
f85afd4e 4080 {
a5777624 4081 // no default action at the moment
60ff3b99 4082 }
a5777624 4083 }
2d66e025 4084
60ff3b99 4085
a5777624
RD
4086 // ------------ Right double click
4087 //
4088 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
4089 {
4090 DisableCellEditControl();
4091 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
4092 coords.GetRow(),
4093 coords.GetCol(),
4094 event ) )
f85afd4e 4095 {
a5777624 4096 // no default action at the moment
60ff3b99 4097 }
a5777624
RD
4098 }
4099
4100 // ------------ Moving and no button action
4101 //
4102 else if ( event.Moving() && !event.IsButton() )
4103 {
4104 int dragRow = YToEdgeOfRow( y );
4105 int dragCol = XToEdgeOfCol( x );
790cc417 4106
a5777624
RD
4107 // Dragging on the corner of a cell to resize in both
4108 // directions is not implemented yet...
790cc417 4109 //
a5777624 4110 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 4111 {
a5777624
RD
4112 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4113 return;
4114 }
6d004f67 4115
a5777624
RD
4116 if ( dragRow >= 0 )
4117 {
4118 m_dragRowOrCol = dragRow;
6d004f67 4119
a5777624 4120 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 4121 {
a5777624
RD
4122 if ( CanDragRowSize() && CanDragGridSize() )
4123 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 4124 }
e2b42eeb 4125
b94ae1ea
VZ
4126 if ( dragCol >= 0 )
4127 {
4128 m_dragRowOrCol = dragCol;
122603f1 4129 }
b94ae1ea 4130
a5777624
RD
4131 return;
4132 }
e2b42eeb 4133
a5777624
RD
4134 if ( dragCol >= 0 )
4135 {
4136 m_dragRowOrCol = dragCol;
e2b42eeb 4137
a5777624 4138 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 4139 {
a5777624
RD
4140 if ( CanDragColSize() && CanDragGridSize() )
4141 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67 4142 }
a5777624
RD
4143
4144 return;
6d004f67 4145 }
a5777624
RD
4146
4147 // Neither on a row or col edge
4148 //
4149 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4150 {
4151 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4152 }
4153 }
6d004f67
MB
4154}
4155
4156
4157void wxGrid::DoEndDragResizeRow()
4158{
4159 if ( m_dragLastPos >= 0 )
4160 {
4161 // erase the last line and resize the row
4162 //
4163 int cw, ch, left, dummy;
4164 m_gridWin->GetClientSize( &cw, &ch );
4165 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4166
4167 wxClientDC dc( m_gridWin );
4168 PrepareDC( dc );
4169 dc.SetLogicalFunction( wxINVERT );
4170 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4171 HideCellEditControl();
a5777624 4172 SaveEditControlValue();
6d004f67 4173
7c1cb261 4174 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67
MB
4175 SetRowSize( m_dragRowOrCol,
4176 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
e2b42eeb 4177
6d004f67
MB
4178 if ( !GetBatchCount() )
4179 {
4180 // Only needed to get the correct rect.y:
4181 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
4182 rect.x = 0;
4183 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
4184 rect.width = m_rowLabelWidth;
4185 rect.height = ch - rect.y;
4186 m_rowLabelWin->Refresh( TRUE, &rect );
4187 rect.width = cw;
4188 m_gridWin->Refresh( FALSE, &rect );
4189 }
4190
4191 ShowCellEditControl();
4192 }
4193}
4194
4195
4196void wxGrid::DoEndDragResizeCol()
4197{
4198 if ( m_dragLastPos >= 0 )
4199 {
4200 // erase the last line and resize the col
4201 //
4202 int cw, ch, dummy, top;
4203 m_gridWin->GetClientSize( &cw, &ch );
4204 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4205
4206 wxClientDC dc( m_gridWin );
4207 PrepareDC( dc );
4208 dc.SetLogicalFunction( wxINVERT );
4209 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4210 HideCellEditControl();
a5777624 4211 SaveEditControlValue();
6d004f67 4212
7c1cb261 4213 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 4214 SetColSize( m_dragRowOrCol,
43947979
VZ
4215 wxMax( m_dragLastPos - colLeft,
4216 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
4217
4218 if ( !GetBatchCount() )
4219 {
4220 // Only needed to get the correct rect.x:
4221 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
4222 rect.y = 0;
4223 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
4224 rect.width = cw - rect.x;
4225 rect.height = m_colLabelHeight;
4226 m_colLabelWin->Refresh( TRUE, &rect );
4227 rect.height = ch;
4228 m_gridWin->Refresh( FALSE, &rect );
790cc417 4229 }
e2b42eeb 4230
6d004f67 4231 ShowCellEditControl();
f85afd4e 4232 }
f85afd4e
MB
4233}
4234
4235
6d004f67 4236
2d66e025
MB
4237//
4238// ------ interaction with data model
4239//
4240bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 4241{
2d66e025 4242 switch ( msg.GetId() )
17732cec 4243 {
2d66e025
MB
4244 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4245 return GetModelValues();
17732cec 4246
2d66e025
MB
4247 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4248 return SetModelValues();
f85afd4e 4249
2d66e025
MB
4250 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4251 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4252 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4253 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4254 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4255 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4256 return Redimension( msg );
4257
4258 default:
4259 return FALSE;
f85afd4e 4260 }
2d66e025 4261}
f85afd4e 4262
f85afd4e 4263
f85afd4e 4264
2d66e025
MB
4265// The behaviour of this function depends on the grid table class
4266// Clear() function. For the default wxGridStringTable class the
4267// behavious is to replace all cell contents with wxEmptyString but
4268// not to change the number of rows or cols.
4269//
4270void wxGrid::ClearGrid()
4271{
4272 if ( m_table )
f85afd4e 4273 {
4cfa5de6
RD
4274 if (IsCellEditControlEnabled())
4275 DisableCellEditControl();
4276
2d66e025 4277 m_table->Clear();
1f1ce288 4278 if ( !GetBatchCount() ) m_gridWin->Refresh();
f85afd4e
MB
4279 }
4280}
4281
4282
2d66e025 4283bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4284{
2d66e025 4285 // TODO: something with updateLabels flag
8f177c8e 4286
2d66e025 4287 if ( !m_created )
f85afd4e 4288 {
bd3c6758 4289 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2d66e025
MB
4290 return FALSE;
4291 }
8f177c8e 4292
2d66e025
MB
4293 if ( m_table )
4294 {
b7fff980 4295 if (IsCellEditControlEnabled())
b54ba671 4296 DisableCellEditControl();
b7fff980 4297
2d66e025 4298 bool ok = m_table->InsertRows( pos, numRows );
f85afd4e 4299
2d66e025
MB
4300 // the table will have sent the results of the insert row
4301 // operation to this view object as a grid table message
4302 //
4303 if ( ok )
4304 {
4305 if ( m_numCols == 0 )
f85afd4e 4306 {
2d66e025
MB
4307 m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
4308 //
4309 // TODO: perhaps instead of appending the default number of cols
4310 // we should remember what the last non-zero number of cols was ?
4311 //
4312 }
8f177c8e 4313
2d66e025
MB
4314 if ( m_currentCellCoords == wxGridNoCellCoords )
4315 {
4316 // if we have just inserted cols into an empty grid the current
4317 // cell will be undefined...
4318 //
4319 SetCurrentCell( 0, 0 );
f85afd4e
MB
4320 }
4321
2d66e025
MB
4322 ClearSelection();
4323 if ( !GetBatchCount() ) Refresh();
f85afd4e 4324 }
2d66e025 4325
2d66e025
MB
4326 return ok;
4327 }
4328 else
4329 {
4330 return FALSE;
f85afd4e
MB
4331 }
4332}
4333
4334
2d66e025 4335bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4336{
2d66e025
MB
4337 // TODO: something with updateLabels flag
4338
4339 if ( !m_created )
f85afd4e 4340 {
bd3c6758 4341 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2d66e025
MB
4342 return FALSE;
4343 }
4344
4345 if ( m_table && m_table->AppendRows( numRows ) )
4346 {
4347 if ( m_currentCellCoords == wxGridNoCellCoords )
4348 {
4349 // if we have just inserted cols into an empty grid the current
4350 // cell will be undefined...
4351 //
4352 SetCurrentCell( 0, 0 );
4353 }
4354
4355 // the table will have sent the results of the append row
4356 // operation to this view object as a grid table message
4357 //
4358 ClearSelection();
4359 if ( !GetBatchCount() ) Refresh();
4360 return TRUE;
4361 }
4362 else
4363 {
4364 return FALSE;
f85afd4e
MB
4365 }
4366}
4367
2d66e025
MB
4368
4369bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4370{
2d66e025
MB
4371 // TODO: something with updateLabels flag
4372
4373 if ( !m_created )
f85afd4e 4374 {
bd3c6758 4375 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2d66e025
MB
4376 return FALSE;
4377 }
4378
b7fff980 4379 if ( m_table )
2d66e025 4380 {
b7fff980 4381 if (IsCellEditControlEnabled())
b54ba671 4382 DisableCellEditControl();
f85afd4e 4383
b7fff980
RD
4384 if (m_table->DeleteRows( pos, numRows ))
4385 {
4386
4387 // the table will have sent the results of the delete row
4388 // operation to this view object as a grid table message
4389 //
4390 ClearSelection();
4391 if ( !GetBatchCount() ) Refresh();
4392 return TRUE;
4393 }
2d66e025 4394 }
b7fff980 4395 return FALSE;
2d66e025 4396}
f85afd4e 4397
f85afd4e 4398
2d66e025
MB
4399bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4400{
4401 // TODO: something with updateLabels flag
8f177c8e 4402
2d66e025
MB
4403 if ( !m_created )
4404 {
bd3c6758 4405 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2d66e025
MB
4406 return FALSE;
4407 }
f85afd4e 4408
2d66e025
MB
4409 if ( m_table )
4410 {
b7fff980 4411 if (IsCellEditControlEnabled())
b54ba671 4412 DisableCellEditControl();
b7fff980 4413
2d66e025
MB
4414 bool ok = m_table->InsertCols( pos, numCols );
4415
4416 // the table will have sent the results of the insert col
4417 // operation to this view object as a grid table message
4418 //
4419 if ( ok )
f85afd4e 4420 {
2d66e025 4421 if ( m_currentCellCoords == wxGridNoCellCoords )
f85afd4e 4422 {
2d66e025
MB
4423 // if we have just inserted cols into an empty grid the current
4424 // cell will be undefined...
4425 //
4426 SetCurrentCell( 0, 0 );
f85afd4e 4427 }
2d66e025
MB
4428
4429 ClearSelection();
4430 if ( !GetBatchCount() ) Refresh();
f85afd4e 4431 }
2d66e025 4432
2d66e025
MB
4433 return ok;
4434 }
4435 else
4436 {
4437 return FALSE;
f85afd4e
MB
4438 }
4439}
4440
2d66e025
MB
4441
4442bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 4443{
2d66e025
MB
4444 // TODO: something with updateLabels flag
4445
4446 if ( !m_created )
f85afd4e 4447 {
bd3c6758 4448 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2d66e025
MB
4449 return FALSE;
4450 }
f85afd4e 4451
2d66e025
MB
4452 if ( m_table && m_table->AppendCols( numCols ) )
4453 {
4454 // the table will have sent the results of the append col
4455 // operation to this view object as a grid table message
4456 //
4457 if ( m_currentCellCoords == wxGridNoCellCoords )
f85afd4e 4458 {
2d66e025
MB
4459 // if we have just inserted cols into an empty grid the current
4460 // cell will be undefined...
4461 //
4462 SetCurrentCell( 0, 0 );
f85afd4e 4463 }
8f177c8e 4464
2d66e025
MB
4465 ClearSelection();
4466 if ( !GetBatchCount() ) Refresh();
4467 return TRUE;
f85afd4e 4468 }
2d66e025 4469 else
f85afd4e 4470 {
2d66e025 4471 return FALSE;
f85afd4e 4472 }
f85afd4e
MB
4473}
4474
4475
2d66e025 4476bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 4477{
2d66e025 4478 // TODO: something with updateLabels flag
8f177c8e 4479
2d66e025 4480 if ( !m_created )
f85afd4e 4481 {
bd3c6758 4482 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2d66e025 4483 return FALSE;
f85afd4e
MB
4484 }
4485
b7fff980 4486 if ( m_table )
2d66e025 4487 {
b7fff980 4488 if (IsCellEditControlEnabled())
b54ba671 4489 DisableCellEditControl();
8f177c8e 4490
b7fff980
RD
4491 if ( m_table->DeleteCols( pos, numCols ) )
4492 {
4493 // the table will have sent the results of the delete col
4494 // operation to this view object as a grid table message
4495 //
4496 ClearSelection();
4497 if ( !GetBatchCount() ) Refresh();
4498 return TRUE;
4499 }
f85afd4e 4500 }
b7fff980 4501 return FALSE;
f85afd4e
MB
4502}
4503
4504
2d66e025
MB
4505
4506//
4507// ----- event handlers
f85afd4e 4508//
8f177c8e 4509
2d66e025
MB
4510// Generate a grid event based on a mouse event and
4511// return the result of ProcessEvent()
4512//
4513bool wxGrid::SendEvent( const wxEventType type,
4514 int row, int col,
4515 wxMouseEvent& mouseEv )
4516{
b54ba671 4517 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 4518 {
2d66e025
MB
4519 int rowOrCol = (row == -1 ? col : row);
4520
4521 wxGridSizeEvent gridEvt( GetId(),
4522 type,
4523 this,
4524 rowOrCol,
4525 mouseEv.GetX(), mouseEv.GetY(),
4526 mouseEv.ControlDown(),
4527 mouseEv.ShiftDown(),
4528 mouseEv.AltDown(),
4529 mouseEv.MetaDown() );
4530
4531 return GetEventHandler()->ProcessEvent(gridEvt);
f85afd4e 4532 }
b54ba671 4533 else if ( type == wxEVT_GRID_RANGE_SELECT )
2d66e025
MB
4534 {
4535 wxGridRangeSelectEvent gridEvt( GetId(),
4536 type,
4537 this,
4538 m_selectedTopLeft,
4539 m_selectedBottomRight,
4540 mouseEv.ControlDown(),
4541 mouseEv.ShiftDown(),
4542 mouseEv.AltDown(),
4543 mouseEv.MetaDown() );
f85afd4e 4544
2d66e025
MB
4545 return GetEventHandler()->ProcessEvent(gridEvt);
4546 }
4547 else
4548 {
4549 wxGridEvent gridEvt( GetId(),
4550 type,
4551 this,
4552 row, col,
4553 mouseEv.GetX(), mouseEv.GetY(),
4554 mouseEv.ControlDown(),
4555 mouseEv.ShiftDown(),
4556 mouseEv.AltDown(),
4557 mouseEv.MetaDown() );
8f177c8e 4558
2d66e025
MB
4559 return GetEventHandler()->ProcessEvent(gridEvt);
4560 }
f85afd4e
MB
4561}
4562
4563
2d66e025
MB
4564// Generate a grid event of specified type and return the result
4565// of ProcessEvent().
f85afd4e 4566//
2d66e025
MB
4567bool wxGrid::SendEvent( const wxEventType type,
4568 int row, int col )
f85afd4e 4569{
b54ba671 4570 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 4571 {
2d66e025 4572 int rowOrCol = (row == -1 ? col : row);
f85afd4e 4573
2d66e025
MB
4574 wxGridSizeEvent gridEvt( GetId(),
4575 type,
4576 this,
4577 rowOrCol );
4578
4579 return GetEventHandler()->ProcessEvent(gridEvt);
4580 }
4581 else
4582 {
4583 wxGridEvent gridEvt( GetId(),
4584 type,
4585 this,
4586 row, col );
8f177c8e 4587
2d66e025
MB
4588 return GetEventHandler()->ProcessEvent(gridEvt);
4589 }
f85afd4e
MB
4590}
4591
4592
2d66e025 4593void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 4594{
2d66e025 4595 wxPaintDC dc( this );
f85afd4e 4596
2d66e025
MB
4597 if ( m_currentCellCoords == wxGridNoCellCoords &&
4598 m_numRows && m_numCols )
f85afd4e 4599 {
2d66e025 4600 m_currentCellCoords.Set(0, 0);
2d66e025 4601 ShowCellEditControl();
f85afd4e 4602 }
4634a5d6
MB
4603
4604 m_displayed = TRUE;
8f177c8e 4605}
f85afd4e
MB
4606
4607
18f9565d
MB
4608// This is just here to make sure that CalcDimensions gets called when
4609// the grid view is resized... then the size event is skipped to allow
4610// the box sizers to handle everything
4611//
2d66e025 4612void wxGrid::OnSize( wxSizeEvent& event )
f85afd4e 4613{
7807d81c 4614 CalcWindowSizes();
2d66e025 4615 CalcDimensions();
f85afd4e
MB
4616}
4617
f85afd4e 4618
2d66e025 4619void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 4620{
2d66e025 4621 if ( m_inOnKeyDown )
f85afd4e 4622 {
2d66e025
MB
4623 // shouldn't be here - we are going round in circles...
4624 //
07296f0b 4625 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
4626 }
4627
2d66e025 4628 m_inOnKeyDown = TRUE;
f85afd4e 4629
2d66e025
MB
4630 // propagate the event up and see if it gets processed
4631 //
4632 wxWindow *parent = GetParent();
4633 wxKeyEvent keyEvt( event );
4634 keyEvt.SetEventObject( parent );
4635
4636 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 4637 {
9b4aede2
RD
4638
4639 // TODO: Should also support Shift-cursor keys for
4640 // extending the selection. Maybe add a flag to
4641 // MoveCursorXXX() and MoveCursorXXXBlock() and
4642 // just send event.ShiftDown().
4643
2d66e025
MB
4644 // try local handlers
4645 //
4646 switch ( event.KeyCode() )
4647 {
4648 case WXK_UP:
4649 if ( event.ControlDown() )
4650 {
4651 MoveCursorUpBlock();
4652 }
4653 else
4654 {
4655 MoveCursorUp();
4656 }
4657 break;
f85afd4e 4658
2d66e025
MB
4659 case WXK_DOWN:
4660 if ( event.ControlDown() )
4661 {
4662 MoveCursorDownBlock();
4663 }
4664 else
4665 {
4666 MoveCursorDown();
4667 }
4668 break;
8f177c8e 4669
2d66e025
MB
4670 case WXK_LEFT:
4671 if ( event.ControlDown() )
4672 {
4673 MoveCursorLeftBlock();
4674 }
4675 else
4676 {
4677 MoveCursorLeft();
4678 }
4679 break;
4680
4681 case WXK_RIGHT:
4682 if ( event.ControlDown() )
4683 {
4684 MoveCursorRightBlock();
4685 }
4686 else
4687 {
4688 MoveCursorRight();
4689 }
4690 break;
b99be8fb 4691
2d66e025 4692 case WXK_RETURN:
58dd5b3b
MB
4693 if ( event.ControlDown() )
4694 {
4695 event.Skip(); // to let the edit control have the return
4696 }
4697 else
4698 {
4699 MoveCursorDown();
4700 }
2d66e025
MB
4701 break;
4702
2c9a89e0
RD
4703 case WXK_TAB:
4704 if (event.ShiftDown())
4705 MoveCursorLeft();
4706 else
4707 MoveCursorRight();
4708 break;
4709
2d66e025
MB
4710 case WXK_HOME:
4711 if ( event.ControlDown() )
4712 {
4713 MakeCellVisible( 0, 0 );
4714 SetCurrentCell( 0, 0 );
4715 }
4716 else
4717 {
4718 event.Skip();
4719 }
4720 break;
4721
4722 case WXK_END:
4723 if ( event.ControlDown() )
4724 {
4725 MakeCellVisible( m_numRows-1, m_numCols-1 );
4726 SetCurrentCell( m_numRows-1, m_numCols-1 );
4727 }
4728 else
4729 {
4730 event.Skip();
4731 }
4732 break;
4733
4734 case WXK_PRIOR:
4735 MovePageUp();
4736 break;
4737
4738 case WXK_NEXT:
4739 MovePageDown();
4740 break;
4741
07296f0b
RD
4742 case WXK_SPACE:
4743 if ( !IsEditable() )
4744 {
4745 MoveCursorRight();
4746 break;
4747 }
4748 // Otherwise fall through to default
4749
2d66e025 4750 default:
c239a4bb
VZ
4751 // alphanumeric keys or F2 (special key just for this) enable
4752 // the cell edit control
b94ae1ea
VZ
4753 if ( !(event.AltDown() ||
4754 event.MetaDown() ||
4755 event.ControlDown()) &&
c239a4bb 4756 (isalnum(event.KeyCode()) || event.KeyCode() == WXK_F2) &&
b94ae1ea
VZ
4757 !IsCellEditControlEnabled() &&
4758 CanEnableCellControl() )
b54ba671
VZ
4759 {
4760 EnableCellEditControl();
f2d76237
RD
4761 int row = m_currentCellCoords.GetRow();
4762 int col = m_currentCellCoords.GetCol();
4763 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 4764 attr->GetEditor(this, row, col)->StartingKey(event);
2c9a89e0
RD
4765 attr->DecRef();
4766 }
b3a7510d
VZ
4767 else
4768 {
b94ae1ea
VZ
4769 // let others process char events with modifiers or all
4770 // char events for readonly cells
b3a7510d
VZ
4771 event.Skip();
4772 }
025562fe 4773 break;
2d66e025 4774 }
f85afd4e
MB
4775 }
4776
2d66e025 4777 m_inOnKeyDown = FALSE;
f85afd4e
MB
4778}
4779
07296f0b 4780
2796cce3 4781void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
4782{
4783}
07296f0b 4784
2d66e025 4785void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 4786{
b54ba671 4787 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
66242c80 4788 {
2d66e025
MB
4789 // the event has been intercepted - do nothing
4790 return;
4791 }
66242c80 4792
4634a5d6
MB
4793 if ( m_displayed &&
4794 m_currentCellCoords != wxGridNoCellCoords )
2d66e025 4795 {
2d66e025 4796 HideCellEditControl();
b54ba671 4797 DisableCellEditControl();
07296f0b
RD
4798
4799 // Clear the old current cell highlight
4800 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
d1c0b4f9
VZ
4801
4802 // Otherwise refresh redraws the highlight!
4803 m_currentCellCoords = coords;
4804
07296f0b 4805 m_gridWin->Refresh( FALSE, &r );
66242c80 4806 }
8f177c8e 4807
2d66e025
MB
4808 m_currentCellCoords = coords;
4809
4634a5d6 4810 if ( m_displayed )
2d66e025 4811 {
07296f0b
RD
4812 wxClientDC dc(m_gridWin);
4813 PrepareDC(dc);
283b7808
VZ
4814
4815 wxGridCellAttr* attr = GetCellAttr(coords);
4816 DrawCellHighlight(dc, attr);
4817 attr->DecRef();
4634a5d6
MB
4818
4819 if ( IsSelection() )
4820 {
4821 wxRect r( SelectionToDeviceRect() );
4822 ClearSelection();
4823 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
4824 }
2d66e025 4825 }
66242c80
MB
4826}
4827
2d66e025
MB
4828
4829//
4830// ------ functions to get/send data (see also public functions)
4831//
4832
4833bool wxGrid::GetModelValues()
66242c80 4834{
2d66e025 4835 if ( m_table )
66242c80 4836 {
2d66e025 4837 // all we need to do is repaint the grid
66242c80 4838 //
2d66e025 4839 m_gridWin->Refresh();
66242c80
MB
4840 return TRUE;
4841 }
8f177c8e 4842
66242c80
MB
4843 return FALSE;
4844}
4845
2d66e025
MB
4846
4847bool wxGrid::SetModelValues()
f85afd4e 4848{
2d66e025 4849 int row, col;
8f177c8e 4850
2d66e025
MB
4851 if ( m_table )
4852 {
4853 for ( row = 0; row < m_numRows; row++ )
f85afd4e 4854 {
2d66e025 4855 for ( col = 0; col < m_numCols; col++ )
f85afd4e 4856 {
2d66e025 4857 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
4858 }
4859 }
8f177c8e 4860
f85afd4e
MB
4861 return TRUE;
4862 }
4863
4864 return FALSE;
4865}
4866
2d66e025
MB
4867
4868
4869// Note - this function only draws cells that are in the list of
4870// exposed cells (usually set from the update region by
4871// CalcExposedCells)
4872//
4873void wxGrid::DrawGridCellArea( wxDC& dc )
f85afd4e 4874{
2d66e025 4875 if ( !m_numRows || !m_numCols ) return;
60ff3b99 4876
2d66e025
MB
4877 size_t i;
4878 size_t numCells = m_cellsExposed.GetCount();
60ff3b99 4879
2d66e025 4880 for ( i = 0; i < numCells; i++ )
f85afd4e 4881 {
2d66e025
MB
4882 DrawCell( dc, m_cellsExposed[i] );
4883 }
4884}
8f177c8e 4885
8f177c8e 4886
7c8a8ad5
MB
4887void wxGrid::DrawGridSpace( wxDC& dc )
4888{
4889 if ( m_numRows && m_numCols )
4890 {
4891 int cw, ch;
4892 m_gridWin->GetClientSize( &cw, &ch );
4893
4894 int right, bottom;
4895 CalcUnscrolledPosition( cw, ch, &right, &bottom );
4896
4897 if ( right > GetColRight(m_numCols-1) ||
4898 bottom > GetRowBottom(m_numRows-1) )
4899 {
4900 int left, top;
4901 CalcUnscrolledPosition( 0, 0, &left, &top );
4902
4903 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
4904 dc.SetPen( *wxTRANSPARENT_PEN );
4905
4906 if ( right > GetColRight(m_numCols-1) )
a5777624 4907 dc.DrawRectangle( GetColRight(m_numCols-1), top,
7c8a8ad5
MB
4908 right - GetColRight(m_numCols-1), ch );
4909
4910 if ( bottom > GetRowBottom(m_numRows-1) )
a5777624 4911 dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
7c8a8ad5
MB
4912 cw, bottom - GetRowBottom(m_numRows-1) );
4913 }
4914 }
4915}
4916
4917
2d66e025
MB
4918void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
4919{
ab79958a
VZ
4920 int row = coords.GetRow();
4921 int col = coords.GetCol();
60ff3b99 4922
7c1cb261 4923 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
4924 return;
4925
4926 // we draw the cell border ourselves
9496deb5 4927#if !WXGRID_DRAW_LINES
2d66e025
MB
4928 if ( m_gridLinesEnabled )
4929 DrawCellBorder( dc, coords );
796df70a 4930#endif
f85afd4e 4931
189d0213
VZ
4932 wxGridCellAttr* attr = GetCellAttr(row, col);
4933
4934 bool isCurrent = coords == m_currentCellCoords;
508011ce 4935
ab79958a 4936 wxRect rect;
7c1cb261
VZ
4937 rect.x = GetColLeft(col);
4938 rect.y = GetRowTop(row);
4939 rect.width = GetColWidth(col) - 1;
4940 rect.height = GetRowHeight(row) - 1;
f85afd4e 4941
189d0213
VZ
4942 // if the editor is shown, we should use it and not the renderer
4943 if ( isCurrent && IsCellEditControlEnabled() )
4944 {
28a77bc4 4945 attr->GetEditor(this, row, col)->PaintBackground(rect, attr);
189d0213
VZ
4946 }
4947 else
4948 {
4949 // but all the rest is drawn by the cell renderer and hence may be
4950 // customized
28a77bc4 4951 attr->GetRenderer(this, row, col)->
f2d76237
RD
4952 Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
4953
189d0213 4954 }
07296f0b 4955
283b7808
VZ
4956 attr->DecRef();
4957}
07296f0b 4958
283b7808 4959void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
4960{
4961 int row = m_currentCellCoords.GetRow();
4962 int col = m_currentCellCoords.GetCol();
4963
7c1cb261 4964 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
4965 return;
4966
4967 wxRect rect;
7c1cb261
VZ
4968 rect.x = GetColLeft(col);
4969 rect.y = GetRowTop(row);
4970 rect.width = GetColWidth(col) - 1;
4971 rect.height = GetRowHeight(row) - 1;
07296f0b 4972
b3a7510d
VZ
4973 // hmmm... what could we do here to show that the cell is disabled?
4974 // for now, I just draw a thinner border than for the other ones, but
4975 // it doesn't look really good
4976 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
4977 dc.SetBrush(*wxTRANSPARENT_BRUSH);
4978
4979 dc.DrawRectangle(rect);
07296f0b 4980
283b7808 4981#if 0
b3a7510d 4982 // VZ: my experiments with 3d borders...
283b7808 4983
b3a7510d 4984 // how to properly set colours for arbitrary bg?
283b7808
VZ
4985 wxCoord x1 = rect.x,
4986 y1 = rect.y,
00cf1208
RR
4987 x2 = rect.x + rect.width -1,
4988 y2 = rect.y + rect.height -1;
283b7808
VZ
4989
4990 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
4991 dc.DrawLine(x1, y1, x2, y1);
4992 dc.DrawLine(x1, y1, x1, y2);
283b7808 4993
283b7808 4994 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
00cf1208 4995 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
283b7808
VZ
4996
4997 dc.SetPen(*wxBLACK_PEN);
4998 dc.DrawLine(x1, y2, x2, y2);
00cf1208 4999 dc.DrawLine(x2, y1, x2, y2+1);
b3a7510d 5000#endif // 0
2d66e025 5001}
f85afd4e 5002
f2d76237 5003
2d66e025
MB
5004void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5005{
2d66e025
MB
5006 int row = coords.GetRow();
5007 int col = coords.GetCol();
7c1cb261
VZ
5008 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5009 return;
5010
5011 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
60ff3b99 5012
2d66e025
MB
5013 // right hand border
5014 //
7c1cb261
VZ
5015 dc.DrawLine( GetColRight(col), GetRowTop(row),
5016 GetColRight(col), GetRowBottom(row) );
2d66e025
MB
5017
5018 // bottom border
5019 //
7c1cb261
VZ
5020 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
5021 GetColRight(col), GetRowBottom(row) );
f85afd4e
MB
5022}
5023
b3a7510d
VZ
5024void wxGrid::DrawHighlight(wxDC& dc)
5025{
99306db2
VZ
5026 if ( IsCellEditControlEnabled() )
5027 {
5028 // don't show highlight when the edit control is shown
5029 return;
5030 }
5031
b3a7510d
VZ
5032 // if the active cell was repainted, repaint its highlight too because it
5033 // might have been damaged by the grid lines
5034 size_t count = m_cellsExposed.GetCount();
5035 for ( size_t n = 0; n < count; n++ )
5036 {
5037 if ( m_cellsExposed[n] == m_currentCellCoords )
5038 {
5039 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5040 DrawCellHighlight(dc, attr);
5041 attr->DecRef();
5042
5043 break;
5044 }
5045 }
5046}
2d66e025 5047
2d66e025
MB
5048// TODO: remove this ???
5049// This is used to redraw all grid lines e.g. when the grid line colour
5050// has been changed
5051//
796df70a 5052void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg )
f85afd4e 5053{
60ff3b99
VZ
5054 if ( !m_gridLinesEnabled ||
5055 !m_numRows ||
2d66e025 5056 !m_numCols ) return;
f85afd4e 5057
2d66e025 5058 int top, bottom, left, right;
796df70a 5059
8e217128 5060#ifndef __WXGTK__
508011ce
VZ
5061 if (reg.IsEmpty())
5062 {
796df70a
SN
5063 int cw, ch;
5064 m_gridWin->GetClientSize(&cw, &ch);
5065
5066 // virtual coords of visible area
5067 //
5068 CalcUnscrolledPosition( 0, 0, &left, &top );
5069 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5070 }
508011ce
VZ
5071 else
5072 {
796df70a
SN
5073 wxCoord x, y, w, h;
5074 reg.GetBox(x, y, w, h);
5075 CalcUnscrolledPosition( x, y, &left, &top );
5076 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
5077 }
8e217128
RR
5078#else
5079 int cw, ch;
5080 m_gridWin->GetClientSize(&cw, &ch);
5081 CalcUnscrolledPosition( 0, 0, &left, &top );
5082 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5083#endif
f85afd4e 5084
9496deb5
MB
5085 // avoid drawing grid lines past the last row and col
5086 //
7c1cb261
VZ
5087 right = wxMin( right, GetColRight(m_numCols - 1) );
5088 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 5089
2d66e025 5090 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
f85afd4e 5091
2d66e025 5092 // horizontal grid lines
f85afd4e 5093 //
60ff3b99 5094 int i;
9496deb5 5095 for ( i = 0; i < m_numRows; i++ )
f85afd4e 5096 {
6d55126d 5097 int bot = GetRowBottom(i) - 1;
7c1cb261 5098
6d55126d 5099 if ( bot > bottom )
2d66e025
MB
5100 {
5101 break;
5102 }
7c1cb261 5103
6d55126d 5104 if ( bot >= top )
2d66e025 5105 {
6d55126d 5106 dc.DrawLine( left, bot, right, bot );
2d66e025 5107 }
f85afd4e
MB
5108 }
5109
f85afd4e 5110
2d66e025
MB
5111 // vertical grid lines
5112 //
9496deb5 5113 for ( i = 0; i < m_numCols; i++ )
f85afd4e 5114 {
7c1cb261
VZ
5115 int colRight = GetColRight(i) - 1;
5116 if ( colRight > right )
2d66e025
MB
5117 {
5118 break;
5119 }
7c1cb261
VZ
5120
5121 if ( colRight >= left )
2d66e025 5122 {
7c1cb261 5123 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
5124 }
5125 }
5126}
f85afd4e 5127
8f177c8e 5128
2d66e025
MB
5129void wxGrid::DrawRowLabels( wxDC& dc )
5130{
5131 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5132
2d66e025
MB
5133 size_t i;
5134 size_t numLabels = m_rowLabelsExposed.GetCount();
60ff3b99 5135
2d66e025
MB
5136 for ( i = 0; i < numLabels; i++ )
5137 {
5138 DrawRowLabel( dc, m_rowLabelsExposed[i] );
60ff3b99 5139 }
f85afd4e
MB
5140}
5141
5142
2d66e025 5143void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 5144{
7c1cb261
VZ
5145 if ( GetRowHeight(row) <= 0 )
5146 return;
60ff3b99 5147
7c1cb261
VZ
5148 int rowTop = GetRowTop(row),
5149 rowBottom = GetRowBottom(row) - 1;
b99be8fb 5150
2d66e025 5151 dc.SetPen( *wxBLACK_PEN );
b0d6ff2f 5152 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7c1cb261 5153 m_rowLabelWidth-1, rowBottom );
b99be8fb 5154
7c1cb261 5155 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
b99be8fb 5156
2d66e025 5157 dc.SetPen( *wxWHITE_PEN );
7c1cb261 5158 dc.DrawLine( 0, rowTop, 0, rowBottom );
b0d6ff2f 5159 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
60ff3b99 5160
f85afd4e 5161 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
5162 dc.SetTextForeground( GetLabelTextColour() );
5163 dc.SetFont( GetLabelFont() );
8f177c8e 5164
f85afd4e 5165 int hAlign, vAlign;
2d66e025 5166 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 5167
2d66e025
MB
5168 wxRect rect;
5169 rect.SetX( 2 );
7c1cb261 5170 rect.SetY( GetRowTop(row) + 2 );
2d66e025 5171 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 5172 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 5173 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
5174}
5175
5176
2d66e025 5177void wxGrid::DrawColLabels( wxDC& dc )
f85afd4e 5178{
2d66e025 5179 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5180
2d66e025
MB
5181 size_t i;
5182 size_t numLabels = m_colLabelsExposed.GetCount();
60ff3b99 5183
2d66e025 5184 for ( i = 0; i < numLabels; i++ )
f85afd4e 5185 {
2d66e025 5186 DrawColLabel( dc, m_colLabelsExposed[i] );
60ff3b99 5187 }
f85afd4e
MB
5188}
5189
5190
2d66e025 5191void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 5192{
7c1cb261
VZ
5193 if ( GetColWidth(col) <= 0 )
5194 return;
60ff3b99 5195
7c1cb261
VZ
5196 int colLeft = GetColLeft(col),
5197 colRight = GetColRight(col) - 1;
b99be8fb 5198
2d66e025 5199 dc.SetPen( *wxBLACK_PEN );
7c1cb261
VZ
5200 dc.DrawLine( colRight, 0,
5201 colRight, m_colLabelHeight-1 );
b99be8fb 5202
b0d6ff2f 5203 dc.DrawLine( colLeft, m_colLabelHeight-1,
7c1cb261 5204 colRight, m_colLabelHeight-1 );
b99be8fb 5205
f85afd4e 5206 dc.SetPen( *wxWHITE_PEN );
b0d6ff2f 5207 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
7c1cb261 5208 dc.DrawLine( colLeft, 0, colRight, 0 );
f85afd4e 5209
b0d6ff2f
MB
5210 dc.SetBackgroundMode( wxTRANSPARENT );
5211 dc.SetTextForeground( GetLabelTextColour() );
5212 dc.SetFont( GetLabelFont() );
8f177c8e 5213
f85afd4e 5214 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
5215 dc.SetTextForeground( GetLabelTextColour() );
5216 dc.SetFont( GetLabelFont() );
8f177c8e 5217
f85afd4e 5218 int hAlign, vAlign;
2d66e025 5219 GetColLabelAlignment( &hAlign, &vAlign );
60ff3b99 5220
2d66e025 5221 wxRect rect;
7c1cb261 5222 rect.SetX( colLeft + 2 );
2d66e025 5223 rect.SetY( 2 );
7c1cb261 5224 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 5225 rect.SetHeight( m_colLabelHeight - 4 );
60ff3b99 5226 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
f85afd4e
MB
5227}
5228
5229
2d66e025
MB
5230void wxGrid::DrawTextRectangle( wxDC& dc,
5231 const wxString& value,
5232 const wxRect& rect,
5233 int horizAlign,
5234 int vertAlign )
f85afd4e 5235{
2d66e025
MB
5236 long textWidth, textHeight;
5237 long lineWidth, lineHeight;
5238 wxArrayString lines;
f85afd4e 5239
2d66e025
MB
5240 dc.SetClippingRegion( rect );
5241 StringToLines( value, lines );
5242 if ( lines.GetCount() )
5243 {
5244 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5245 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
f85afd4e 5246
2d66e025
MB
5247 float x, y;
5248 switch ( horizAlign )
5249 {
5250 case wxRIGHT:
5251 x = rect.x + (rect.width - textWidth - 1);
5252 break;
f85afd4e 5253
2d66e025
MB
5254 case wxCENTRE:
5255 x = rect.x + ((rect.width - textWidth)/2);
5256 break;
f85afd4e 5257
2d66e025
MB
5258 case wxLEFT:
5259 default:
5260 x = rect.x + 1;
5261 break;
5262 }
f85afd4e 5263
2d66e025
MB
5264 switch ( vertAlign )
5265 {
5266 case wxBOTTOM:
5267 y = rect.y + (rect.height - textHeight - 1);
5268 break;
f85afd4e
MB
5269
5270 case wxCENTRE:
8f177c8e 5271 y = rect.y + ((rect.height - textHeight)/2);
f85afd4e
MB
5272 break;
5273
5274 case wxTOP:
5275 default:
8f177c8e 5276 y = rect.y + 1;
f85afd4e
MB
5277 break;
5278 }
5279
b540eb2b 5280 for ( size_t i = 0; i < lines.GetCount(); i++ )
f85afd4e
MB
5281 {
5282 dc.DrawText( lines[i], (long)x, (long)y );
5283 y += lineHeight;
5284 }
5285 }
8f177c8e 5286
f85afd4e 5287 dc.DestroyClippingRegion();
f85afd4e
MB
5288}
5289
5290
5291// Split multi line text up into an array of strings. Any existing
5292// contents of the string array are preserved.
5293//
5294void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5295{
f85afd4e
MB
5296 int startPos = 0;
5297 int pos;
6d004f67 5298 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 5299 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 5300
2433bb2e 5301 while ( startPos < (int)tVal.Length() )
f85afd4e 5302 {
2433bb2e 5303 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
5304 if ( pos < 0 )
5305 {
5306 break;
5307 }
5308 else if ( pos == 0 )
5309 {
5310 lines.Add( wxEmptyString );
5311 }
5312 else
5313 {
2433bb2e 5314 lines.Add( value.Mid(startPos, pos) );
f85afd4e
MB
5315 }
5316 startPos += pos+1;
5317 }
b540eb2b 5318 if ( startPos < (int)value.Length() )
f85afd4e
MB
5319 {
5320 lines.Add( value.Mid( startPos ) );
5321 }
5322}
5323
5324
5325void wxGrid::GetTextBoxSize( wxDC& dc,
5326 wxArrayString& lines,
5327 long *width, long *height )
5328{
5329 long w = 0;
5330 long h = 0;
5331 long lineW, lineH;
5332
b540eb2b 5333 size_t i;
f85afd4e
MB
5334 for ( i = 0; i < lines.GetCount(); i++ )
5335 {
5336 dc.GetTextExtent( lines[i], &lineW, &lineH );
5337 w = wxMax( w, lineW );
5338 h += lineH;
5339 }
5340
5341 *width = w;
5342 *height = h;
5343}
5344
5345
5346//
2d66e025 5347// ------ Edit control functions
f85afd4e
MB
5348//
5349
2d66e025
MB
5350
5351void wxGrid::EnableEditing( bool edit )
f85afd4e 5352{
2d66e025
MB
5353 // TODO: improve this ?
5354 //
5355 if ( edit != m_editable )
f85afd4e 5356 {
2d66e025 5357 m_editable = edit;
1f1ce288 5358
b54ba671
VZ
5359 // FIXME IMHO this won't disable the edit control if edit == FALSE
5360 // because of the check in the beginning of
5361 // EnableCellEditControl() just below (VZ)
2c9a89e0 5362 EnableCellEditControl(m_editable);
f85afd4e 5363 }
f85afd4e
MB
5364}
5365
5366
2d66e025 5367void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 5368{
2c9a89e0
RD
5369 if (! m_editable)
5370 return;
5371
dcfe4c3d
SN
5372 if ( m_currentCellCoords == wxGridNoCellCoords )
5373 SetCurrentCell( 0, 0 );
60ff3b99 5374
2c9a89e0
RD
5375 if ( enable != m_cellEditCtrlEnabled )
5376 {
b54ba671
VZ
5377 // TODO allow the app to Veto() this event?
5378 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5379
dcfe4c3d 5380 if ( enable )
f85afd4e 5381 {
b54ba671
VZ
5382 // this should be checked by the caller!
5383 wxASSERT_MSG( CanEnableCellControl(),
5384 _T("can't enable editing for this cell!") );
5385
5386 // do it before ShowCellEditControl()
dcfe4c3d 5387 m_cellEditCtrlEnabled = enable;
b54ba671 5388
2d66e025 5389 ShowCellEditControl();
2d66e025
MB
5390 }
5391 else
5392 {
2d66e025
MB
5393 HideCellEditControl();
5394 SaveEditControlValue();
b54ba671
VZ
5395
5396 // do it after HideCellEditControl()
dcfe4c3d 5397 m_cellEditCtrlEnabled = enable;
f85afd4e 5398 }
f85afd4e 5399 }
f85afd4e 5400}
f85afd4e 5401
b54ba671 5402bool wxGrid::IsCurrentCellReadOnly() const
283b7808 5403{
b54ba671
VZ
5404 // const_cast
5405 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5406 bool readonly = attr->IsReadOnly();
5407 attr->DecRef();
283b7808 5408
b54ba671
VZ
5409 return readonly;
5410}
283b7808 5411
b54ba671
VZ
5412bool wxGrid::CanEnableCellControl() const
5413{
5414 return m_editable && !IsCurrentCellReadOnly();
5415}
5416
5417bool wxGrid::IsCellEditControlEnabled() const
5418{
5419 // the cell edit control might be disable for all cells or just for the
5420 // current one if it's read only
5421 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
283b7808
VZ
5422}
5423
2d66e025 5424void wxGrid::ShowCellEditControl()
f85afd4e 5425{
2d66e025 5426 if ( IsCellEditControlEnabled() )
f85afd4e 5427 {
2d66e025
MB
5428 if ( !IsVisible( m_currentCellCoords ) )
5429 {
5430 return;
5431 }
5432 else
5433 {
2c9a89e0
RD
5434 wxRect rect = CellToRect( m_currentCellCoords );
5435 int row = m_currentCellCoords.GetRow();
5436 int col = m_currentCellCoords.GetCol();
2d66e025
MB
5437
5438 // convert to scrolled coords
5439 //
99306db2 5440 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
508011ce 5441
99306db2
VZ
5442 // done in PaintBackground()
5443#if 0
5444 // erase the highlight and the cell contents because the editor
5445 // might not cover the entire cell
5446 wxClientDC dc( m_gridWin );
5447 PrepareDC( dc );
5448 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5449 dc.SetPen(*wxTRANSPARENT_PEN);
5450 dc.DrawRectangle(rect);
5451#endif // 0
d2fdd8d2 5452
99306db2
VZ
5453 // cell is shifted by one pixel
5454 rect.x--;
5455 rect.y--;
f0102d2a 5456
508011ce 5457 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 5458 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
5459 if ( !editor->IsCreated() )
5460 {
2c9a89e0
RD
5461 editor->Create(m_gridWin, -1,
5462 new wxGridCellEditorEvtHandler(this, editor));
2d66e025
MB
5463 }
5464
b0e282b3
RR
5465 editor->Show( TRUE, attr );
5466
2c9a89e0 5467 editor->SetSize( rect );
99306db2 5468
3da93aae 5469 editor->BeginEdit(row, col, this);
2c9a89e0
RD
5470 attr->DecRef();
5471 }
f85afd4e
MB
5472 }
5473}
5474
5475
2d66e025 5476void wxGrid::HideCellEditControl()
f85afd4e 5477{
2d66e025 5478 if ( IsCellEditControlEnabled() )
f85afd4e 5479 {
2c9a89e0
RD
5480 int row = m_currentCellCoords.GetRow();
5481 int col = m_currentCellCoords.GetCol();
5482
3da93aae 5483 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 5484 attr->GetEditor(this, row, col)->Show( FALSE );
2c9a89e0
RD
5485 attr->DecRef();
5486 m_gridWin->SetFocus();
f85afd4e 5487 }
2d66e025 5488}
8f177c8e 5489
2d66e025 5490
2d66e025
MB
5491void wxGrid::SaveEditControlValue()
5492{
3da93aae
VZ
5493 if ( IsCellEditControlEnabled() )
5494 {
2c9a89e0
RD
5495 int row = m_currentCellCoords.GetRow();
5496 int col = m_currentCellCoords.GetCol();
8f177c8e 5497
3da93aae 5498 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 5499 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 5500 bool changed = editor->EndEdit(row, col, this);
2d66e025 5501
2c9a89e0 5502 attr->DecRef();
2d66e025 5503
3da93aae
VZ
5504 if (changed)
5505 {
b54ba671 5506 SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025
MB
5507 m_currentCellCoords.GetRow(),
5508 m_currentCellCoords.GetCol() );
5509 }
f85afd4e
MB
5510 }
5511}
5512
2d66e025
MB
5513
5514//
60ff3b99
VZ
5515// ------ Grid location functions
5516// Note that all of these functions work with the logical coordinates of
2d66e025
MB
5517// grid cells and labels so you will need to convert from device
5518// coordinates for mouse events etc.
5519//
5520
5521void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 5522{
58dd5b3b
MB
5523 int row = YToRow(y);
5524 int col = XToCol(x);
5525
5526 if ( row == -1 || col == -1 )
5527 {
5528 coords = wxGridNoCellCoords;
5529 }
5530 else
5531 {
5532 coords.Set( row, col );
5533 }
2d66e025 5534}
f85afd4e 5535
8f177c8e 5536
2d66e025
MB
5537int wxGrid::YToRow( int y )
5538{
5539 int i;
8f177c8e 5540
2d66e025 5541 for ( i = 0; i < m_numRows; i++ )
f85afd4e 5542 {
7c1cb261
VZ
5543 if ( y < GetRowBottom(i) )
5544 return i;
f85afd4e 5545 }
2d66e025 5546
4ee5fc9c 5547 return -1;
f85afd4e
MB
5548}
5549
2d66e025
MB
5550
5551int wxGrid::XToCol( int x )
f85afd4e 5552{
2d66e025 5553 int i;
8f177c8e 5554
2d66e025 5555 for ( i = 0; i < m_numCols; i++ )
f85afd4e 5556 {
7c1cb261
VZ
5557 if ( x < GetColRight(i) )
5558 return i;
f85afd4e
MB
5559 }
5560
4ee5fc9c 5561 return -1;
2d66e025 5562}
8f177c8e 5563
2d66e025
MB
5564
5565// return the row number that that the y coord is near the edge of, or
5566// -1 if not near an edge
5567//
5568int wxGrid::YToEdgeOfRow( int y )
5569{
5570 int i, d;
5571
5572 for ( i = 0; i < m_numRows; i++ )
5573 {
7c1cb261 5574 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 5575 {
7c1cb261
VZ
5576 d = abs( y - GetRowBottom(i) );
5577 if ( d < WXGRID_LABEL_EDGE_ZONE )
5578 return i;
f85afd4e 5579 }
f85afd4e 5580 }
2d66e025
MB
5581
5582 return -1;
5583}
5584
5585
5586// return the col number that that the x coord is near the edge of, or
5587// -1 if not near an edge
5588//
5589int wxGrid::XToEdgeOfCol( int x )
5590{
5591 int i, d;
5592
5593 for ( i = 0; i < m_numCols; i++ )
f85afd4e 5594 {
7c1cb261 5595 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 5596 {
7c1cb261
VZ
5597 d = abs( x - GetColRight(i) );
5598 if ( d < WXGRID_LABEL_EDGE_ZONE )
5599 return i;
2d66e025 5600 }
f85afd4e 5601 }
2d66e025
MB
5602
5603 return -1;
f85afd4e
MB
5604}
5605
2d66e025
MB
5606
5607wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 5608{
2d66e025 5609 wxRect rect( -1, -1, -1, -1 );
f85afd4e 5610
2d66e025
MB
5611 if ( row >= 0 && row < m_numRows &&
5612 col >= 0 && col < m_numCols )
f85afd4e 5613 {
7c1cb261
VZ
5614 rect.x = GetColLeft(col);
5615 rect.y = GetRowTop(row);
5616 rect.width = GetColWidth(col);
5617 rect.height = GetRowHeight(row);
f85afd4e
MB
5618 }
5619
2d66e025
MB
5620 return rect;
5621}
5622
5623
5624bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
5625{
5626 // get the cell rectangle in logical coords
5627 //
5628 wxRect r( CellToRect( row, col ) );
60ff3b99 5629
2d66e025
MB
5630 // convert to device coords
5631 //
5632 int left, top, right, bottom;
5633 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5634 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 5635
2d66e025
MB
5636 // check against the client area of the grid window
5637 //
5638 int cw, ch;
5639 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 5640
2d66e025 5641 if ( wholeCellVisible )
f85afd4e 5642 {
2d66e025 5643 // is the cell wholly visible ?
8f177c8e 5644 //
2d66e025
MB
5645 return ( left >= 0 && right <= cw &&
5646 top >= 0 && bottom <= ch );
5647 }
5648 else
5649 {
5650 // is the cell partly visible ?
5651 //
5652 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
5653 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
5654 }
5655}
5656
5657
5658// make the specified cell location visible by doing a minimal amount
5659// of scrolling
5660//
5661void wxGrid::MakeCellVisible( int row, int col )
5662{
5663 int i;
60ff3b99 5664 int xpos = -1, ypos = -1;
2d66e025
MB
5665
5666 if ( row >= 0 && row < m_numRows &&
5667 col >= 0 && col < m_numCols )
5668 {
5669 // get the cell rectangle in logical coords
5670 //
5671 wxRect r( CellToRect( row, col ) );
60ff3b99 5672
2d66e025
MB
5673 // convert to device coords
5674 //
5675 int left, top, right, bottom;
5676 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5677 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 5678
2d66e025
MB
5679 int cw, ch;
5680 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 5681
2d66e025 5682 if ( top < 0 )
3f296516 5683 {
2d66e025 5684 ypos = r.GetTop();
3f296516 5685 }
2d66e025
MB
5686 else if ( bottom > ch )
5687 {
5688 int h = r.GetHeight();
5689 ypos = r.GetTop();
5690 for ( i = row-1; i >= 0; i-- )
5691 {
7c1cb261
VZ
5692 int rowHeight = GetRowHeight(i);
5693 if ( h + rowHeight > ch )
5694 break;
2d66e025 5695
7c1cb261
VZ
5696 h += rowHeight;
5697 ypos -= rowHeight;
2d66e025 5698 }
f0102d2a
VZ
5699
5700 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5701 // have rounding errors (this is important, because if we do, we
5702 // might not scroll at all and some cells won't be redrawn)
5703 ypos += GRID_SCROLL_LINE / 2;
2d66e025
MB
5704 }
5705
5706 if ( left < 0 )
5707 {
5708 xpos = r.GetLeft();
5709 }
5710 else if ( right > cw )
5711 {
5712 int w = r.GetWidth();
5713 xpos = r.GetLeft();
5714 for ( i = col-1; i >= 0; i-- )
5715 {
7c1cb261
VZ
5716 int colWidth = GetColWidth(i);
5717 if ( w + colWidth > cw )
5718 break;
2d66e025 5719
7c1cb261
VZ
5720 w += colWidth;
5721 xpos -= colWidth;
2d66e025 5722 }
f0102d2a
VZ
5723
5724 // see comment for ypos above
5725 xpos += GRID_SCROLL_LINE / 2;
2d66e025
MB
5726 }
5727
5728 if ( xpos != -1 || ypos != -1 )
5729 {
f0102d2a
VZ
5730 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
5731 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
2d66e025
MB
5732 Scroll( xpos, ypos );
5733 AdjustScrollbars();
5734 }
5735 }
5736}
5737
5738
5739//
5740// ------ Grid cursor movement functions
5741//
5742
5743bool wxGrid::MoveCursorUp()
5744{
5745 if ( m_currentCellCoords != wxGridNoCellCoords &&
5746 m_currentCellCoords.GetRow() > 0 )
5747 {
5748 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
5749 m_currentCellCoords.GetCol() );
60ff3b99 5750
2d66e025
MB
5751 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
5752 m_currentCellCoords.GetCol() );
5753
8f177c8e 5754 return TRUE;
f85afd4e 5755 }
2d66e025
MB
5756
5757 return FALSE;
5758}
5759
5760
5761bool wxGrid::MoveCursorDown()
5762{
5763 // TODO: allow for scrolling
5764 //
5765 if ( m_currentCellCoords != wxGridNoCellCoords &&
5766 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 5767 {
2d66e025
MB
5768 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
5769 m_currentCellCoords.GetCol() );
60ff3b99 5770
2d66e025
MB
5771 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
5772 m_currentCellCoords.GetCol() );
5773
5774 return TRUE;
f85afd4e 5775 }
2d66e025
MB
5776
5777 return FALSE;
f85afd4e
MB
5778}
5779
2d66e025
MB
5780
5781bool wxGrid::MoveCursorLeft()
f85afd4e 5782{
2d66e025
MB
5783 if ( m_currentCellCoords != wxGridNoCellCoords &&
5784 m_currentCellCoords.GetCol() > 0 )
5785 {
5786 MakeCellVisible( m_currentCellCoords.GetRow(),
5787 m_currentCellCoords.GetCol() - 1 );
60ff3b99 5788
2d66e025
MB
5789 SetCurrentCell( m_currentCellCoords.GetRow(),
5790 m_currentCellCoords.GetCol() - 1 );
8f177c8e 5791
2d66e025
MB
5792 return TRUE;
5793 }
5794
5795 return FALSE;
5796}
5797
5798
5799bool wxGrid::MoveCursorRight()
5800{
5801 if ( m_currentCellCoords != wxGridNoCellCoords &&
5802 m_currentCellCoords.GetCol() < m_numCols - 1 )
f85afd4e 5803 {
2d66e025
MB
5804 MakeCellVisible( m_currentCellCoords.GetRow(),
5805 m_currentCellCoords.GetCol() + 1 );
60ff3b99 5806
2d66e025
MB
5807 SetCurrentCell( m_currentCellCoords.GetRow(),
5808 m_currentCellCoords.GetCol() + 1 );
5809
5810 return TRUE;
f85afd4e 5811 }
8f177c8e 5812
2d66e025
MB
5813 return FALSE;
5814}
5815
5816
5817bool wxGrid::MovePageUp()
5818{
5819 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 5820
2d66e025
MB
5821 int row = m_currentCellCoords.GetRow();
5822 if ( row > 0 )
f85afd4e 5823 {
2d66e025
MB
5824 int cw, ch;
5825 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 5826
7c1cb261 5827 int y = GetRowTop(row);
2d66e025
MB
5828 int newRow = YToRow( y - ch + 1 );
5829 if ( newRow == -1 )
5830 {
5831 newRow = 0;
5832 }
60ff3b99 5833 else if ( newRow == row )
2d66e025
MB
5834 {
5835 newRow = row - 1;
5836 }
8f177c8e 5837
2d66e025
MB
5838 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5839 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 5840
f85afd4e
MB
5841 return TRUE;
5842 }
2d66e025
MB
5843
5844 return FALSE;
5845}
5846
5847bool wxGrid::MovePageDown()
5848{
5849 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 5850
2d66e025
MB
5851 int row = m_currentCellCoords.GetRow();
5852 if ( row < m_numRows )
f85afd4e 5853 {
2d66e025
MB
5854 int cw, ch;
5855 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 5856
7c1cb261 5857 int y = GetRowTop(row);
2d66e025
MB
5858 int newRow = YToRow( y + ch );
5859 if ( newRow == -1 )
5860 {
5861 newRow = m_numRows - 1;
5862 }
60ff3b99 5863 else if ( newRow == row )
2d66e025
MB
5864 {
5865 newRow = row + 1;
5866 }
5867
5868 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5869 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 5870
2d66e025 5871 return TRUE;
f85afd4e 5872 }
2d66e025
MB
5873
5874 return FALSE;
f85afd4e 5875}
8f177c8e 5876
2d66e025
MB
5877bool wxGrid::MoveCursorUpBlock()
5878{
5879 if ( m_table &&
5880 m_currentCellCoords != wxGridNoCellCoords &&
5881 m_currentCellCoords.GetRow() > 0 )
5882 {
5883 int row = m_currentCellCoords.GetRow();
5884 int col = m_currentCellCoords.GetCol();
5885
5886 if ( m_table->IsEmptyCell(row, col) )
5887 {
5888 // starting in an empty cell: find the next block of
5889 // non-empty cells
5890 //
5891 while ( row > 0 )
5892 {
5893 row-- ;
5894 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5895 }
5896 }
5897 else if ( m_table->IsEmptyCell(row-1, col) )
5898 {
5899 // starting at the top of a block: find the next block
5900 //
5901 row--;
5902 while ( row > 0 )
5903 {
5904 row-- ;
5905 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5906 }
5907 }
5908 else
5909 {
5910 // starting within a block: find the top of the block
5911 //
5912 while ( row > 0 )
5913 {
5914 row-- ;
5915 if ( m_table->IsEmptyCell(row, col) )
5916 {
5917 row++ ;
5918 break;
5919 }
5920 }
5921 }
f85afd4e 5922
2d66e025
MB
5923 MakeCellVisible( row, col );
5924 SetCurrentCell( row, col );
f85afd4e 5925
2d66e025
MB
5926 return TRUE;
5927 }
f85afd4e 5928
2d66e025
MB
5929 return FALSE;
5930}
f85afd4e 5931
2d66e025 5932bool wxGrid::MoveCursorDownBlock()
f85afd4e 5933{
2d66e025
MB
5934 if ( m_table &&
5935 m_currentCellCoords != wxGridNoCellCoords &&
5936 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 5937 {
2d66e025
MB
5938 int row = m_currentCellCoords.GetRow();
5939 int col = m_currentCellCoords.GetCol();
5940
5941 if ( m_table->IsEmptyCell(row, col) )
5942 {
5943 // starting in an empty cell: find the next block of
5944 // non-empty cells
5945 //
5946 while ( row < m_numRows-1 )
5947 {
5948 row++ ;
5949 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5950 }
5951 }
5952 else if ( m_table->IsEmptyCell(row+1, col) )
5953 {
5954 // starting at the bottom of a block: find the next block
5955 //
5956 row++;
5957 while ( row < m_numRows-1 )
5958 {
5959 row++ ;
5960 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5961 }
5962 }
5963 else
5964 {
5965 // starting within a block: find the bottom of the block
5966 //
5967 while ( row < m_numRows-1 )
5968 {
5969 row++ ;
5970 if ( m_table->IsEmptyCell(row, col) )
5971 {
5972 row-- ;
5973 break;
5974 }
5975 }
5976 }
5977
5978 MakeCellVisible( row, col );
5979 SetCurrentCell( row, col );
5980
5981 return TRUE;
f85afd4e 5982 }
f85afd4e 5983
2d66e025
MB
5984 return FALSE;
5985}
f85afd4e 5986
2d66e025 5987bool wxGrid::MoveCursorLeftBlock()
f85afd4e 5988{
2d66e025
MB
5989 if ( m_table &&
5990 m_currentCellCoords != wxGridNoCellCoords &&
5991 m_currentCellCoords.GetCol() > 0 )
f85afd4e 5992 {
2d66e025
MB
5993 int row = m_currentCellCoords.GetRow();
5994 int col = m_currentCellCoords.GetCol();
5995
5996 if ( m_table->IsEmptyCell(row, col) )
5997 {
5998 // starting in an empty cell: find the next block of
5999 // non-empty cells
6000 //
6001 while ( col > 0 )
6002 {
6003 col-- ;
6004 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6005 }
6006 }
6007 else if ( m_table->IsEmptyCell(row, col-1) )
6008 {
6009 // starting at the left of a block: find the next block
6010 //
6011 col--;
6012 while ( col > 0 )
6013 {
6014 col-- ;
6015 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6016 }
6017 }
6018 else
6019 {
6020 // starting within a block: find the left of the block
6021 //
6022 while ( col > 0 )
6023 {
6024 col-- ;
6025 if ( m_table->IsEmptyCell(row, col) )
6026 {
6027 col++ ;
6028 break;
6029 }
6030 }
6031 }
f85afd4e 6032
2d66e025
MB
6033 MakeCellVisible( row, col );
6034 SetCurrentCell( row, col );
8f177c8e 6035
2d66e025 6036 return TRUE;
f85afd4e 6037 }
2d66e025
MB
6038
6039 return FALSE;
f85afd4e
MB
6040}
6041
2d66e025 6042bool wxGrid::MoveCursorRightBlock()
f85afd4e 6043{
2d66e025
MB
6044 if ( m_table &&
6045 m_currentCellCoords != wxGridNoCellCoords &&
6046 m_currentCellCoords.GetCol() < m_numCols-1 )
f85afd4e 6047 {
2d66e025
MB
6048 int row = m_currentCellCoords.GetRow();
6049 int col = m_currentCellCoords.GetCol();
8f177c8e 6050
2d66e025
MB
6051 if ( m_table->IsEmptyCell(row, col) )
6052 {
6053 // starting in an empty cell: find the next block of
6054 // non-empty cells
6055 //
6056 while ( col < m_numCols-1 )
6057 {
6058 col++ ;
6059 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6060 }
6061 }
6062 else if ( m_table->IsEmptyCell(row, col+1) )
6063 {
6064 // starting at the right of a block: find the next block
6065 //
6066 col++;
6067 while ( col < m_numCols-1 )
6068 {
6069 col++ ;
6070 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6071 }
6072 }
6073 else
6074 {
6075 // starting within a block: find the right of the block
6076 //
6077 while ( col < m_numCols-1 )
6078 {
6079 col++ ;
6080 if ( m_table->IsEmptyCell(row, col) )
6081 {
6082 col-- ;
6083 break;
6084 }
6085 }
6086 }
8f177c8e 6087
2d66e025
MB
6088 MakeCellVisible( row, col );
6089 SetCurrentCell( row, col );
f85afd4e 6090
2d66e025 6091 return TRUE;
f85afd4e 6092 }
2d66e025
MB
6093
6094 return FALSE;
f85afd4e
MB
6095}
6096
6097
2d66e025 6098
f85afd4e 6099//
2d66e025 6100// ------ Label values and formatting
f85afd4e
MB
6101//
6102
6103void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
6104{
6105 *horiz = m_rowLabelHorizAlign;
6106 *vert = m_rowLabelVertAlign;
6107}
6108
6109void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
6110{
6111 *horiz = m_colLabelHorizAlign;
6112 *vert = m_colLabelVertAlign;
6113}
6114
6115wxString wxGrid::GetRowLabelValue( int row )
6116{
6117 if ( m_table )
6118 {
6119 return m_table->GetRowLabelValue( row );
6120 }
6121 else
6122 {
6123 wxString s;
6124 s << row;
6125 return s;
6126 }
6127}
6128
6129wxString wxGrid::GetColLabelValue( int col )
6130{
6131 if ( m_table )
6132 {
6133 return m_table->GetColLabelValue( col );
6134 }
6135 else
6136 {
6137 wxString s;
6138 s << col;
6139 return s;
6140 }
6141}
6142
2e8cd977 6143
f85afd4e
MB
6144void wxGrid::SetRowLabelSize( int width )
6145{
2e8cd977
MB
6146 width = wxMax( width, 0 );
6147 if ( width != m_rowLabelWidth )
6148 {
2e8cd977
MB
6149 if ( width == 0 )
6150 {
6151 m_rowLabelWin->Show( FALSE );
7807d81c 6152 m_cornerLabelWin->Show( FALSE );
2e8cd977 6153 }
7807d81c 6154 else if ( m_rowLabelWidth == 0 )
2e8cd977 6155 {
7807d81c
MB
6156 m_rowLabelWin->Show( TRUE );
6157 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 6158 }
b99be8fb 6159
2e8cd977 6160 m_rowLabelWidth = width;
7807d81c
MB
6161 CalcWindowSizes();
6162 Refresh( TRUE );
2e8cd977 6163 }
f85afd4e
MB
6164}
6165
2e8cd977 6166
f85afd4e
MB
6167void wxGrid::SetColLabelSize( int height )
6168{
7807d81c 6169 height = wxMax( height, 0 );
2e8cd977
MB
6170 if ( height != m_colLabelHeight )
6171 {
2e8cd977
MB
6172 if ( height == 0 )
6173 {
2e8cd977 6174 m_colLabelWin->Show( FALSE );
7807d81c 6175 m_cornerLabelWin->Show( FALSE );
2e8cd977 6176 }
7807d81c 6177 else if ( m_colLabelHeight == 0 )
2e8cd977 6178 {
7807d81c
MB
6179 m_colLabelWin->Show( TRUE );
6180 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 6181 }
7807d81c 6182
2e8cd977 6183 m_colLabelHeight = height;
7807d81c
MB
6184 CalcWindowSizes();
6185 Refresh( TRUE );
2e8cd977 6186 }
f85afd4e
MB
6187}
6188
2e8cd977 6189
f85afd4e
MB
6190void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6191{
2d66e025
MB
6192 if ( m_labelBackgroundColour != colour )
6193 {
6194 m_labelBackgroundColour = colour;
6195 m_rowLabelWin->SetBackgroundColour( colour );
6196 m_colLabelWin->SetBackgroundColour( colour );
6197 m_cornerLabelWin->SetBackgroundColour( colour );
6198
6199 if ( !GetBatchCount() )
6200 {
6201 m_rowLabelWin->Refresh();
6202 m_colLabelWin->Refresh();
6203 m_cornerLabelWin->Refresh();
6204 }
6205 }
f85afd4e
MB
6206}
6207
6208void wxGrid::SetLabelTextColour( const wxColour& colour )
6209{
2d66e025
MB
6210 if ( m_labelTextColour != colour )
6211 {
6212 m_labelTextColour = colour;
6213 if ( !GetBatchCount() )
6214 {
6215 m_rowLabelWin->Refresh();
6216 m_colLabelWin->Refresh();
6217 }
6218 }
f85afd4e
MB
6219}
6220
6221void wxGrid::SetLabelFont( const wxFont& font )
6222{
6223 m_labelFont = font;
2d66e025
MB
6224 if ( !GetBatchCount() )
6225 {
6226 m_rowLabelWin->Refresh();
6227 m_colLabelWin->Refresh();
6228 }
f85afd4e
MB
6229}
6230
6231void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6232{
6233 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6234 {
6235 m_rowLabelHorizAlign = horiz;
6236 }
8f177c8e 6237
f85afd4e
MB
6238 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6239 {
6240 m_rowLabelVertAlign = vert;
6241 }
6242
2d66e025
MB
6243 if ( !GetBatchCount() )
6244 {
6245 m_rowLabelWin->Refresh();
60ff3b99 6246 }
f85afd4e
MB
6247}
6248
6249void wxGrid::SetColLabelAlignment( int horiz, int vert )
6250{
6251 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6252 {
6253 m_colLabelHorizAlign = horiz;
6254 }
8f177c8e 6255
f85afd4e
MB
6256 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6257 {
6258 m_colLabelVertAlign = vert;
6259 }
6260
2d66e025
MB
6261 if ( !GetBatchCount() )
6262 {
2d66e025 6263 m_colLabelWin->Refresh();
60ff3b99 6264 }
f85afd4e
MB
6265}
6266
6267void wxGrid::SetRowLabelValue( int row, const wxString& s )
6268{
6269 if ( m_table )
6270 {
6271 m_table->SetRowLabelValue( row, s );
2d66e025
MB
6272 if ( !GetBatchCount() )
6273 {
70c7a608
SN
6274 wxRect rect = CellToRect( row, 0);
6275 if ( rect.height > 0 )
6276 {
cb309039 6277 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
70c7a608
SN
6278 rect.x = m_left;
6279 rect.width = m_rowLabelWidth;
6280 m_rowLabelWin->Refresh( TRUE, &rect );
6281 }
2d66e025 6282 }
f85afd4e
MB
6283 }
6284}
6285
6286void wxGrid::SetColLabelValue( int col, const wxString& s )
6287{
6288 if ( m_table )
6289 {
6290 m_table->SetColLabelValue( col, s );
2d66e025
MB
6291 if ( !GetBatchCount() )
6292 {
70c7a608
SN
6293 wxRect rect = CellToRect( 0, col );
6294 if ( rect.width > 0 )
6295 {
cb309039 6296 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
70c7a608
SN
6297 rect.y = m_top;
6298 rect.height = m_colLabelHeight;
6299 m_colLabelWin->Refresh( TRUE, &rect );
6300 }
2d66e025 6301 }
f85afd4e
MB
6302 }
6303}
6304
6305void wxGrid::SetGridLineColour( const wxColour& colour )
6306{
2d66e025
MB
6307 if ( m_gridLineColour != colour )
6308 {
6309 m_gridLineColour = colour;
60ff3b99 6310
2d66e025
MB
6311 wxClientDC dc( m_gridWin );
6312 PrepareDC( dc );
c6a51dcd 6313 DrawAllGridLines( dc, wxRegion() );
2d66e025 6314 }
f85afd4e
MB
6315}
6316
6317void wxGrid::EnableGridLines( bool enable )
6318{
6319 if ( enable != m_gridLinesEnabled )
6320 {
6321 m_gridLinesEnabled = enable;
2d66e025
MB
6322
6323 if ( !GetBatchCount() )
6324 {
6325 if ( enable )
6326 {
6327 wxClientDC dc( m_gridWin );
6328 PrepareDC( dc );
c6a51dcd 6329 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
6330 }
6331 else
6332 {
6333 m_gridWin->Refresh();
6334 }
6335 }
f85afd4e
MB
6336 }
6337}
6338
6339
6340int wxGrid::GetDefaultRowSize()
6341{
6342 return m_defaultRowHeight;
6343}
6344
6345int wxGrid::GetRowSize( int row )
6346{
b99be8fb
VZ
6347 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
6348
7c1cb261 6349 return GetRowHeight(row);
f85afd4e
MB
6350}
6351
6352int wxGrid::GetDefaultColSize()
6353{
6354 return m_defaultColWidth;
6355}
6356
6357int wxGrid::GetColSize( int col )
6358{
b99be8fb
VZ
6359 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
6360
7c1cb261 6361 return GetColWidth(col);
f85afd4e
MB
6362}
6363
2e9a6788
VZ
6364// ============================================================================
6365// access to the grid attributes: each of them has a default value in the grid
6366// itself and may be overidden on a per-cell basis
6367// ============================================================================
6368
6369// ----------------------------------------------------------------------------
6370// setting default attributes
6371// ----------------------------------------------------------------------------
6372
6373void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6374{
2796cce3 6375 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
6376#ifdef __WXGTK__
6377 m_gridWin->SetBackgroundColour(col);
6378#endif
2e9a6788
VZ
6379}
6380
6381void wxGrid::SetDefaultCellTextColour( const wxColour& col )
6382{
2796cce3 6383 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
6384}
6385
6386void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
6387{
2796cce3 6388 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
6389}
6390
6391void wxGrid::SetDefaultCellFont( const wxFont& font )
6392{
2796cce3
RD
6393 m_defaultCellAttr->SetFont(font);
6394}
6395
0ba143c9
RD
6396void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
6397{
6398 m_defaultCellAttr->SetRenderer(renderer);
6399}
2e9a6788 6400
0ba143c9
RD
6401void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
6402{
6403 m_defaultCellAttr->SetEditor(editor);
6404}
9b4aede2 6405
2e9a6788
VZ
6406// ----------------------------------------------------------------------------
6407// access to the default attrbiutes
6408// ----------------------------------------------------------------------------
6409
f85afd4e
MB
6410wxColour wxGrid::GetDefaultCellBackgroundColour()
6411{
2796cce3 6412 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
6413}
6414
2e9a6788
VZ
6415wxColour wxGrid::GetDefaultCellTextColour()
6416{
2796cce3 6417 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
6418}
6419
6420wxFont wxGrid::GetDefaultCellFont()
6421{
2796cce3 6422 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
6423}
6424
6425void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
6426{
2796cce3 6427 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
6428}
6429
0ba143c9
RD
6430wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
6431{
28a77bc4 6432 return m_defaultCellAttr->GetRenderer(NULL,0,0);
0ba143c9 6433}
ab79958a 6434
0ba143c9
RD
6435wxGridCellEditor *wxGrid::GetDefaultEditor() const
6436{
28a77bc4 6437 return m_defaultCellAttr->GetEditor(NULL,0,0);
0ba143c9 6438}
9b4aede2 6439
2e9a6788
VZ
6440// ----------------------------------------------------------------------------
6441// access to cell attributes
6442// ----------------------------------------------------------------------------
6443
b99be8fb 6444wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 6445{
0a976765 6446 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6447 wxColour colour = attr->GetBackgroundColour();
2e9a6788 6448 attr->SafeDecRef();
b99be8fb 6449 return colour;
f85afd4e
MB
6450}
6451
b99be8fb 6452wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 6453{
0a976765 6454 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6455 wxColour colour = attr->GetTextColour();
2e9a6788 6456 attr->SafeDecRef();
b99be8fb 6457 return colour;
f85afd4e
MB
6458}
6459
b99be8fb 6460wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 6461{
0a976765 6462 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6463 wxFont font = attr->GetFont();
2e9a6788 6464 attr->SafeDecRef();
b99be8fb 6465 return font;
f85afd4e
MB
6466}
6467
b99be8fb 6468void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 6469{
0a976765 6470 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6471 attr->GetAlignment(horiz, vert);
2e9a6788
VZ
6472 attr->SafeDecRef();
6473}
6474
2796cce3
RD
6475wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
6476{
6477 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6478 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3
RD
6479 attr->DecRef();
6480 return renderer;
6481}
6482
9b4aede2
RD
6483wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
6484{
6485 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6486 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2
RD
6487 attr->DecRef();
6488 return editor;
6489}
6490
283b7808
VZ
6491bool wxGrid::IsReadOnly(int row, int col) const
6492{
6493 wxGridCellAttr* attr = GetCellAttr(row, col);
6494 bool isReadOnly = attr->IsReadOnly();
6495 attr->DecRef();
6496 return isReadOnly;
6497}
6498
2e9a6788 6499// ----------------------------------------------------------------------------
758cbedf 6500// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
6501// ----------------------------------------------------------------------------
6502
6503bool wxGrid::CanHaveAttributes()
6504{
6505 if ( !m_table )
6506 {
6507 return FALSE;
6508 }
6509
f2d76237 6510 return m_table->CanHaveAttributes();
2e9a6788
VZ
6511}
6512
0a976765
VZ
6513void wxGrid::ClearAttrCache()
6514{
6515 if ( m_attrCache.row != -1 )
6516 {
6517 m_attrCache.attr->SafeDecRef();
6518 m_attrCache.row = -1;
6519 }
6520}
6521
6522void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
6523{
6524 wxGrid *self = (wxGrid *)this; // const_cast
6525
6526 self->ClearAttrCache();
6527 self->m_attrCache.row = row;
6528 self->m_attrCache.col = col;
6529 self->m_attrCache.attr = attr;
6530 attr->SafeIncRef();
6531}
6532
6533bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
6534{
6535 if ( row == m_attrCache.row && col == m_attrCache.col )
6536 {
6537 *attr = m_attrCache.attr;
6538 (*attr)->SafeIncRef();
6539
6540#ifdef DEBUG_ATTR_CACHE
6541 gs_nAttrCacheHits++;
6542#endif
6543
6544 return TRUE;
6545 }
6546 else
6547 {
6548#ifdef DEBUG_ATTR_CACHE
6549 gs_nAttrCacheMisses++;
6550#endif
6551 return FALSE;
6552 }
6553}
6554
2e9a6788
VZ
6555wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
6556{
0a976765
VZ
6557 wxGridCellAttr *attr;
6558 if ( !LookupAttr(row, col, &attr) )
2e9a6788 6559 {
0a976765
VZ
6560 attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
6561 CacheAttr(row, col, attr);
6562 }
508011ce
VZ
6563 if (attr)
6564 {
2796cce3 6565 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
6566 }
6567 else
6568 {
2796cce3
RD
6569 attr = m_defaultCellAttr;
6570 attr->IncRef();
6571 }
2e9a6788 6572
0a976765
VZ
6573 return attr;
6574}
6575
6576wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
6577{
6578 wxGridCellAttr *attr;
6579 if ( !LookupAttr(row, col, &attr) || !attr )
6580 {
6581 wxASSERT_MSG( m_table,
6582 _T("we may only be called if CanHaveAttributes() "
6583 "returned TRUE and then m_table should be !NULL") );
6584
6585 attr = m_table->GetAttr(row, col);
6586 if ( !attr )
6587 {
6588 attr = new wxGridCellAttr;
6589
6590 // artificially inc the ref count to match DecRef() in caller
6591 attr->IncRef();
6592
6593 m_table->SetAttr(attr, row, col);
6594 }
2e9a6788 6595
0a976765 6596 CacheAttr(row, col, attr);
2e9a6788 6597 }
2796cce3 6598 attr->SetDefAttr(m_defaultCellAttr);
2e9a6788
VZ
6599 return attr;
6600}
6601
758cbedf
VZ
6602// ----------------------------------------------------------------------------
6603// setting cell attributes: this is forwarded to the table
6604// ----------------------------------------------------------------------------
6605
6606void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
6607{
6608 if ( CanHaveAttributes() )
6609 {
6610 m_table->SetRowAttr(attr, row);
6611 }
6612 else
6613 {
6614 attr->SafeDecRef();
6615 }
6616}
6617
6618void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
6619{
6620 if ( CanHaveAttributes() )
6621 {
6622 m_table->SetColAttr(attr, col);
6623 }
6624 else
6625 {
6626 attr->SafeDecRef();
6627 }
6628}
6629
2e9a6788
VZ
6630void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
6631{
6632 if ( CanHaveAttributes() )
6633 {
0a976765 6634 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
6635 attr->SetBackgroundColour(colour);
6636 attr->DecRef();
6637 }
6638}
6639
6640void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
6641{
6642 if ( CanHaveAttributes() )
6643 {
0a976765 6644 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
6645 attr->SetTextColour(colour);
6646 attr->DecRef();
6647 }
6648}
6649
6650void wxGrid::SetCellFont( int row, int col, const wxFont& font )
6651{
6652 if ( CanHaveAttributes() )
6653 {
0a976765 6654 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
6655 attr->SetFont(font);
6656 attr->DecRef();
6657 }
6658}
6659
6660void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
6661{
6662 if ( CanHaveAttributes() )
6663 {
0a976765 6664 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
6665 attr->SetAlignment(horiz, vert);
6666 attr->DecRef();
ab79958a
VZ
6667 }
6668}
6669
6670void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
6671{
6672 if ( CanHaveAttributes() )
6673 {
0a976765 6674 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
6675 attr->SetRenderer(renderer);
6676 attr->DecRef();
9b4aede2
RD
6677 }
6678}
6679
6680void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
6681{
6682 if ( CanHaveAttributes() )
6683 {
6684 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6685 attr->SetEditor(editor);
6686 attr->DecRef();
283b7808
VZ
6687 }
6688}
6689
6690void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
6691{
6692 if ( CanHaveAttributes() )
6693 {
6694 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6695 attr->SetReadOnly(isReadOnly);
6696 attr->DecRef();
2e9a6788 6697 }
f85afd4e
MB
6698}
6699
f2d76237
RD
6700// ----------------------------------------------------------------------------
6701// Data type registration
6702// ----------------------------------------------------------------------------
6703
6704void wxGrid::RegisterDataType(const wxString& typeName,
6705 wxGridCellRenderer* renderer,
6706 wxGridCellEditor* editor)
6707{
6708 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
6709}
6710
6711
99306db2 6712wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
6713{
6714 wxString typeName = m_table->GetTypeName(row, col);
6715 return GetDefaultEditorForType(typeName);
6716}
6717
99306db2 6718wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
6719{
6720 wxString typeName = m_table->GetTypeName(row, col);
6721 return GetDefaultRendererForType(typeName);
6722}
6723
99306db2
VZ
6724wxGridCellEditor*
6725wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237
RD
6726{
6727 int index = m_typeRegistry->FindDataType(typeName);
6728 if (index == -1) {
6729 // Should we force the failure here or let it fallback to string handling???
6730 // wxFAIL_MSG(wxT("Unknown data type name"));
6731 return NULL;
6732 }
6733 return m_typeRegistry->GetEditor(index);
6734}
6735
99306db2
VZ
6736wxGridCellRenderer*
6737wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237
RD
6738{
6739 int index = m_typeRegistry->FindDataType(typeName);
6740 if (index == -1) {
6741 // Should we force the failure here or let it fallback to string handling???
6742 // wxFAIL_MSG(wxT("Unknown data type name"));
6743 return NULL;
6744 }
6745 return m_typeRegistry->GetRenderer(index);
6746}
6747
6748
2e9a6788
VZ
6749// ----------------------------------------------------------------------------
6750// row/col size
6751// ----------------------------------------------------------------------------
6752
6e8524b1
MB
6753void wxGrid::EnableDragRowSize( bool enable )
6754{
6755 m_canDragRowSize = enable;
6756}
6757
6758
6759void wxGrid::EnableDragColSize( bool enable )
6760{
6761 m_canDragColSize = enable;
6762}
6763
4cfa5de6
RD
6764void wxGrid::EnableDragGridSize( bool enable )
6765{
6766 m_canDragGridSize = enable;
6767}
6768
6e8524b1 6769
f85afd4e
MB
6770void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
6771{
6772 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
6773
6774 if ( resizeExistingRows )
6775 {
7c1cb261
VZ
6776 InitRowHeights();
6777
f85afd4e 6778 CalcDimensions();
f85afd4e
MB
6779 }
6780}
6781
6782void wxGrid::SetRowSize( int row, int height )
6783{
b99be8fb 6784 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 6785
7c1cb261
VZ
6786 if ( m_rowHeights.IsEmpty() )
6787 {
6788 // need to really create the array
6789 InitRowHeights();
6790 }
60ff3b99 6791
b99be8fb
VZ
6792 int h = wxMax( 0, height );
6793 int diff = h - m_rowHeights[row];
60ff3b99 6794
b99be8fb 6795 m_rowHeights[row] = h;
7c1cb261 6796 int i;
b99be8fb 6797 for ( i = row; i < m_numRows; i++ )
f85afd4e 6798 {
b99be8fb 6799 m_rowBottoms[i] += diff;
f85afd4e 6800 }
b99be8fb 6801 CalcDimensions();
f85afd4e
MB
6802}
6803
6804void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
6805{
6806 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
6807
6808 if ( resizeExistingCols )
6809 {
7c1cb261
VZ
6810 InitColWidths();
6811
f85afd4e 6812 CalcDimensions();
f85afd4e
MB
6813 }
6814}
6815
6816void wxGrid::SetColSize( int col, int width )
6817{
b99be8fb 6818 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 6819
43947979
VZ
6820 // should we check that it's bigger than GetColMinimalWidth(col) here?
6821
7c1cb261
VZ
6822 if ( m_colWidths.IsEmpty() )
6823 {
6824 // need to really create the array
6825 InitColWidths();
6826 }
f85afd4e 6827
b99be8fb
VZ
6828 int w = wxMax( 0, width );
6829 int diff = w - m_colWidths[col];
6830 m_colWidths[col] = w;
60ff3b99 6831
7c1cb261 6832 int i;
b99be8fb 6833 for ( i = col; i < m_numCols; i++ )
f85afd4e 6834 {
b99be8fb 6835 m_colRights[i] += diff;
f85afd4e 6836 }
b99be8fb 6837 CalcDimensions();
f85afd4e
MB
6838}
6839
2d66e025 6840
43947979
VZ
6841void wxGrid::SetColMinimalWidth( int col, int width )
6842{
6843 m_colMinWidths.Put(col, (wxObject *)width);
6844}
6845
6846int wxGrid::GetColMinimalWidth(int col) const
6847{
6848 wxObject *obj = m_colMinWidths.Get(m_dragRowOrCol);
6849 return obj ? (int)obj : WXGRID_MIN_COL_WIDTH;
6850}
6851
65e4e78e
VZ
6852void wxGrid::AutoSizeColumn( int col, bool setAsMin )
6853{
6854 wxClientDC dc(m_gridWin);
6855
6856 wxCoord width, widthMax = 0;
6857 for ( int row = 0; row < m_numRows; row++ )
6858 {
6859 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6860 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
6861 if ( renderer )
6862 {
6863 width = renderer->GetBestSize(*this, *attr, dc, row, col).x;
6864 if ( width > widthMax )
6865 {
6866 widthMax = width;
6867 }
6868 }
6869
6870 attr->DecRef();
6871 }
6872
6873 // now also compare with the column label width
6874 dc.SetFont( GetLabelFont() );
6875 dc.GetTextExtent( GetColLabelValue(col), &width, NULL );
6876 if ( width > widthMax )
6877 {
6878 widthMax = width;
6879 }
6880
6881 if ( !widthMax )
6882 {
6883 // empty column - give default width (notice that if widthMax is less
6884 // than default width but != 0, it's ok)
6885 widthMax = m_defaultColWidth;
6886 }
6887 else
6888 {
6889 // leave some space around text
6890 widthMax += 10;
6891 }
6892
6893 SetColSize(col, widthMax);
6894 if ( setAsMin )
6895 {
6896 SetColMinimalWidth(col, widthMax);
6897 }
6898}
6899
6900void wxGrid::AutoSizeColumns( bool setAsMin )
6901{
6902 for ( int col = 0; col < m_numCols; col++ )
6903 {
6904 AutoSizeColumn(col, setAsMin);
6905 }
6906}
6907
f85afd4e
MB
6908//
6909// ------ cell value accessor functions
6910//
6911
6912void wxGrid::SetCellValue( int row, int col, const wxString& s )
6913{
6914 if ( m_table )
6915 {
6916 m_table->SetValue( row, col, s.c_str() );
2d66e025
MB
6917 if ( !GetBatchCount() )
6918 {
6919 wxClientDC dc( m_gridWin );
6920 PrepareDC( dc );
6921 DrawCell( dc, wxGridCellCoords(row, col) );
6922 }
60ff3b99 6923
f85afd4e 6924 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6
RD
6925 m_currentCellCoords.GetCol() == col &&
6926 IsCellEditControlEnabled())
f85afd4e 6927 {
4cfa5de6
RD
6928 HideCellEditControl();
6929 ShowCellEditControl(); // will reread data from table
f85afd4e 6930 }
f85afd4e
MB
6931 }
6932}
6933
6934
6935//
2d66e025 6936// ------ Block, row and col selection
f85afd4e
MB
6937//
6938
6939void wxGrid::SelectRow( int row, bool addToSelected )
6940{
2d66e025 6941 wxRect r;
60ff3b99 6942
f85afd4e
MB
6943 if ( IsSelection() && addToSelected )
6944 {
70c7a608 6945 wxRect rect[4];
ee6694a7
VZ
6946 bool need_refresh[4];
6947 need_refresh[0] =
6948 need_refresh[1] =
6949 need_refresh[2] =
6950 need_refresh[3] = FALSE;
6951
70c7a608
SN
6952 int i;
6953
6954 wxCoord oldLeft = m_selectedTopLeft.GetCol();
6955 wxCoord oldTop = m_selectedTopLeft.GetRow();
6956 wxCoord oldRight = m_selectedBottomRight.GetCol();
6957 wxCoord oldBottom = m_selectedBottomRight.GetRow();
6958
6959 if ( oldTop > row )
6960 {
6961 need_refresh[0] = TRUE;
6962 rect[0] = BlockToDeviceRect( wxGridCellCoords ( row, 0 ),
6963 wxGridCellCoords ( oldTop - 1,
6964 m_numCols - 1 ) );
f85afd4e 6965 m_selectedTopLeft.SetRow( row );
70c7a608 6966 }
8f177c8e 6967
70c7a608
SN
6968 if ( oldLeft > 0 )
6969 {
6970 need_refresh[1] = TRUE;
6971 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, 0 ),
6972 wxGridCellCoords ( oldBottom,
6973 oldLeft - 1 ) );
8f177c8e 6974
70c7a608
SN
6975 m_selectedTopLeft.SetCol( 0 );
6976 }
6977
6978 if ( oldBottom < row )
6979 {
6980 need_refresh[2] = TRUE;
6981 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1, 0 ),
6982 wxGridCellCoords ( row,
6983 m_numCols - 1 ) );
f85afd4e 6984 m_selectedBottomRight.SetRow( row );
70c7a608 6985 }
8f177c8e 6986
70c7a608
SN
6987 if ( oldRight < m_numCols - 1 )
6988 {
6989 need_refresh[3] = TRUE;
6990 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop ,
6991 oldRight + 1 ),
6992 wxGridCellCoords ( oldBottom,
6993 m_numCols - 1 ) );
6994 m_selectedBottomRight.SetCol( m_numCols - 1 );
6995 }
2d66e025 6996
70c7a608
SN
6997 for (i = 0; i < 4; i++ )
6998 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
c6a51dcd 6999 m_gridWin->Refresh( FALSE, &(rect[i]) );
f85afd4e
MB
7000 }
7001 else
7002 {
2d66e025 7003 r = SelectionToDeviceRect();
f85afd4e 7004 ClearSelection();
c6a51dcd 7005 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
60ff3b99 7006
f85afd4e
MB
7007 m_selectedTopLeft.Set( row, 0 );
7008 m_selectedBottomRight.Set( row, m_numCols-1 );
2d66e025 7009 r = SelectionToDeviceRect();
c6a51dcd 7010 m_gridWin->Refresh( FALSE, &r );
f85afd4e 7011 }
8f177c8e 7012
f85afd4e 7013 wxGridRangeSelectEvent gridEvt( GetId(),
b54ba671 7014 wxEVT_GRID_RANGE_SELECT,
f85afd4e
MB
7015 this,
7016 m_selectedTopLeft,
7017 m_selectedBottomRight );
7018
7019 GetEventHandler()->ProcessEvent(gridEvt);
7020}
7021
7022
7023void wxGrid::SelectCol( int col, bool addToSelected )
7024{
2d66e025 7025 if ( IsSelection() && addToSelected )
f85afd4e 7026 {
70c7a608 7027 wxRect rect[4];
ee6694a7
VZ
7028 bool need_refresh[4];
7029 need_refresh[0] =
7030 need_refresh[1] =
7031 need_refresh[2] =
7032 need_refresh[3] = FALSE;
70c7a608
SN
7033 int i;
7034
7035 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7036 wxCoord oldTop = m_selectedTopLeft.GetRow();
7037 wxCoord oldRight = m_selectedBottomRight.GetCol();
7038 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7039
7040 if ( oldLeft > col )
7041 {
7042 need_refresh[0] = TRUE;
7043 rect[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col ),
b99be8fb 7044 wxGridCellCoords ( m_numRows - 1,
70c7a608 7045 oldLeft - 1 ) );
f85afd4e 7046 m_selectedTopLeft.SetCol( col );
70c7a608 7047 }
f85afd4e 7048
70c7a608
SN
7049 if ( oldTop > 0 )
7050 {
7051 need_refresh[1] = TRUE;
7052 rect[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft ),
b99be8fb 7053 wxGridCellCoords ( oldTop - 1,
70c7a608
SN
7054 oldRight ) );
7055 m_selectedTopLeft.SetRow( 0 );
7056 }
f85afd4e 7057
70c7a608
SN
7058 if ( oldRight < col )
7059 {
7060 need_refresh[2] = TRUE;
7061 rect[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight + 1 ),
7062 wxGridCellCoords ( m_numRows - 1,
7063 col ) );
f85afd4e 7064 m_selectedBottomRight.SetCol( col );
70c7a608 7065 }
f85afd4e 7066
70c7a608
SN
7067 if ( oldBottom < m_numRows - 1 )
7068 {
7069 need_refresh[3] = TRUE;
7070 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1,
7071 oldLeft ),
7072 wxGridCellCoords ( m_numRows - 1,
7073 oldRight ) );
7074 m_selectedBottomRight.SetRow( m_numRows - 1 );
7075 }
2d66e025 7076
70c7a608
SN
7077 for (i = 0; i < 4; i++ )
7078 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
c6a51dcd 7079 m_gridWin->Refresh( FALSE, &(rect[i]) );
f85afd4e
MB
7080 }
7081 else
7082 {
70c7a608 7083 wxRect r;
b99be8fb 7084
2d66e025 7085 r = SelectionToDeviceRect();
f85afd4e 7086 ClearSelection();
c6a51dcd 7087 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
60ff3b99 7088
f85afd4e
MB
7089 m_selectedTopLeft.Set( 0, col );
7090 m_selectedBottomRight.Set( m_numRows-1, col );
2d66e025 7091 r = SelectionToDeviceRect();
c6a51dcd 7092 m_gridWin->Refresh( FALSE, &r );
f85afd4e
MB
7093 }
7094
7095 wxGridRangeSelectEvent gridEvt( GetId(),
b54ba671 7096 wxEVT_GRID_RANGE_SELECT,
f85afd4e
MB
7097 this,
7098 m_selectedTopLeft,
7099 m_selectedBottomRight );
7100
7101 GetEventHandler()->ProcessEvent(gridEvt);
7102}
7103
7104
7105void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7106{
7107 int temp;
da6af900 7108 wxGridCellCoords updateTopLeft, updateBottomRight;
8f177c8e 7109
f85afd4e
MB
7110 if ( topRow > bottomRow )
7111 {
7112 temp = topRow;
7113 topRow = bottomRow;
7114 bottomRow = temp;
7115 }
7116
7117 if ( leftCol > rightCol )
7118 {
7119 temp = leftCol;
7120 leftCol = rightCol;
7121 rightCol = temp;
7122 }
f0102d2a 7123
70c7a608
SN
7124 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7125 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
da6af900 7126
70c7a608
SN
7127 if ( m_selectedTopLeft != updateTopLeft ||
7128 m_selectedBottomRight != updateBottomRight )
da6af900 7129 {
70c7a608
SN
7130 // Compute two optimal update rectangles:
7131 // Either one rectangle is a real subset of the
7132 // other, or they are (almost) disjoint!
7133 wxRect rect[4];
ee6694a7
VZ
7134 bool need_refresh[4];
7135 need_refresh[0] =
7136 need_refresh[1] =
7137 need_refresh[2] =
7138 need_refresh[3] = FALSE;
70c7a608
SN
7139 int i;
7140
7141 // Store intermediate values
7142 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7143 wxCoord oldTop = m_selectedTopLeft.GetRow();
7144 wxCoord oldRight = m_selectedBottomRight.GetCol();
7145 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7146
7147 // Determine the outer/inner coordinates.
7148 if (oldLeft > leftCol)
f0102d2a 7149 {
70c7a608
SN
7150 temp = oldLeft;
7151 oldLeft = leftCol;
7152 leftCol = temp;
cb309039 7153 }
70c7a608 7154 if (oldTop > topRow )
f0102d2a 7155 {
70c7a608
SN
7156 temp = oldTop;
7157 oldTop = topRow;
7158 topRow = temp;
cb309039
SN
7159 }
7160 if (oldRight < rightCol )
70c7a608
SN
7161 {
7162 temp = oldRight;
7163 oldRight = rightCol;
7164 rightCol = temp;
cb309039
SN
7165 }
7166 if (oldBottom < bottomRow)
7167 {
70c7a608
SN
7168 temp = oldBottom;
7169 oldBottom = bottomRow;
7170 bottomRow = temp;
cb309039 7171 }
70c7a608
SN
7172
7173 // Now, either the stuff marked old is the outer
7174 // rectangle or we don't have a situation where one
7175 // is contained in the other.
b99be8fb 7176
cb309039
SN
7177 if ( oldLeft < leftCol )
7178 {
7179 need_refresh[0] = TRUE;
7180 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7181 oldLeft ),
b99be8fb 7182 wxGridCellCoords ( oldBottom,
cb309039
SN
7183 leftCol - 1 ) );
7184 }
7185
7186 if ( oldTop < topRow )
7187 {
7188 need_refresh[1] = TRUE;
7189 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7190 leftCol ),
b99be8fb 7191 wxGridCellCoords ( topRow - 1,
cb309039
SN
7192 rightCol ) );
7193 }
7194
7195 if ( oldRight > rightCol )
7196 {
7197 need_refresh[2] = TRUE;
7198 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7199 rightCol + 1 ),
7200 wxGridCellCoords ( oldBottom,
7201 oldRight ) );
7202 }
7203
7204 if ( oldBottom > bottomRow )
7205 {
7206 need_refresh[3] = TRUE;
7207 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
7208 leftCol ),
7209 wxGridCellCoords ( oldBottom,
7210 rightCol ) );
7211 }
70c7a608
SN
7212
7213
7214 // Change Selection
7215 m_selectedTopLeft = updateTopLeft;
7216 m_selectedBottomRight = updateBottomRight;
b99be8fb 7217
70c7a608
SN
7218 // various Refresh() calls
7219 for (i = 0; i < 4; i++ )
7220 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
c6a51dcd 7221 m_gridWin->Refresh( FALSE, &(rect[i]) );
da6af900 7222 }
f85afd4e
MB
7223
7224 // only generate an event if the block is not being selected by
7225 // dragging the mouse (in which case the event will be generated in
2d66e025 7226 // the mouse event handler)
f85afd4e
MB
7227 if ( !m_isDragging )
7228 {
7229 wxGridRangeSelectEvent gridEvt( GetId(),
b54ba671 7230 wxEVT_GRID_RANGE_SELECT,
f85afd4e
MB
7231 this,
7232 m_selectedTopLeft,
7233 m_selectedBottomRight );
8f177c8e 7234
f85afd4e
MB
7235 GetEventHandler()->ProcessEvent(gridEvt);
7236 }
7237}
7238
7239void wxGrid::SelectAll()
7240{
7241 m_selectedTopLeft.Set( 0, 0 );
7242 m_selectedBottomRight.Set( m_numRows-1, m_numCols-1 );
7243
2d66e025 7244 m_gridWin->Refresh();
f85afd4e
MB
7245}
7246
7247
7248void wxGrid::ClearSelection()
7249{
2d66e025
MB
7250 m_selectedTopLeft = wxGridNoCellCoords;
7251 m_selectedBottomRight = wxGridNoCellCoords;
8f177c8e 7252}
f85afd4e
MB
7253
7254
da6af900 7255// This function returns the rectangle that encloses the given block
2d66e025
MB
7256// in device coords clipped to the client size of the grid window.
7257//
58dd5b3b
MB
7258wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
7259 const wxGridCellCoords &bottomRight )
f85afd4e 7260{
58dd5b3b 7261 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
7262 wxRect cellRect;
7263
58dd5b3b
MB
7264 cellRect = CellToRect( topLeft );
7265 if ( cellRect != wxGridNoCellRect )
f85afd4e 7266 {
58dd5b3b
MB
7267 rect = cellRect;
7268 }
7269 else
7270 {
7271 rect = wxRect( 0, 0, 0, 0 );
7272 }
60ff3b99 7273
58dd5b3b
MB
7274 cellRect = CellToRect( bottomRight );
7275 if ( cellRect != wxGridNoCellRect )
7276 {
7277 rect += cellRect;
2d66e025
MB
7278 }
7279 else
7280 {
7281 return wxGridNoCellRect;
f85afd4e
MB
7282 }
7283
58dd5b3b
MB
7284 // convert to scrolled coords
7285 //
7286 int left, top, right, bottom;
7287 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
7288 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
7289
7290 int cw, ch;
7291 m_gridWin->GetClientSize( &cw, &ch );
7292
7293 rect.SetLeft( wxMax(0, left) );
7294 rect.SetTop( wxMax(0, top) );
7295 rect.SetRight( wxMin(cw, right) );
7296 rect.SetBottom( wxMin(ch, bottom) );
7297
f85afd4e
MB
7298 return rect;
7299}
7300
7301
58dd5b3b 7302
f85afd4e
MB
7303//
7304// ------ Grid event classes
7305//
7306
7307IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent )
7308
7309wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
7310 int row, int col, int x, int y,
7311 bool control, bool shift, bool alt, bool meta )
7312 : wxNotifyEvent( type, id )
7313{
7314 m_row = row;
7315 m_col = col;
7316 m_x = x;
7317 m_y = y;
7318 m_control = control;
7319 m_shift = shift;
7320 m_alt = alt;
7321 m_meta = meta;
8f177c8e 7322
f85afd4e
MB
7323 SetEventObject(obj);
7324}
7325
7326
7327IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent )
7328
7329wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
7330 int rowOrCol, int x, int y,
7331 bool control, bool shift, bool alt, bool meta )
7332 : wxNotifyEvent( type, id )
7333{
7334 m_rowOrCol = rowOrCol;
7335 m_x = x;
7336 m_y = y;
7337 m_control = control;
7338 m_shift = shift;
7339 m_alt = alt;
7340 m_meta = meta;
8f177c8e 7341
f85afd4e
MB
7342 SetEventObject(obj);
7343}
7344
7345
7346IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent )
7347
7348wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
7349 const wxGridCellCoords& topLeft,
7350 const wxGridCellCoords& bottomRight,
7351 bool control, bool shift, bool alt, bool meta )
7352 : wxNotifyEvent( type, id )
f85afd4e
MB
7353{
7354 m_topLeft = topLeft;
7355 m_bottomRight = bottomRight;
7356 m_control = control;
7357 m_shift = shift;
7358 m_alt = alt;
7359 m_meta = meta;
7360
7361 SetEventObject(obj);
7362}
7363
7364
7365#endif // ifndef wxUSE_NEW_GRID
7366