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