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