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