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