]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
relayout the window after adding/removing the columns (fixes a SF bug I closed a...
[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
1052 fmt = _T("%g");
1053 }
1054 else if ( m_precision == -1 )
1055 {
1056 // default precision
1057 fmt.Printf(_T("%%%d.g"), m_width);
1058 }
1059 else
1060 {
1061 fmt.Printf(_T("%%%d.%dg"), m_width, m_precision);
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
3507// m_defaultRowHeight/m_defaultColWidth or binary search on array
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;
b5808881 3607 m_selection = 0;
f2d76237 3608 // subwindow components that make up the wxGrid
7807d81c
MB
3609 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3610 -1,
18f9565d
MB
3611 wxDefaultPosition,
3612 wxDefaultSize );
7807d81c 3613
60ff3b99
VZ
3614 m_rowLabelWin = new wxGridRowLabelWindow( this,
3615 -1,
18f9565d
MB
3616 wxDefaultPosition,
3617 wxDefaultSize );
2d66e025
MB
3618
3619 m_colLabelWin = new wxGridColLabelWindow( this,
3620 -1,
18f9565d
MB
3621 wxDefaultPosition,
3622 wxDefaultSize );
60ff3b99 3623
60ff3b99
VZ
3624 m_gridWin = new wxGridWindow( this,
3625 m_rowLabelWin,
3626 m_colLabelWin,
3627 -1,
18f9565d 3628 wxDefaultPosition,
2d66e025
MB
3629 wxDefaultSize );
3630
3631 SetTargetWindow( m_gridWin );
6f36917b
VZ
3632
3633 Init();
2d66e025 3634}
f85afd4e 3635
2d66e025 3636
b5808881
SN
3637bool wxGrid::CreateGrid( int numRows, int numCols,
3638 wxGrid::wxGridSelectionModes selmode )
2d66e025 3639{
f6bcfd97
BP
3640 wxCHECK_MSG( !m_created,
3641 FALSE,
3642 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3643
3644 m_numRows = numRows;
3645 m_numCols = numCols;
3646
3647 m_table = new wxGridStringTable( m_numRows, m_numCols );
3648 m_table->SetView( this );
3649 m_ownTable = TRUE;
3650 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
3651
3652 CalcDimensions();
3653
f6bcfd97 3654 m_created = TRUE;
2d66e025 3655
2796cce3
RD
3656 return m_created;
3657}
3658
f1567cdd
SN
3659void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
3660{
6f36917b
VZ
3661 wxCHECK_RET( m_created,
3662 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3663
3664 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
3665}
3666
043d16b2
SN
3667bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
3668 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
3669{
3670 if ( m_created )
3671 {
fb295790
RD
3672 // RD: Actually, this should probably be allowed. I think it would be
3673 // nice to be able to switch multiple Tables in and out of a single
3674 // View at runtime. Is there anything in the implmentation that would
3675 // prevent this?
3676
d95b0c2b 3677 // At least, you now have to cope with m_selection
2796cce3
RD
3678 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3679 return FALSE;
3680 }
3681 else
3682 {
3683 m_numRows = table->GetNumberRows();
3684 m_numCols = table->GetNumberCols();
3685
3686 m_table = table;
3687 m_table->SetView( this );
3688 if (takeOwnership)
3689 m_ownTable = TRUE;
043d16b2 3690 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
3691
3692 CalcDimensions();
3693
2d66e025
MB
3694 m_created = TRUE;
3695 }
f85afd4e 3696
2d66e025 3697 return m_created;
f85afd4e
MB
3698}
3699
2d66e025 3700
f85afd4e
MB
3701void wxGrid::Init()
3702{
f85afd4e
MB
3703 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3704 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3705
60ff3b99
VZ
3706 if ( m_rowLabelWin )
3707 {
3708 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
3709 }
3710 else
3711 {
3712 m_labelBackgroundColour = wxColour( _T("WHITE") );
3713 }
3714
3715 m_labelTextColour = wxColour( _T("BLACK") );
f85afd4e 3716
0a976765
VZ
3717 // init attr cache
3718 m_attrCache.row = -1;
3719
f85afd4e
MB
3720 // TODO: something better than this ?
3721 //
3722 m_labelFont = this->GetFont();
3723 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
8f177c8e 3724
4c7277db
MB
3725 m_rowLabelHorizAlign = wxALIGN_LEFT;
3726 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 3727
4c7277db
MB
3728 m_colLabelHorizAlign = wxALIGN_CENTRE;
3729 m_colLabelVertAlign = wxALIGN_TOP;
f85afd4e 3730
f85afd4e 3731 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
3732 m_defaultRowHeight = m_gridWin->GetCharHeight();
3733
d2fdd8d2 3734#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
3735 m_defaultRowHeight += 8;
3736#else
3737 m_defaultRowHeight += 4;
3738#endif
3739
2d66e025 3740 m_gridLineColour = wxColour( 128, 128, 255 );
f85afd4e 3741 m_gridLinesEnabled = TRUE;
f6bcfd97 3742 m_cellHighlightColour = m_gridLineColour;
bf7945ce 3743 m_cellHighlightPenWidth = 2;
d2520c85 3744 m_cellHighlightROPenWidth = 1;
8f177c8e 3745
58dd5b3b 3746 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 3747 m_winCapture = (wxWindow *)NULL;
6e8524b1
MB
3748 m_canDragRowSize = TRUE;
3749 m_canDragColSize = TRUE;
4cfa5de6 3750 m_canDragGridSize = TRUE;
f85afd4e
MB
3751 m_dragLastPos = -1;
3752 m_dragRowOrCol = -1;
3753 m_isDragging = FALSE;
07296f0b 3754 m_startDragPos = wxDefaultPosition;
f85afd4e 3755
07296f0b 3756 m_waitForSlowClick = FALSE;
025562fe 3757
f85afd4e
MB
3758 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
3759 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
3760
3761 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 3762
b5808881
SN
3763 m_selectingTopLeft = wxGridNoCellCoords;
3764 m_selectingBottomRight = wxGridNoCellCoords;
a756f210
VS
3765 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
3766 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 3767
f85afd4e
MB
3768 m_editable = TRUE; // default for whole grid
3769
2d66e025
MB
3770 m_inOnKeyDown = FALSE;
3771 m_batchCount = 0;
3c79cf49 3772
266e8367 3773 m_extraWidth =
526dbb95 3774 m_extraHeight = 0;
7c1cb261
VZ
3775}
3776
3777// ----------------------------------------------------------------------------
3778// the idea is to call these functions only when necessary because they create
3779// quite big arrays which eat memory mostly unnecessary - in particular, if
3780// default widths/heights are used for all rows/columns, we may not use these
3781// arrays at all
3782//
3783// with some extra code, it should be possible to only store the
3784// widths/heights different from default ones but this will be done later...
3785// ----------------------------------------------------------------------------
3786
3787void wxGrid::InitRowHeights()
3788{
3789 m_rowHeights.Empty();
3790 m_rowBottoms.Empty();
3791
3792 m_rowHeights.Alloc( m_numRows );
3793 m_rowBottoms.Alloc( m_numRows );
3794
3795 int rowBottom = 0;
3796 for ( int i = 0; i < m_numRows; i++ )
3797 {
3798 m_rowHeights.Add( m_defaultRowHeight );
3799 rowBottom += m_defaultRowHeight;
3800 m_rowBottoms.Add( rowBottom );
3801 }
3802}
3803
3804void wxGrid::InitColWidths()
3805{
3806 m_colWidths.Empty();
3807 m_colRights.Empty();
3808
3809 m_colWidths.Alloc( m_numCols );
3810 m_colRights.Alloc( m_numCols );
3811 int colRight = 0;
3812 for ( int i = 0; i < m_numCols; i++ )
3813 {
3814 m_colWidths.Add( m_defaultColWidth );
3815 colRight += m_defaultColWidth;
3816 m_colRights.Add( colRight );
3817 }
3818}
3819
3820int wxGrid::GetColWidth(int col) const
3821{
3822 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
3823}
3824
3825int wxGrid::GetColLeft(int col) const
3826{
3827 return m_colRights.IsEmpty() ? col * m_defaultColWidth
3828 : m_colRights[col] - m_colWidths[col];
3829}
3830
3831int wxGrid::GetColRight(int col) const
3832{
3833 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3834 : m_colRights[col];
3835}
3836
3837int wxGrid::GetRowHeight(int row) const
3838{
3839 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3840}
2d66e025 3841
7c1cb261
VZ
3842int wxGrid::GetRowTop(int row) const
3843{
3844 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3845 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
3846}
3847
7c1cb261
VZ
3848int wxGrid::GetRowBottom(int row) const
3849{
3850 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3851 : m_rowBottoms[row];
3852}
f85afd4e
MB
3853
3854void wxGrid::CalcDimensions()
3855{
f85afd4e 3856 int cw, ch;
2d66e025 3857 GetClientSize( &cw, &ch );
f85afd4e 3858
faec5a43 3859 if ( m_rowLabelWin->IsShown() )
ae1d0c6c
VZ
3860 cw -= m_rowLabelWidth;
3861 if ( m_colLabelWin->IsShown() )
faec5a43 3862 ch -= m_colLabelHeight;
60ff3b99 3863
faec5a43
SN
3864 // grid total size
3865 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
3866 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
3867
3868 // preserve (more or less) the previous position
3869 int x, y;
3870 GetViewStart( &x, &y );
97a9929e
VZ
3871
3872 // maybe we don't need scrollbars at all?
3873 //
3874 // also adjust the position to be valid for the new scroll rangs
faec5a43
SN
3875 if ( w <= cw )
3876 {
97a9929e 3877 w = x = 0;
faec5a43
SN
3878 }
3879 else
3880 {
edb89f7e
VZ
3881 if ( x >= w )
3882 x = w - 1;
f85afd4e 3883 }
97a9929e 3884
faec5a43
SN
3885 if ( h <= ch )
3886 {
97a9929e 3887 h = y = 0;
faec5a43
SN
3888 }
3889 else
3890 {
edb89f7e
VZ
3891 if ( y >= h )
3892 y = h - 1;
faec5a43
SN
3893 }
3894
3895 // do set scrollbar parameters
97a9929e
VZ
3896 SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y,
3897 GetScrollX(w), GetScrollY(h), x, y,
3898 GetBatchCount() != 0);
12314291
VZ
3899
3900 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3901 // still must reposition the children
3902 CalcWindowSizes();
f85afd4e
MB
3903}
3904
3905
7807d81c
MB
3906void wxGrid::CalcWindowSizes()
3907{
3908 int cw, ch;
3909 GetClientSize( &cw, &ch );
b99be8fb 3910
7807d81c
MB
3911 if ( m_cornerLabelWin->IsShown() )
3912 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3913
3914 if ( m_colLabelWin->IsShown() )
3915 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3916
3917 if ( m_rowLabelWin->IsShown() )
3918 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3919
3920 if ( m_gridWin->IsShown() )
3921 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3922}
3923
3924
f85afd4e
MB
3925// this is called when the grid table sends a message to say that it
3926// has been redimensioned
3927//
3928bool wxGrid::Redimension( wxGridTableMessage& msg )
3929{
3930 int i;
f6bcfd97 3931 bool result = FALSE;
8f177c8e 3932
f6bcfd97 3933#if 0
7c1cb261
VZ
3934 // if we were using the default widths/heights so far, we must change them
3935 // now
3936 if ( m_colWidths.IsEmpty() )
3937 {
3938 InitColWidths();
3939 }
3940
3941 if ( m_rowHeights.IsEmpty() )
3942 {
3943 InitRowHeights();
3944 }
f6bcfd97 3945#endif
7c1cb261 3946
f85afd4e
MB
3947 switch ( msg.GetId() )
3948 {
3949 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3950 {
3951 size_t pos = msg.GetCommandInt();
3952 int numRows = msg.GetCommandInt2();
f6bcfd97 3953
f85afd4e 3954 m_numRows += numRows;
2d66e025 3955
f6bcfd97
BP
3956 if ( !m_rowHeights.IsEmpty() )
3957 {
3958 for ( i = 0; i < numRows; i++ )
3959 {
3960 m_rowHeights.Insert( m_defaultRowHeight, pos );
3961 m_rowBottoms.Insert( 0, pos );
3962 }
3963
3964 int bottom = 0;
3965 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
60ff3b99 3966
f6bcfd97
BP
3967 for ( i = pos; i < m_numRows; i++ )
3968 {
3969 bottom += m_rowHeights[i];
3970 m_rowBottoms[i] = bottom;
3971 }
3972 }
3973 if ( m_currentCellCoords == wxGridNoCellCoords )
3974 {
3975 // if we have just inserted cols into an empty grid the current
3976 // cell will be undefined...
3977 //
3978 SetCurrentCell( 0, 0 );
3979 }
3980 m_selection->UpdateRows( pos, numRows );
3981 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
3982 if (attrProvider)
3983 attrProvider->UpdateAttrRows( pos, numRows );
3984
3985 if ( !GetBatchCount() )
2d66e025 3986 {
f6bcfd97
BP
3987 CalcDimensions();
3988 m_rowLabelWin->Refresh();
2d66e025 3989 }
f85afd4e 3990 }
f6bcfd97
BP
3991 result = TRUE;
3992 break;
f85afd4e
MB
3993
3994 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3995 {
3996 int numRows = msg.GetCommandInt();
2d66e025 3997 int oldNumRows = m_numRows;
f85afd4e 3998 m_numRows += numRows;
2d66e025 3999
f6bcfd97
BP
4000 if ( !m_rowHeights.IsEmpty() )
4001 {
4002 for ( i = 0; i < numRows; i++ )
4003 {
4004 m_rowHeights.Add( m_defaultRowHeight );
4005 m_rowBottoms.Add( 0 );
4006 }
60ff3b99 4007
f6bcfd97
BP
4008 int bottom = 0;
4009 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
4010
4011 for ( i = oldNumRows; i < m_numRows; i++ )
4012 {
4013 bottom += m_rowHeights[i];
4014 m_rowBottoms[i] = bottom;
4015 }
4016 }
4017 if ( m_currentCellCoords == wxGridNoCellCoords )
4018 {
4019 // if we have just inserted cols into an empty grid the current
4020 // cell will be undefined...
4021 //
4022 SetCurrentCell( 0, 0 );
4023 }
4024 if ( !GetBatchCount() )
2d66e025 4025 {
f6bcfd97
BP
4026 CalcDimensions();
4027 m_rowLabelWin->Refresh();
2d66e025 4028 }
f85afd4e 4029 }
f6bcfd97
BP
4030 result = TRUE;
4031 break;
f85afd4e
MB
4032
4033 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4034 {
4035 size_t pos = msg.GetCommandInt();
4036 int numRows = msg.GetCommandInt2();
f85afd4e
MB
4037 m_numRows -= numRows;
4038
f6bcfd97 4039 if ( !m_rowHeights.IsEmpty() )
f85afd4e 4040 {
f6bcfd97
BP
4041 for ( i = 0; i < numRows; i++ )
4042 {
4403ced6
VZ
4043 m_rowHeights.RemoveAt( pos );
4044 m_rowBottoms.RemoveAt( pos );
f6bcfd97 4045 }
2d66e025
MB
4046
4047 int h = 0;
4048 for ( i = 0; i < m_numRows; i++ )
4049 {
4050 h += m_rowHeights[i];
4051 m_rowBottoms[i] = h;
4052 }
f85afd4e 4053 }
f6bcfd97
BP
4054 if ( !m_numRows )
4055 {
4056 m_currentCellCoords = wxGridNoCellCoords;
4057 }
4058 else
4059 {
4060 if ( m_currentCellCoords.GetRow() >= m_numRows )
4061 m_currentCellCoords.Set( 0, 0 );
4062 }
4063 m_selection->UpdateRows( pos, -((int)numRows) );
4064 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4065 if (attrProvider) {
4066 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
84912ef8
RD
4067// ifdef'd out following patch from Paul Gammans
4068#if 0
3ca6a5f0 4069 // No need to touch column attributes, unless we
f6bcfd97
BP
4070 // removed _all_ rows, in this case, we remove
4071 // all column attributes.
4072 // I hate to do this here, but the
4073 // needed data is not available inside UpdateAttrRows.
4074 if ( !GetNumberRows() )
4075 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 4076#endif
f6bcfd97
BP
4077 }
4078 if ( !GetBatchCount() )
4079 {
4080 CalcDimensions();
4081 m_rowLabelWin->Refresh();
4082 }
f85afd4e 4083 }
f6bcfd97
BP
4084 result = TRUE;
4085 break;
f85afd4e
MB
4086
4087 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4088 {
4089 size_t pos = msg.GetCommandInt();
4090 int numCols = msg.GetCommandInt2();
f85afd4e 4091 m_numCols += numCols;
2d66e025 4092
f6bcfd97
BP
4093 if ( !m_colWidths.IsEmpty() )
4094 {
4095 for ( i = 0; i < numCols; i++ )
4096 {
4097 m_colWidths.Insert( m_defaultColWidth, pos );
4098 m_colRights.Insert( 0, pos );
4099 }
4100
4101 int right = 0;
4102 if ( pos > 0 ) right = m_colRights[pos-1];
60ff3b99 4103
f6bcfd97
BP
4104 for ( i = pos; i < m_numCols; i++ )
4105 {
4106 right += m_colWidths[i];
4107 m_colRights[i] = right;
4108 }
4109 }
4110 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4111 {
f6bcfd97
BP
4112 // if we have just inserted cols into an empty grid the current
4113 // cell will be undefined...
4114 //
4115 SetCurrentCell( 0, 0 );
2d66e025 4116 }
f6bcfd97
BP
4117 m_selection->UpdateCols( pos, numCols );
4118 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4119 if (attrProvider)
4120 attrProvider->UpdateAttrCols( pos, numCols );
4121 if ( !GetBatchCount() )
4122 {
4123 CalcDimensions();
4124 m_colLabelWin->Refresh();
4125 }
4126
f85afd4e 4127 }
f6bcfd97
BP
4128 result = TRUE;
4129 break;
f85afd4e
MB
4130
4131 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4132 {
4133 int numCols = msg.GetCommandInt();
2d66e025 4134 int oldNumCols = m_numCols;
f85afd4e 4135 m_numCols += numCols;
f6bcfd97
BP
4136 if ( !m_colWidths.IsEmpty() )
4137 {
4138 for ( i = 0; i < numCols; i++ )
4139 {
4140 m_colWidths.Add( m_defaultColWidth );
4141 m_colRights.Add( 0 );
4142 }
2d66e025 4143
f6bcfd97
BP
4144 int right = 0;
4145 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
60ff3b99 4146
f6bcfd97
BP
4147 for ( i = oldNumCols; i < m_numCols; i++ )
4148 {
4149 right += m_colWidths[i];
4150 m_colRights[i] = right;
4151 }
4152 }
4153 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4154 {
f6bcfd97
BP
4155 // if we have just inserted cols into an empty grid the current
4156 // cell will be undefined...
4157 //
4158 SetCurrentCell( 0, 0 );
4159 }
4160 if ( !GetBatchCount() )
4161 {
4162 CalcDimensions();
4163 m_colLabelWin->Refresh();
2d66e025 4164 }
f85afd4e 4165 }
f6bcfd97
BP
4166 result = TRUE;
4167 break;
f85afd4e
MB
4168
4169 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4170 {
4171 size_t pos = msg.GetCommandInt();
4172 int numCols = msg.GetCommandInt2();
f85afd4e 4173 m_numCols -= numCols;
f85afd4e 4174
f6bcfd97 4175 if ( !m_colWidths.IsEmpty() )
f85afd4e 4176 {
f6bcfd97
BP
4177 for ( i = 0; i < numCols; i++ )
4178 {
4403ced6
VZ
4179 m_colWidths.RemoveAt( pos );
4180 m_colRights.RemoveAt( pos );
f6bcfd97 4181 }
2d66e025
MB
4182
4183 int w = 0;
4184 for ( i = 0; i < m_numCols; i++ )
4185 {
4186 w += m_colWidths[i];
4187 m_colRights[i] = w;
4188 }
f85afd4e 4189 }
f6bcfd97
BP
4190 if ( !m_numCols )
4191 {
4192 m_currentCellCoords = wxGridNoCellCoords;
4193 }
4194 else
4195 {
4196 if ( m_currentCellCoords.GetCol() >= m_numCols )
4197 m_currentCellCoords.Set( 0, 0 );
4198 }
4199 m_selection->UpdateCols( pos, -((int)numCols) );
4200 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4201 if (attrProvider) {
4202 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
84912ef8
RD
4203// ifdef'd out following patch from Paul Gammans
4204#if 0
f6bcfd97
BP
4205 // No need to touch row attributes, unless we
4206 // removed _all_ columns, in this case, we remove
4207 // all row attributes.
4208 // I hate to do this here, but the
4209 // needed data is not available inside UpdateAttrCols.
4210 if ( !GetNumberCols() )
4211 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 4212#endif
f6bcfd97
BP
4213 }
4214 if ( !GetBatchCount() )
4215 {
4216 CalcDimensions();
4217 m_colLabelWin->Refresh();
4218 }
f85afd4e 4219 }
faec5a43 4220 result = TRUE;
f6bcfd97 4221 break;
f85afd4e
MB
4222 }
4223
f6bcfd97
BP
4224 if (result && !GetBatchCount() )
4225 m_gridWin->Refresh();
4226 return result;
f85afd4e
MB
4227}
4228
4229
d10f4bf9 4230wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
f85afd4e 4231{
2d66e025
MB
4232 wxRegionIterator iter( reg );
4233 wxRect r;
f85afd4e 4234
275c4ae4
RD
4235 wxArrayInt rowlabels;
4236
2d66e025
MB
4237 int top, bottom;
4238 while ( iter )
f85afd4e 4239 {
2d66e025 4240 r = iter.GetRect();
f85afd4e 4241
2d66e025
MB
4242 // TODO: remove this when we can...
4243 // There is a bug in wxMotif that gives garbage update
4244 // rectangles if you jump-scroll a long way by clicking the
4245 // scrollbar with middle button. This is a work-around
4246 //
4247#if defined(__WXMOTIF__)
4248 int cw, ch;
4249 m_gridWin->GetClientSize( &cw, &ch );
4250 if ( r.GetTop() > ch ) r.SetTop( 0 );
4251 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4252#endif
f85afd4e 4253
2d66e025
MB
4254 // logical bounds of update region
4255 //
4256 int dummy;
4257 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4258 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4259
4260 // find the row labels within these bounds
4261 //
4262 int row;
33188aa4 4263 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 4264 {
7c1cb261
VZ
4265 if ( GetRowBottom(row) < top )
4266 continue;
2d66e025 4267
6d55126d 4268 if ( GetRowTop(row) > bottom )
7c1cb261 4269 break;
60ff3b99 4270
d10f4bf9 4271 rowlabels.Add( row );
2d66e025 4272 }
60ff3b99 4273
2d66e025 4274 iter++ ;
f85afd4e 4275 }
d10f4bf9
VZ
4276
4277 return rowlabels;
f85afd4e
MB
4278}
4279
4280
d10f4bf9 4281wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
f85afd4e 4282{
2d66e025
MB
4283 wxRegionIterator iter( reg );
4284 wxRect r;
f85afd4e 4285
d10f4bf9 4286 wxArrayInt colLabels;
f85afd4e 4287
2d66e025
MB
4288 int left, right;
4289 while ( iter )
f85afd4e 4290 {
2d66e025 4291 r = iter.GetRect();
f85afd4e 4292
2d66e025
MB
4293 // TODO: remove this when we can...
4294 // There is a bug in wxMotif that gives garbage update
4295 // rectangles if you jump-scroll a long way by clicking the
4296 // scrollbar with middle button. This is a work-around
4297 //
4298#if defined(__WXMOTIF__)
4299 int cw, ch;
4300 m_gridWin->GetClientSize( &cw, &ch );
4301 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4302 r.SetRight( wxMin( r.GetRight(), cw ) );
4303#endif
4304
4305 // logical bounds of update region
4306 //
4307 int dummy;
4308 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4309 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4310
4311 // find the cells within these bounds
4312 //
4313 int col;
33188aa4 4314 for ( col = internalXToCol(left); col < m_numCols; col++ )
2d66e025 4315 {
7c1cb261
VZ
4316 if ( GetColRight(col) < left )
4317 continue;
60ff3b99 4318
7c1cb261
VZ
4319 if ( GetColLeft(col) > right )
4320 break;
2d66e025 4321
d10f4bf9 4322 colLabels.Add( col );
2d66e025 4323 }
60ff3b99 4324
2d66e025 4325 iter++ ;
f85afd4e 4326 }
d10f4bf9 4327 return colLabels;
f85afd4e
MB
4328}
4329
4330
d10f4bf9 4331wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
f85afd4e 4332{
2d66e025
MB
4333 wxRegionIterator iter( reg );
4334 wxRect r;
f85afd4e 4335
d10f4bf9 4336 wxGridCellCoordsArray cellsExposed;
f85afd4e 4337
2d66e025
MB
4338 int left, top, right, bottom;
4339 while ( iter )
4340 {
4341 r = iter.GetRect();
f85afd4e 4342
2d66e025
MB
4343 // TODO: remove this when we can...
4344 // There is a bug in wxMotif that gives garbage update
4345 // rectangles if you jump-scroll a long way by clicking the
4346 // scrollbar with middle button. This is a work-around
4347 //
4348#if defined(__WXMOTIF__)
f85afd4e 4349 int cw, ch;
2d66e025
MB
4350 m_gridWin->GetClientSize( &cw, &ch );
4351 if ( r.GetTop() > ch ) r.SetTop( 0 );
4352 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4353 r.SetRight( wxMin( r.GetRight(), cw ) );
4354 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4355#endif
8f177c8e 4356
2d66e025
MB
4357 // logical bounds of update region
4358 //
4359 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
4360 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 4361
2d66e025 4362 // find the cells within these bounds
f85afd4e 4363 //
2d66e025 4364 int row, col;
33188aa4 4365 for ( row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 4366 {
7c1cb261
VZ
4367 if ( GetRowBottom(row) <= top )
4368 continue;
f85afd4e 4369
7c1cb261
VZ
4370 if ( GetRowTop(row) > bottom )
4371 break;
60ff3b99 4372
33188aa4 4373 for ( col = internalXToCol(left); col < m_numCols; col++ )
2d66e025 4374 {
7c1cb261
VZ
4375 if ( GetColRight(col) <= left )
4376 continue;
60ff3b99 4377
7c1cb261
VZ
4378 if ( GetColLeft(col) > right )
4379 break;
60ff3b99 4380
d10f4bf9 4381 cellsExposed.Add( wxGridCellCoords( row, col ) );
2d66e025
MB
4382 }
4383 }
60ff3b99 4384
7c1cb261 4385 iter++;
f85afd4e 4386 }
d10f4bf9
VZ
4387
4388 return cellsExposed;
f85afd4e
MB
4389}
4390
4391
2d66e025 4392void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 4393{
2d66e025
MB
4394 int x, y, row;
4395 wxPoint pos( event.GetPosition() );
4396 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4397
2d66e025 4398 if ( event.Dragging() )
f85afd4e 4399 {
426b2d87
JS
4400 if (!m_isDragging)
4401 {
4402 m_isDragging = TRUE;
4403 m_rowLabelWin->CaptureMouse();
4404 }
8f177c8e 4405
2d66e025 4406 if ( event.LeftIsDown() )
f85afd4e
MB
4407 {
4408 switch( m_cursorMode )
4409 {
f85afd4e
MB
4410 case WXGRID_CURSOR_RESIZE_ROW:
4411 {
2d66e025
MB
4412 int cw, ch, left, dummy;
4413 m_gridWin->GetClientSize( &cw, &ch );
4414 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 4415
2d66e025
MB
4416 wxClientDC dc( m_gridWin );
4417 PrepareDC( dc );
af547d51
VZ
4418 y = wxMax( y,
4419 GetRowTop(m_dragRowOrCol) +
4420 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 4421 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
4422 if ( m_dragLastPos >= 0 )
4423 {
2d66e025 4424 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 4425 }
2d66e025
MB
4426 dc.DrawLine( left, y, left+cw, y );
4427 m_dragLastPos = y;
f85afd4e
MB
4428 }
4429 break;
4430
4431 case WXGRID_CURSOR_SELECT_ROW:
e32352cf 4432 if ( (row = YToRow( y )) >= 0 )
aa5e1f75
SN
4433 {
4434 m_selection->SelectRow( row,
4435 event.ControlDown(),
4436 event.ShiftDown(),
4437 event.AltDown(),
4438 event.MetaDown() );
f85afd4e 4439 }
e2b42eeb
VZ
4440
4441 // default label to suppress warnings about "enumeration value
4442 // 'xxx' not handled in switch
4443 default:
4444 break;
f85afd4e
MB
4445 }
4446 }
4447 return;
4448 }
4449
426b2d87
JS
4450 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4451 return;
8f177c8e 4452
426b2d87
JS
4453 if (m_isDragging)
4454 {
4455 m_rowLabelWin->ReleaseMouse();
4456 m_isDragging = FALSE;
4457 }
60ff3b99 4458
6d004f67
MB
4459 // ------------ Entering or leaving the window
4460 //
4461 if ( event.Entering() || event.Leaving() )
4462 {
e2b42eeb 4463 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
4464 }
4465
e2b42eeb 4466
2d66e025 4467 // ------------ Left button pressed
f85afd4e 4468 //
6d004f67 4469 else if ( event.LeftDown() )
f85afd4e 4470 {
2d66e025
MB
4471 // don't send a label click event for a hit on the
4472 // edge of the row label - this is probably the user
4473 // wanting to resize the row
4474 //
4475 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 4476 {
2d66e025 4477 row = YToRow(y);
58dd5b3b 4478 if ( row >= 0 &&
b54ba671 4479 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 4480 {
aa5e1f75
SN
4481 if ( !event.ShiftDown() && !event.ControlDown() )
4482 ClearSelection();
4483 if ( event.ShiftDown() )
4484 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4485 0,
4486 row,
4487 GetNumberCols() - 1,
4488 event.ControlDown(),
4489 event.ShiftDown(),
4490 event.AltDown(),
4491 event.MetaDown() );
4492 else
4493 m_selection->SelectRow( row,
4494 event.ControlDown(),
4495 event.ShiftDown(),
4496 event.AltDown(),
4497 event.MetaDown() );
e2b42eeb 4498 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 4499 }
2d66e025
MB
4500 }
4501 else
4502 {
4503 // starting to drag-resize a row
4504 //
6e8524b1
MB
4505 if ( CanDragRowSize() )
4506 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
4507 }
4508 }
f85afd4e 4509
f85afd4e 4510
2d66e025
MB
4511 // ------------ Left double click
4512 //
4513 else if (event.LeftDClick() )
4514 {
4515 if ( YToEdgeOfRow(y) < 0 )
4516 {
4517 row = YToRow(y);
b54ba671 4518 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
f85afd4e
MB
4519 }
4520 }
60ff3b99
VZ
4521
4522
2d66e025 4523 // ------------ Left button released
f85afd4e 4524 //
2d66e025 4525 else if ( event.LeftUp() )
f85afd4e 4526 {
2d66e025 4527 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 4528 {
6d004f67 4529 DoEndDragResizeRow();
60ff3b99 4530
6d004f67
MB
4531 // Note: we are ending the event *after* doing
4532 // default processing in this case
4533 //
b54ba671 4534 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 4535 }
f85afd4e 4536
e2b42eeb
VZ
4537 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4538 m_dragLastPos = -1;
2d66e025 4539 }
f85afd4e 4540
f85afd4e 4541
2d66e025
MB
4542 // ------------ Right button down
4543 //
4544 else if ( event.RightDown() )
4545 {
4546 row = YToRow(y);
b54ba671 4547 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
4548 {
4549 // no default action at the moment
f85afd4e
MB
4550 }
4551 }
60ff3b99
VZ
4552
4553
2d66e025 4554 // ------------ Right double click
f85afd4e 4555 //
2d66e025
MB
4556 else if ( event.RightDClick() )
4557 {
4558 row = YToRow(y);
b54ba671 4559 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
4560 {
4561 // no default action at the moment
4562 }
4563 }
60ff3b99
VZ
4564
4565
2d66e025 4566 // ------------ No buttons down and mouse moving
f85afd4e 4567 //
2d66e025 4568 else if ( event.Moving() )
f85afd4e 4569 {
2d66e025
MB
4570 m_dragRowOrCol = YToEdgeOfRow( y );
4571 if ( m_dragRowOrCol >= 0 )
8f177c8e 4572 {
2d66e025 4573 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 4574 {
e2b42eeb 4575 // don't capture the mouse yet
6e8524b1
MB
4576 if ( CanDragRowSize() )
4577 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
8f177c8e 4578 }
2d66e025 4579 }
6d004f67 4580 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 4581 {
e2b42eeb 4582 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
8f177c8e 4583 }
f85afd4e 4584 }
2d66e025
MB
4585}
4586
4587
4588void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
4589{
4590 int x, y, col;
4591 wxPoint pos( event.GetPosition() );
4592 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4593
2d66e025 4594 if ( event.Dragging() )
f85afd4e 4595 {
fe77cf60
JS
4596 if (!m_isDragging)
4597 {
4598 m_isDragging = TRUE;
4599 m_colLabelWin->CaptureMouse();
4600 }
8f177c8e 4601
2d66e025 4602 if ( event.LeftIsDown() )
8f177c8e 4603 {
2d66e025 4604 switch( m_cursorMode )
8f177c8e 4605 {
2d66e025 4606 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 4607 {
2d66e025
MB
4608 int cw, ch, dummy, top;
4609 m_gridWin->GetClientSize( &cw, &ch );
4610 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 4611
2d66e025
MB
4612 wxClientDC dc( m_gridWin );
4613 PrepareDC( dc );
43947979
VZ
4614
4615 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4616 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 4617 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
4618 if ( m_dragLastPos >= 0 )
4619 {
4620 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4621 }
4622 dc.DrawLine( x, top, x, top+ch );
4623 m_dragLastPos = x;
f85afd4e 4624 }
2d66e025 4625 break;
f85afd4e 4626
2d66e025 4627 case WXGRID_CURSOR_SELECT_COL:
e32352cf 4628 if ( (col = XToCol( x )) >= 0 )
aa5e1f75
SN
4629 {
4630 m_selection->SelectCol( col,
4631 event.ControlDown(),
4632 event.ShiftDown(),
4633 event.AltDown(),
4634 event.MetaDown() );
2d66e025 4635 }
e2b42eeb
VZ
4636
4637 // default label to suppress warnings about "enumeration value
4638 // 'xxx' not handled in switch
4639 default:
4640 break;
2d66e025 4641 }
f85afd4e 4642 }
2d66e025 4643 return;
f85afd4e 4644 }
2d66e025 4645
fe77cf60
JS
4646 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4647 return;
2d66e025 4648
fe77cf60
JS
4649 if (m_isDragging)
4650 {
4651 m_colLabelWin->ReleaseMouse();
4652 m_isDragging = FALSE;
4653 }
60ff3b99 4654
6d004f67
MB
4655 // ------------ Entering or leaving the window
4656 //
4657 if ( event.Entering() || event.Leaving() )
4658 {
e2b42eeb 4659 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
4660 }
4661
e2b42eeb 4662
2d66e025 4663 // ------------ Left button pressed
f85afd4e 4664 //
6d004f67 4665 else if ( event.LeftDown() )
f85afd4e 4666 {
2d66e025
MB
4667 // don't send a label click event for a hit on the
4668 // edge of the col label - this is probably the user
4669 // wanting to resize the col
4670 //
4671 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 4672 {
2d66e025 4673 col = XToCol(x);
58dd5b3b 4674 if ( col >= 0 &&
b54ba671 4675 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 4676 {
aa5e1f75
SN
4677 if ( !event.ShiftDown() && !event.ControlDown() )
4678 ClearSelection();
4679 if ( event.ShiftDown() )
4680 m_selection->SelectBlock( 0,
4681 m_currentCellCoords.GetCol(),
4682 GetNumberRows() - 1, col,
4683 event.ControlDown(),
4684 event.ShiftDown(),
4685 event.AltDown(),
4686 event.MetaDown() );
4687 else
4688 m_selection->SelectCol( col,
4689 event.ControlDown(),
4690 event.ShiftDown(),
4691 event.AltDown(),
4692 event.MetaDown() );
e2b42eeb 4693 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
f85afd4e 4694 }
2d66e025
MB
4695 }
4696 else
4697 {
4698 // starting to drag-resize a col
4699 //
6e8524b1
MB
4700 if ( CanDragColSize() )
4701 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
4702 }
4703 }
f85afd4e 4704
f85afd4e 4705
2d66e025
MB
4706 // ------------ Left double click
4707 //
4708 if ( event.LeftDClick() )
4709 {
4710 if ( XToEdgeOfCol(x) < 0 )
4711 {
4712 col = XToCol(x);
b54ba671 4713 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
2d66e025
MB
4714 }
4715 }
60ff3b99
VZ
4716
4717
2d66e025
MB
4718 // ------------ Left button released
4719 //
4720 else if ( event.LeftUp() )
4721 {
4722 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4723 {
6d004f67 4724 DoEndDragResizeCol();
e2b42eeb 4725
6d004f67
MB
4726 // Note: we are ending the event *after* doing
4727 // default processing in this case
4728 //
b54ba671 4729 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
2d66e025 4730 }
f85afd4e 4731
e2b42eeb 4732 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
2d66e025 4733 m_dragLastPos = -1;
60ff3b99
VZ
4734 }
4735
4736
2d66e025
MB
4737 // ------------ Right button down
4738 //
4739 else if ( event.RightDown() )
4740 {
4741 col = XToCol(x);
b54ba671 4742 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
4743 {
4744 // no default action at the moment
f85afd4e
MB
4745 }
4746 }
60ff3b99
VZ
4747
4748
2d66e025 4749 // ------------ Right double click
f85afd4e 4750 //
2d66e025
MB
4751 else if ( event.RightDClick() )
4752 {
4753 col = XToCol(x);
b54ba671 4754 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
4755 {
4756 // no default action at the moment
4757 }
4758 }
60ff3b99
VZ
4759
4760
2d66e025 4761 // ------------ No buttons down and mouse moving
f85afd4e 4762 //
2d66e025 4763 else if ( event.Moving() )
f85afd4e 4764 {
2d66e025
MB
4765 m_dragRowOrCol = XToEdgeOfCol( x );
4766 if ( m_dragRowOrCol >= 0 )
f85afd4e 4767 {
2d66e025 4768 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4769 {
e2b42eeb 4770 // don't capture the cursor yet
6e8524b1
MB
4771 if ( CanDragColSize() )
4772 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
f85afd4e 4773 }
2d66e025 4774 }
6d004f67 4775 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 4776 {
e2b42eeb 4777 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
8f177c8e 4778 }
f85afd4e
MB
4779 }
4780}
4781
4782
2d66e025 4783void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 4784{
2d66e025 4785 if ( event.LeftDown() )
f85afd4e 4786 {
2d66e025
MB
4787 // indicate corner label by having both row and
4788 // col args == -1
f85afd4e 4789 //
b54ba671 4790 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
4791 {
4792 SelectAll();
4793 }
f85afd4e
MB
4794 }
4795
2d66e025
MB
4796 else if ( event.LeftDClick() )
4797 {
b54ba671 4798 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 4799 }
8f177c8e 4800
2d66e025 4801 else if ( event.RightDown() )
f85afd4e 4802 {
b54ba671 4803 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 4804 {
2d66e025
MB
4805 // no default action at the moment
4806 }
4807 }
f85afd4e 4808
2d66e025
MB
4809 else if ( event.RightDClick() )
4810 {
b54ba671 4811 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
4812 {
4813 // no default action at the moment
4814 }
4815 }
4816}
f85afd4e 4817
e2b42eeb
VZ
4818void wxGrid::ChangeCursorMode(CursorMode mode,
4819 wxWindow *win,
4820 bool captureMouse)
4821{
4822#ifdef __WXDEBUG__
4823 static const wxChar *cursorModes[] =
4824 {
4825 _T("SELECT_CELL"),
4826 _T("RESIZE_ROW"),
4827 _T("RESIZE_COL"),
4828 _T("SELECT_ROW"),
4829 _T("SELECT_COL")
4830 };
4831
181bfffd
VZ
4832 wxLogTrace(_T("grid"),
4833 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
4834 win == m_colLabelWin ? _T("colLabelWin")
4835 : win ? _T("rowLabelWin")
4836 : _T("gridWin"),
4837 cursorModes[m_cursorMode], cursorModes[mode]);
4838#endif // __WXDEBUG__
4839
faec5a43
SN
4840 if ( mode == m_cursorMode &&
4841 win == m_winCapture &&
4842 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
4843 return;
4844
4845 if ( !win )
4846 {
4847 // by default use the grid itself
4848 win = m_gridWin;
4849 }
4850
4851 if ( m_winCapture )
4852 {
4853 m_winCapture->ReleaseMouse();
4854 m_winCapture = (wxWindow *)NULL;
4855 }
4856
4857 m_cursorMode = mode;
4858
4859 switch ( m_cursorMode )
4860 {
4861 case WXGRID_CURSOR_RESIZE_ROW:
4862 win->SetCursor( m_rowResizeCursor );
4863 break;
4864
4865 case WXGRID_CURSOR_RESIZE_COL:
4866 win->SetCursor( m_colResizeCursor );
4867 break;
4868
4869 default:
4870 win->SetCursor( *wxSTANDARD_CURSOR );
4871 }
4872
4873 // we need to capture mouse when resizing
4874 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
4875 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
4876
4877 if ( captureMouse && resize )
4878 {
4879 win->CaptureMouse();
4880 m_winCapture = win;
4881 }
4882}
8f177c8e 4883
2d66e025
MB
4884void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
4885{
4886 int x, y;
4887 wxPoint pos( event.GetPosition() );
4888 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4889
2d66e025
MB
4890 wxGridCellCoords coords;
4891 XYToCell( x, y, coords );
60ff3b99 4892
2d66e025
MB
4893 if ( event.Dragging() )
4894 {
07296f0b
RD
4895 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4896
4897 // Don't start doing anything until the mouse has been drug at
4898 // least 3 pixels in any direction...
508011ce
VZ
4899 if (! m_isDragging)
4900 {
4901 if (m_startDragPos == wxDefaultPosition)
4902 {
07296f0b
RD
4903 m_startDragPos = pos;
4904 return;
4905 }
4906 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
4907 return;
4908 }
4909
2d66e025 4910 m_isDragging = TRUE;
2d66e025
MB
4911 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4912 {
f0102d2a
VZ
4913 // Hide the edit control, so it
4914 // won't interfer with drag-shrinking.
f6bcfd97 4915 if ( IsCellEditControlShown() )
a5777624 4916 {
f0102d2a 4917 HideCellEditControl();
a5777624
RD
4918 SaveEditControlValue();
4919 }
07296f0b
RD
4920
4921 // Have we captured the mouse yet?
508011ce
VZ
4922 if (! m_winCapture)
4923 {
07296f0b
RD
4924 m_winCapture = m_gridWin;
4925 m_winCapture->CaptureMouse();
4926 }
4927
2d66e025
MB
4928 if ( coords != wxGridNoCellCoords )
4929 {
aa5e1f75
SN
4930 if ( event.ControlDown() )
4931 {
4932 if ( m_selectingKeyboard == wxGridNoCellCoords)
4933 m_selectingKeyboard = coords;
c9097836 4934 HighlightBlock ( m_selectingKeyboard, coords );
aa5e1f75
SN
4935 }
4936 else
4937 {
4938 if ( !IsSelection() )
4939 {
c9097836 4940 HighlightBlock( coords, coords );
aa5e1f75
SN
4941 }
4942 else
4943 {
c9097836 4944 HighlightBlock( m_currentCellCoords, coords );
aa5e1f75 4945 }
f85afd4e 4946 }
07296f0b 4947
508011ce
VZ
4948 if (! IsVisible(coords))
4949 {
07296f0b
RD
4950 MakeCellVisible(coords);
4951 // TODO: need to introduce a delay or something here. The
e32352cf 4952 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 4953 }
2d66e025
MB
4954 }
4955 }
6d004f67
MB
4956 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4957 {
4958 int cw, ch, left, dummy;
4959 m_gridWin->GetClientSize( &cw, &ch );
4960 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 4961
6d004f67
MB
4962 wxClientDC dc( m_gridWin );
4963 PrepareDC( dc );
a95e38c0
VZ
4964 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
4965 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
4966 dc.SetLogicalFunction(wxINVERT);
4967 if ( m_dragLastPos >= 0 )
4968 {
4969 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4970 }
4971 dc.DrawLine( left, y, left+cw, y );
4972 m_dragLastPos = y;
4973 }
4974 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4975 {
4976 int cw, ch, dummy, top;
4977 m_gridWin->GetClientSize( &cw, &ch );
4978 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 4979
6d004f67
MB
4980 wxClientDC dc( m_gridWin );
4981 PrepareDC( dc );
43947979
VZ
4982 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4983 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
4984 dc.SetLogicalFunction(wxINVERT);
4985 if ( m_dragLastPos >= 0 )
4986 {
4987 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4988 }
4989 dc.DrawLine( x, top, x, top+ch );
4990 m_dragLastPos = x;
4991 }
e2b42eeb 4992
2d66e025
MB
4993 return;
4994 }
66242c80 4995
2d66e025 4996 m_isDragging = FALSE;
07296f0b
RD
4997 m_startDragPos = wxDefaultPosition;
4998
a5777624
RD
4999 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5000 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5001 // wxGTK
e2b42eeb 5002#if 0
a5777624
RD
5003 if ( event.Entering() || event.Leaving() )
5004 {
5005 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5006 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5007 }
5008 else
e2b42eeb
VZ
5009#endif // 0
5010
a5777624
RD
5011 // ------------ Left button pressed
5012 //
5013 if ( event.LeftDown() && coords != wxGridNoCellCoords )
f6bcfd97
BP
5014 {
5015 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5016 coords.GetRow(),
5017 coords.GetCol(),
5018 event ) )
a5777624 5019 {
f6bcfd97
BP
5020 if ( !event.ControlDown() )
5021 ClearSelection();
5022 if ( event.ShiftDown() )
5023 {
5024 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5025 m_currentCellCoords.GetCol(),
5026 coords.GetRow(),
5027 coords.GetCol(),
5028 event.ControlDown(),
5029 event.ShiftDown(),
5030 event.AltDown(),
5031 event.MetaDown() );
5032 }
5033 else if ( XToEdgeOfCol(x) < 0 &&
5034 YToEdgeOfRow(y) < 0 )
58dd5b3b 5035 {
a5777624
RD
5036 DisableCellEditControl();
5037 MakeCellVisible( coords );
5038
5039 // if this is the second click on this cell then start
5040 // the edit control
5041 if ( m_waitForSlowClick &&
5042 (coords == m_currentCellCoords) &&
5043 CanEnableCellControl())
58dd5b3b 5044 {
a5777624 5045 EnableCellEditControl();
e195a54c 5046
a5777624 5047 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
0b190b0f
VZ
5048 wxGridCellEditor *editor = attr->GetEditor(this,
5049 coords.GetRow(),
5050 coords.GetCol());
5051 editor->StartingClick();
5052 editor->DecRef();
a5777624 5053 attr->DecRef();
e195a54c 5054
a5777624
RD
5055 m_waitForSlowClick = FALSE;
5056 }
5057 else
5058 {
aa5e1f75
SN
5059 if ( event.ControlDown() )
5060 {
5061 m_selection->ToggleCellSelection( coords.GetRow(),
5062 coords.GetCol(),
5063 event.ControlDown(),
5064 event.ShiftDown(),
5065 event.AltDown(),
5066 event.MetaDown() );
5067 m_selectingTopLeft = wxGridNoCellCoords;
5068 m_selectingBottomRight = wxGridNoCellCoords;
5069 m_selectingKeyboard = coords;
5070 }
5071 else
f6bcfd97 5072 {
aa5e1f75 5073 SetCurrentCell( coords );
f6bcfd97
BP
5074 if ( m_selection->GetSelectionMode()
5075 != wxGrid::wxGridSelectCells)
c9097836 5076 HighlightBlock( coords, coords );
f6bcfd97 5077 }
a5777624 5078 m_waitForSlowClick = TRUE;
58dd5b3b
MB
5079 }
5080 }
f85afd4e 5081 }
a5777624 5082 }
f85afd4e 5083
f85afd4e 5084
a5777624
RD
5085 // ------------ Left double click
5086 //
5087 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5088 {
5089 DisableCellEditControl();
5090
5091 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 5092 {
a5777624
RD
5093 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5094 coords.GetRow(),
5095 coords.GetCol(),
5096 event );
58dd5b3b 5097 }
a5777624 5098 }
f85afd4e 5099
8f177c8e 5100
a5777624
RD
5101 // ------------ Left button released
5102 //
5103 else if ( event.LeftUp() )
5104 {
5105 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5106 {
b5808881
SN
5107 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5108 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 5109 {
a5777624 5110 if (m_winCapture)
58dd5b3b 5111 {
a5777624
RD
5112 m_winCapture->ReleaseMouse();
5113 m_winCapture = NULL;
58dd5b3b 5114 }
5c8fc7c1
SN
5115 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5116 m_selectingTopLeft.GetCol(),
5117 m_selectingBottomRight.GetRow(),
5118 m_selectingBottomRight.GetCol(),
d95b0c2b
SN
5119 event.ControlDown(),
5120 event.ShiftDown(),
5121 event.AltDown(),
5122 event.MetaDown() );
5c8fc7c1
SN
5123 m_selectingTopLeft = wxGridNoCellCoords;
5124 m_selectingBottomRight = wxGridNoCellCoords;
6d004f67 5125 }
2d66e025 5126
a5777624
RD
5127 // Show the edit control, if it has been hidden for
5128 // drag-shrinking.
5129 ShowCellEditControl();
5130 }
5131 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5132 {
5133 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5134 DoEndDragResizeRow();
e2b42eeb 5135
a5777624
RD
5136 // Note: we are ending the event *after* doing
5137 // default processing in this case
5138 //
5139 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5140 }
5141 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5142 {
5143 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5144 DoEndDragResizeCol();
e2b42eeb 5145
a5777624
RD
5146 // Note: we are ending the event *after* doing
5147 // default processing in this case
5148 //
5149 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 5150 }
f85afd4e 5151
a5777624
RD
5152 m_dragLastPos = -1;
5153 }
5154
f85afd4e 5155
a5777624
RD
5156 // ------------ Right button down
5157 //
5158 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5159 {
5160 DisableCellEditControl();
5161 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5162 coords.GetRow(),
5163 coords.GetCol(),
5164 event ) )
f85afd4e 5165 {
a5777624 5166 // no default action at the moment
60ff3b99 5167 }
a5777624 5168 }
2d66e025 5169
60ff3b99 5170
a5777624
RD
5171 // ------------ Right double click
5172 //
5173 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5174 {
5175 DisableCellEditControl();
5176 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5177 coords.GetRow(),
5178 coords.GetCol(),
5179 event ) )
f85afd4e 5180 {
a5777624 5181 // no default action at the moment
60ff3b99 5182 }
a5777624
RD
5183 }
5184
5185 // ------------ Moving and no button action
5186 //
5187 else if ( event.Moving() && !event.IsButton() )
5188 {
5189 int dragRow = YToEdgeOfRow( y );
5190 int dragCol = XToEdgeOfCol( x );
790cc417 5191
a5777624
RD
5192 // Dragging on the corner of a cell to resize in both
5193 // directions is not implemented yet...
790cc417 5194 //
a5777624 5195 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 5196 {
a5777624
RD
5197 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5198 return;
5199 }
6d004f67 5200
a5777624
RD
5201 if ( dragRow >= 0 )
5202 {
5203 m_dragRowOrCol = dragRow;
6d004f67 5204
a5777624 5205 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5206 {
a5777624
RD
5207 if ( CanDragRowSize() && CanDragGridSize() )
5208 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 5209 }
e2b42eeb 5210
b94ae1ea
VZ
5211 if ( dragCol >= 0 )
5212 {
5213 m_dragRowOrCol = dragCol;
122603f1 5214 }
b94ae1ea 5215
a5777624
RD
5216 return;
5217 }
e2b42eeb 5218
a5777624
RD
5219 if ( dragCol >= 0 )
5220 {
5221 m_dragRowOrCol = dragCol;
e2b42eeb 5222
a5777624 5223 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5224 {
a5777624
RD
5225 if ( CanDragColSize() && CanDragGridSize() )
5226 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67 5227 }
a5777624
RD
5228
5229 return;
6d004f67 5230 }
a5777624
RD
5231
5232 // Neither on a row or col edge
5233 //
5234 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5235 {
5236 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5237 }
5238 }
6d004f67
MB
5239}
5240
5241
5242void wxGrid::DoEndDragResizeRow()
5243{
5244 if ( m_dragLastPos >= 0 )
5245 {
5246 // erase the last line and resize the row
5247 //
5248 int cw, ch, left, dummy;
5249 m_gridWin->GetClientSize( &cw, &ch );
5250 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5251
5252 wxClientDC dc( m_gridWin );
5253 PrepareDC( dc );
5254 dc.SetLogicalFunction( wxINVERT );
5255 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5256 HideCellEditControl();
a5777624 5257 SaveEditControlValue();
6d004f67 5258
7c1cb261 5259 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67
MB
5260 SetRowSize( m_dragRowOrCol,
5261 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
e2b42eeb 5262
6d004f67
MB
5263 if ( !GetBatchCount() )
5264 {
5265 // Only needed to get the correct rect.y:
5266 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5267 rect.x = 0;
5268 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5269 rect.width = m_rowLabelWidth;
5270 rect.height = ch - rect.y;
5271 m_rowLabelWin->Refresh( TRUE, &rect );
5272 rect.width = cw;
5273 m_gridWin->Refresh( FALSE, &rect );
5274 }
5275
5276 ShowCellEditControl();
5277 }
5278}
5279
5280
5281void wxGrid::DoEndDragResizeCol()
5282{
5283 if ( m_dragLastPos >= 0 )
5284 {
5285 // erase the last line and resize the col
5286 //
5287 int cw, ch, dummy, top;
5288 m_gridWin->GetClientSize( &cw, &ch );
5289 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5290
5291 wxClientDC dc( m_gridWin );
5292 PrepareDC( dc );
5293 dc.SetLogicalFunction( wxINVERT );
5294 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5295 HideCellEditControl();
a5777624 5296 SaveEditControlValue();
6d004f67 5297
7c1cb261 5298 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 5299 SetColSize( m_dragRowOrCol,
43947979
VZ
5300 wxMax( m_dragLastPos - colLeft,
5301 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
5302
5303 if ( !GetBatchCount() )
5304 {
5305 // Only needed to get the correct rect.x:
5306 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5307 rect.y = 0;
5308 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5309 rect.width = cw - rect.x;
5310 rect.height = m_colLabelHeight;
5311 m_colLabelWin->Refresh( TRUE, &rect );
5312 rect.height = ch;
5313 m_gridWin->Refresh( FALSE, &rect );
790cc417 5314 }
e2b42eeb 5315
6d004f67 5316 ShowCellEditControl();
f85afd4e 5317 }
f85afd4e
MB
5318}
5319
5320
6d004f67 5321
2d66e025
MB
5322//
5323// ------ interaction with data model
5324//
5325bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 5326{
2d66e025 5327 switch ( msg.GetId() )
17732cec 5328 {
2d66e025
MB
5329 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5330 return GetModelValues();
17732cec 5331
2d66e025
MB
5332 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5333 return SetModelValues();
f85afd4e 5334
2d66e025
MB
5335 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5336 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5337 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5338 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5339 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5340 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5341 return Redimension( msg );
5342
5343 default:
5344 return FALSE;
f85afd4e 5345 }
2d66e025 5346}
f85afd4e 5347
f85afd4e 5348
f85afd4e 5349
2d66e025
MB
5350// The behaviour of this function depends on the grid table class
5351// Clear() function. For the default wxGridStringTable class the
5352// behavious is to replace all cell contents with wxEmptyString but
5353// not to change the number of rows or cols.
5354//
5355void wxGrid::ClearGrid()
5356{
5357 if ( m_table )
f85afd4e 5358 {
4cfa5de6
RD
5359 if (IsCellEditControlEnabled())
5360 DisableCellEditControl();
5361
2d66e025 5362 m_table->Clear();
1f1ce288 5363 if ( !GetBatchCount() ) m_gridWin->Refresh();
f85afd4e
MB
5364 }
5365}
5366
5367
2d66e025 5368bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5369{
2d66e025 5370 // TODO: something with updateLabels flag
8f177c8e 5371
2d66e025 5372 if ( !m_created )
f85afd4e 5373 {
bd3c6758 5374 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2d66e025
MB
5375 return FALSE;
5376 }
8f177c8e 5377
2d66e025
MB
5378 if ( m_table )
5379 {
b7fff980 5380 if (IsCellEditControlEnabled())
b54ba671 5381 DisableCellEditControl();
b7fff980 5382
f6bcfd97 5383 return m_table->InsertRows( pos, numRows );
f85afd4e 5384
2d66e025
MB
5385 // the table will have sent the results of the insert row
5386 // operation to this view object as a grid table message
f85afd4e 5387 }
f6bcfd97 5388 return FALSE;
f85afd4e
MB
5389}
5390
5391
2d66e025 5392bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5393{
2d66e025
MB
5394 // TODO: something with updateLabels flag
5395
5396 if ( !m_created )
f85afd4e 5397 {
bd3c6758 5398 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2d66e025
MB
5399 return FALSE;
5400 }
5401
f6bcfd97
BP
5402 return ( m_table && m_table->AppendRows( numRows ) );
5403 // the table will have sent the results of the append row
5404 // operation to this view object as a grid table message
f85afd4e
MB
5405}
5406
2d66e025
MB
5407
5408bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5409{
2d66e025
MB
5410 // TODO: something with updateLabels flag
5411
5412 if ( !m_created )
f85afd4e 5413 {
bd3c6758 5414 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2d66e025
MB
5415 return FALSE;
5416 }
5417
b7fff980 5418 if ( m_table )
2d66e025 5419 {
b7fff980 5420 if (IsCellEditControlEnabled())
b54ba671 5421 DisableCellEditControl();
f85afd4e 5422
f6bcfd97
BP
5423 return (m_table->DeleteRows( pos, numRows ));
5424 // the table will have sent the results of the delete row
5425 // operation to this view object as a grid table message
2d66e025 5426 }
b7fff980 5427 return FALSE;
2d66e025 5428}
f85afd4e 5429
f85afd4e 5430
2d66e025
MB
5431bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5432{
5433 // TODO: something with updateLabels flag
8f177c8e 5434
2d66e025
MB
5435 if ( !m_created )
5436 {
bd3c6758 5437 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2d66e025
MB
5438 return FALSE;
5439 }
f85afd4e 5440
2d66e025
MB
5441 if ( m_table )
5442 {
b7fff980 5443 if (IsCellEditControlEnabled())
b54ba671 5444 DisableCellEditControl();
b7fff980 5445
f6bcfd97 5446 return m_table->InsertCols( pos, numCols );
2d66e025
MB
5447 // the table will have sent the results of the insert col
5448 // operation to this view object as a grid table message
f85afd4e 5449 }
f6bcfd97 5450 return FALSE;
f85afd4e
MB
5451}
5452
2d66e025
MB
5453
5454bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 5455{
2d66e025
MB
5456 // TODO: something with updateLabels flag
5457
5458 if ( !m_created )
f85afd4e 5459 {
bd3c6758 5460 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2d66e025
MB
5461 return FALSE;
5462 }
f85afd4e 5463
f6bcfd97
BP
5464 return ( m_table && m_table->AppendCols( numCols ) );
5465 // the table will have sent the results of the append col
5466 // operation to this view object as a grid table message
f85afd4e
MB
5467}
5468
5469
2d66e025 5470bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 5471{
2d66e025 5472 // TODO: something with updateLabels flag
8f177c8e 5473
2d66e025 5474 if ( !m_created )
f85afd4e 5475 {
bd3c6758 5476 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2d66e025 5477 return FALSE;
f85afd4e
MB
5478 }
5479
b7fff980 5480 if ( m_table )
2d66e025 5481 {
b7fff980 5482 if (IsCellEditControlEnabled())
b54ba671 5483 DisableCellEditControl();
8f177c8e 5484
f6bcfd97
BP
5485 return ( m_table->DeleteCols( pos, numCols ) );
5486 // the table will have sent the results of the delete col
5487 // operation to this view object as a grid table message
f85afd4e 5488 }
b7fff980 5489 return FALSE;
f85afd4e
MB
5490}
5491
5492
2d66e025
MB
5493
5494//
5495// ----- event handlers
f85afd4e 5496//
8f177c8e 5497
2d66e025
MB
5498// Generate a grid event based on a mouse event and
5499// return the result of ProcessEvent()
5500//
fe7b9ed6 5501int wxGrid::SendEvent( const wxEventType type,
2d66e025
MB
5502 int row, int col,
5503 wxMouseEvent& mouseEv )
5504{
fe7b9ed6 5505 bool claimed;
1bcf0c7d 5506 bool vetoed= FALSE;
fe7b9ed6 5507
97a9929e
VZ
5508 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
5509 {
5510 int rowOrCol = (row == -1 ? col : row);
5511
5512 wxGridSizeEvent gridEvt( GetId(),
5513 type,
5514 this,
5515 rowOrCol,
5516 mouseEv.GetX() + GetRowLabelSize(),
5517 mouseEv.GetY() + GetColLabelSize(),
5518 mouseEv.ControlDown(),
5519 mouseEv.ShiftDown(),
5520 mouseEv.AltDown(),
5521 mouseEv.MetaDown() );
5522
5523 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5524 vetoed = !gridEvt.IsAllowed();
5525 }
fe7b9ed6 5526 else if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
5527 {
5528 // Right now, it should _never_ end up here!
5529 wxGridRangeSelectEvent gridEvt( GetId(),
5530 type,
5531 this,
5532 m_selectingTopLeft,
5533 m_selectingBottomRight,
5534 TRUE,
5535 mouseEv.ControlDown(),
5536 mouseEv.ShiftDown(),
5537 mouseEv.AltDown(),
5538 mouseEv.MetaDown() );
5539
5540 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5541 vetoed = !gridEvt.IsAllowed();
5542 }
fe7b9ed6 5543 else
97a9929e
VZ
5544 {
5545 wxGridEvent gridEvt( GetId(),
5546 type,
5547 this,
5548 row, col,
5549 mouseEv.GetX() + GetRowLabelSize(),
5550 mouseEv.GetY() + GetColLabelSize(),
5551 FALSE,
5552 mouseEv.ControlDown(),
5553 mouseEv.ShiftDown(),
5554 mouseEv.AltDown(),
5555 mouseEv.MetaDown() );
5556 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5557 vetoed = !gridEvt.IsAllowed();
5558 }
5559
5560 // A Veto'd event may not be `claimed' so test this first
5561 if (vetoed) return -1;
5562 return claimed ? 1 : 0;
f85afd4e
MB
5563}
5564
5565
2d66e025
MB
5566// Generate a grid event of specified type and return the result
5567// of ProcessEvent().
f85afd4e 5568//
fe7b9ed6 5569int wxGrid::SendEvent( const wxEventType type,
2d66e025 5570 int row, int col )
f85afd4e 5571{
fe7b9ed6 5572 bool claimed;
1bcf0c7d 5573 bool vetoed= FALSE;
fe7b9ed6 5574
b54ba671 5575 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 5576 {
2d66e025 5577 int rowOrCol = (row == -1 ? col : row);
f85afd4e 5578
2d66e025
MB
5579 wxGridSizeEvent gridEvt( GetId(),
5580 type,
5581 this,
5582 rowOrCol );
5583
fe7b9ed6
VZ
5584 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5585 vetoed = !gridEvt.IsAllowed();
2d66e025
MB
5586 }
5587 else
5588 {
5589 wxGridEvent gridEvt( GetId(),
5590 type,
5591 this,
5592 row, col );
8f177c8e 5593
fe7b9ed6
VZ
5594 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5595 vetoed = !gridEvt.IsAllowed();
5596 }
5597
97a9929e
VZ
5598 // A Veto'd event may not be `claimed' so test this first
5599 if (vetoed) return -1;
5600 return claimed ? 1 : 0;
f85afd4e
MB
5601}
5602
5603
2d66e025 5604void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 5605{
f6bcfd97 5606 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
8f177c8e 5607}
f85afd4e
MB
5608
5609
97a9929e 5610void wxGrid::OnSize( wxSizeEvent& event )
f85afd4e 5611{
97a9929e 5612 // position the child windows
7807d81c 5613 CalcWindowSizes();
97a9929e
VZ
5614
5615 // don't call CalcDimensions() from here, the base class handles the size
5616 // changes itself
5617 event.Skip();
f85afd4e
MB
5618}
5619
f85afd4e 5620
2d66e025 5621void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 5622{
2d66e025 5623 if ( m_inOnKeyDown )
f85afd4e 5624 {
2d66e025
MB
5625 // shouldn't be here - we are going round in circles...
5626 //
07296f0b 5627 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
5628 }
5629
2d66e025 5630 m_inOnKeyDown = TRUE;
f85afd4e 5631
2d66e025
MB
5632 // propagate the event up and see if it gets processed
5633 //
5634 wxWindow *parent = GetParent();
5635 wxKeyEvent keyEvt( event );
5636 keyEvt.SetEventObject( parent );
5637
5638 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 5639 {
9b4aede2 5640
2d66e025
MB
5641 // try local handlers
5642 //
5643 switch ( event.KeyCode() )
5644 {
5645 case WXK_UP:
5646 if ( event.ControlDown() )
5647 {
5c8fc7c1 5648 MoveCursorUpBlock( event.ShiftDown() );
2d66e025
MB
5649 }
5650 else
5651 {
5c8fc7c1 5652 MoveCursorUp( event.ShiftDown() );
2d66e025
MB
5653 }
5654 break;
f85afd4e 5655
2d66e025
MB
5656 case WXK_DOWN:
5657 if ( event.ControlDown() )
5658 {
5c8fc7c1 5659 MoveCursorDownBlock( event.ShiftDown() );
2d66e025
MB
5660 }
5661 else
5662 {
5c8fc7c1 5663 MoveCursorDown( event.ShiftDown() );
2d66e025
MB
5664 }
5665 break;
8f177c8e 5666
2d66e025
MB
5667 case WXK_LEFT:
5668 if ( event.ControlDown() )
5669 {
5c8fc7c1 5670 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025
MB
5671 }
5672 else
5673 {
5c8fc7c1 5674 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
5675 }
5676 break;
5677
5678 case WXK_RIGHT:
5679 if ( event.ControlDown() )
5680 {
5c8fc7c1 5681 MoveCursorRightBlock( event.ShiftDown() );
2d66e025
MB
5682 }
5683 else
5684 {
5c8fc7c1 5685 MoveCursorRight( event.ShiftDown() );
2d66e025
MB
5686 }
5687 break;
b99be8fb 5688
2d66e025 5689 case WXK_RETURN:
a4f7bf58 5690 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
5691 if ( event.ControlDown() )
5692 {
5693 event.Skip(); // to let the edit control have the return
5694 }
5695 else
5696 {
f6bcfd97
BP
5697 if ( GetGridCursorRow() < GetNumberRows()-1 )
5698 {
5699 MoveCursorDown( event.ShiftDown() );
5700 }
5701 else
5702 {
5703 // at the bottom of a column
5704 HideCellEditControl();
5705 SaveEditControlValue();
5706 }
58dd5b3b 5707 }
2d66e025
MB
5708 break;
5709
5c8fc7c1 5710 case WXK_ESCAPE:
e32352cf 5711 ClearSelection();
5c8fc7c1
SN
5712 break;
5713
2c9a89e0
RD
5714 case WXK_TAB:
5715 if (event.ShiftDown())
f6bcfd97
BP
5716 {
5717 if ( GetGridCursorCol() > 0 )
5718 {
5719 MoveCursorLeft( FALSE );
5720 }
5721 else
5722 {
5723 // at left of grid
5724 HideCellEditControl();
5725 SaveEditControlValue();
5726 }
5727 }
2c9a89e0 5728 else
f6bcfd97
BP
5729 {
5730 if ( GetGridCursorCol() < GetNumberCols()-1 )
5731 {
5732 MoveCursorRight( FALSE );
5733 }
5734 else
5735 {
5736 // at right of grid
5737 HideCellEditControl();
5738 SaveEditControlValue();
5739 }
5740 }
2c9a89e0
RD
5741 break;
5742
2d66e025
MB
5743 case WXK_HOME:
5744 if ( event.ControlDown() )
5745 {
5746 MakeCellVisible( 0, 0 );
5747 SetCurrentCell( 0, 0 );
5748 }
5749 else
5750 {
5751 event.Skip();
5752 }
5753 break;
5754
5755 case WXK_END:
5756 if ( event.ControlDown() )
5757 {
5758 MakeCellVisible( m_numRows-1, m_numCols-1 );
5759 SetCurrentCell( m_numRows-1, m_numCols-1 );
5760 }
5761 else
5762 {
5763 event.Skip();
5764 }
5765 break;
5766
5767 case WXK_PRIOR:
5768 MovePageUp();
5769 break;
5770
5771 case WXK_NEXT:
5772 MovePageDown();
5773 break;
5774
07296f0b 5775 case WXK_SPACE:
aa5e1f75
SN
5776 if ( event.ControlDown() )
5777 {
5778 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
5779 m_currentCellCoords.GetCol(),
5780 event.ControlDown(),
5781 event.ShiftDown(),
5782 event.AltDown(),
5783 event.MetaDown() );
5784 break;
5785 }
07296f0b
RD
5786 if ( !IsEditable() )
5787 {
5c8fc7c1 5788 MoveCursorRight( FALSE );
07296f0b
RD
5789 break;
5790 }
5791 // Otherwise fall through to default
5792
2d66e025 5793 default:
f6bcfd97
BP
5794 // is it possible to edit the current cell at all?
5795 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
b54ba671 5796 {
f6bcfd97 5797 // yes, now check whether the cells editor accepts the key
f2d76237
RD
5798 int row = m_currentCellCoords.GetRow();
5799 int col = m_currentCellCoords.GetCol();
5800 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f 5801 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
f6bcfd97
BP
5802
5803 // <F2> is special and will always start editing, for
5804 // other keys - ask the editor itself
5805 if ( (event.KeyCode() == WXK_F2 && !event.HasModifiers())
5806 || editor->IsAcceptedKey(event) )
5807 {
5808 EnableCellEditControl();
2f159c58
VZ
5809
5810 // the editor could be not shown for a variety of
5811 // reasons (i.e. blocked by the app or whatever), so
5812 // check if it really was created
5813 if ( m_cellEditCtrlEnabled )
5814 {
5815 editor->StartingKey(event);
5816 }
f6bcfd97
BP
5817 }
5818 else
5819 {
5820 event.Skip();
5821 }
5822
0b190b0f 5823 editor->DecRef();
2c9a89e0
RD
5824 attr->DecRef();
5825 }
b3a7510d
VZ
5826 else
5827 {
b94ae1ea
VZ
5828 // let others process char events with modifiers or all
5829 // char events for readonly cells
b3a7510d
VZ
5830 event.Skip();
5831 }
025562fe 5832 break;
2d66e025 5833 }
f85afd4e
MB
5834 }
5835
2d66e025 5836 m_inOnKeyDown = FALSE;
f85afd4e
MB
5837}
5838
f6bcfd97
BP
5839void wxGrid::OnKeyUp( wxKeyEvent& event )
5840{
5841 // try local handlers
5842 //
5843 if ( event.KeyCode() == WXK_SHIFT )
5844 {
5845 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5846 m_selectingBottomRight != wxGridNoCellCoords )
5847 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5848 m_selectingTopLeft.GetCol(),
5849 m_selectingBottomRight.GetRow(),
5850 m_selectingBottomRight.GetCol(),
5851 event.ControlDown(),
5852 TRUE,
5853 event.AltDown(),
5854 event.MetaDown() );
5855 m_selectingTopLeft = wxGridNoCellCoords;
5856 m_selectingBottomRight = wxGridNoCellCoords;
5857 m_selectingKeyboard = wxGridNoCellCoords;
5858 }
5859}
5860
2796cce3 5861void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
5862{
5863}
07296f0b 5864
2d66e025 5865void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 5866{
f6bcfd97
BP
5867 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
5868 {
5869 // the event has been intercepted - do nothing
5870 return;
5871 }
5872
5873 wxClientDC dc(m_gridWin);
5874 PrepareDC(dc);
5875
2a7750d9 5876 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 5877 {
2d66e025 5878 HideCellEditControl();
b54ba671 5879 DisableCellEditControl();
07296f0b 5880
3ca6a5f0 5881 if ( IsVisible( m_currentCellCoords, FALSE ) )
f6bcfd97
BP
5882 {
5883 wxRect r;
d10f4bf9 5884 r = BlockToDeviceRect(m_currentCellCoords, coords);
f6bcfd97
BP
5885 if ( !m_gridLinesEnabled )
5886 {
5887 r.x--;
5888 r.y--;
5889 r.width++;
5890 r.height++;
5891 }
d1c0b4f9 5892
d10f4bf9 5893 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 5894
f6bcfd97
BP
5895 // Otherwise refresh redraws the highlight!
5896 m_currentCellCoords = coords;
275c4ae4 5897
d10f4bf9 5898 DrawGridCellArea(dc,cells);
f6bcfd97
BP
5899 DrawAllGridLines( dc, r );
5900 }
66242c80 5901 }
8f177c8e 5902
2d66e025
MB
5903 m_currentCellCoords = coords;
5904
2a7750d9
MB
5905 wxGridCellAttr* attr = GetCellAttr(coords);
5906 DrawCellHighlight(dc, attr);
5907 attr->DecRef();
66242c80
MB
5908}
5909
2d66e025 5910
c9097836
MB
5911void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
5912{
5913 int temp;
5914 wxGridCellCoords updateTopLeft, updateBottomRight;
5915
5916 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
5917 {
5918 leftCol = 0;
5919 rightCol = GetNumberCols() - 1;
5920 }
5921 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
5922 {
5923 topRow = 0;
5924 bottomRow = GetNumberRows() - 1;
5925 }
5926 if ( topRow > bottomRow )
5927 {
5928 temp = topRow;
5929 topRow = bottomRow;
5930 bottomRow = temp;
5931 }
5932
5933 if ( leftCol > rightCol )
5934 {
5935 temp = leftCol;
5936 leftCol = rightCol;
5937 rightCol = temp;
5938 }
5939
5940 updateTopLeft = wxGridCellCoords( topRow, leftCol );
5941 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
5942
5943 if ( m_selectingTopLeft != updateTopLeft ||
5944 m_selectingBottomRight != updateBottomRight )
5945 {
5946 // Compute two optimal update rectangles:
5947 // Either one rectangle is a real subset of the
5948 // other, or they are (almost) disjoint!
5949 wxRect rect[4];
5950 bool need_refresh[4];
5951 need_refresh[0] =
5952 need_refresh[1] =
5953 need_refresh[2] =
5954 need_refresh[3] = FALSE;
5955 int i;
5956
5957 // Store intermediate values
5958 wxCoord oldLeft = m_selectingTopLeft.GetCol();
5959 wxCoord oldTop = m_selectingTopLeft.GetRow();
5960 wxCoord oldRight = m_selectingBottomRight.GetCol();
5961 wxCoord oldBottom = m_selectingBottomRight.GetRow();
5962
5963 // Determine the outer/inner coordinates.
5964 if (oldLeft > leftCol)
5965 {
5966 temp = oldLeft;
5967 oldLeft = leftCol;
5968 leftCol = temp;
5969 }
5970 if (oldTop > topRow )
5971 {
5972 temp = oldTop;
5973 oldTop = topRow;
5974 topRow = temp;
5975 }
5976 if (oldRight < rightCol )
5977 {
5978 temp = oldRight;
5979 oldRight = rightCol;
5980 rightCol = temp;
5981 }
5982 if (oldBottom < bottomRow)
5983 {
5984 temp = oldBottom;
5985 oldBottom = bottomRow;
5986 bottomRow = temp;
5987 }
5988
5989 // Now, either the stuff marked old is the outer
5990 // rectangle or we don't have a situation where one
5991 // is contained in the other.
5992
5993 if ( oldLeft < leftCol )
5994 {
5995 need_refresh[0] = TRUE;
5996 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
5997 oldLeft ),
5998 wxGridCellCoords ( oldBottom,
5999 leftCol - 1 ) );
6000 }
6001
6002 if ( oldTop < topRow )
6003 {
6004 need_refresh[1] = TRUE;
6005 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6006 leftCol ),
6007 wxGridCellCoords ( topRow - 1,
6008 rightCol ) );
6009 }
6010
6011 if ( oldRight > rightCol )
6012 {
6013 need_refresh[2] = TRUE;
6014 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6015 rightCol + 1 ),
6016 wxGridCellCoords ( oldBottom,
6017 oldRight ) );
6018 }
6019
6020 if ( oldBottom > bottomRow )
6021 {
6022 need_refresh[3] = TRUE;
6023 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
6024 leftCol ),
6025 wxGridCellCoords ( oldBottom,
6026 rightCol ) );
6027 }
6028
6029
6030 // Change Selection
6031 m_selectingTopLeft = updateTopLeft;
6032 m_selectingBottomRight = updateBottomRight;
6033
6034 // various Refresh() calls
6035 for (i = 0; i < 4; i++ )
6036 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6037 m_gridWin->Refresh( FALSE, &(rect[i]) );
6038 }
6039
6040 // never generate an event as it will be generated from
6041 // wxGridSelection::SelectBlock!
6042 // (old comment from when this was the body of SelectBlock)
6043}
6044
2d66e025
MB
6045//
6046// ------ functions to get/send data (see also public functions)
6047//
6048
6049bool wxGrid::GetModelValues()
66242c80 6050{
2d66e025 6051 if ( m_table )
66242c80 6052 {
2d66e025 6053 // all we need to do is repaint the grid
66242c80 6054 //
2d66e025 6055 m_gridWin->Refresh();
66242c80
MB
6056 return TRUE;
6057 }
8f177c8e 6058
66242c80
MB
6059 return FALSE;
6060}
6061
2d66e025
MB
6062
6063bool wxGrid::SetModelValues()
f85afd4e 6064{
2d66e025 6065 int row, col;
8f177c8e 6066
2d66e025
MB
6067 if ( m_table )
6068 {
6069 for ( row = 0; row < m_numRows; row++ )
f85afd4e 6070 {
2d66e025 6071 for ( col = 0; col < m_numCols; col++ )
f85afd4e 6072 {
2d66e025 6073 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
6074 }
6075 }
8f177c8e 6076
f85afd4e
MB
6077 return TRUE;
6078 }
6079
6080 return FALSE;
6081}
6082
2d66e025
MB
6083
6084
6085// Note - this function only draws cells that are in the list of
6086// exposed cells (usually set from the update region by
6087// CalcExposedCells)
6088//
d10f4bf9 6089void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 6090{
2d66e025 6091 if ( !m_numRows || !m_numCols ) return;
60ff3b99 6092
2d66e025 6093 size_t i;
d10f4bf9 6094 size_t numCells = cells.GetCount();
60ff3b99 6095
2d66e025 6096 for ( i = 0; i < numCells; i++ )
f85afd4e 6097 {
d10f4bf9 6098 DrawCell( dc, cells[i] );
2d66e025
MB
6099 }
6100}
8f177c8e 6101
8f177c8e 6102
7c8a8ad5
MB
6103void wxGrid::DrawGridSpace( wxDC& dc )
6104{
f6bcfd97
BP
6105 int cw, ch;
6106 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 6107
f6bcfd97
BP
6108 int right, bottom;
6109 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 6110
f6bcfd97
BP
6111 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6112 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
7c8a8ad5 6113
f6bcfd97
BP
6114 if ( right > rightCol || bottom > bottomRow )
6115 {
6116 int left, top;
6117 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 6118
f6bcfd97
BP
6119 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6120 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 6121
f6bcfd97
BP
6122 if ( right > rightCol )
6123 {
6124 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6125 }
6126
6127 if ( bottom > bottomRow )
6128 {
6129 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6130 }
6131 }
7c8a8ad5
MB
6132}
6133
6134
2d66e025
MB
6135void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6136{
ab79958a
VZ
6137 int row = coords.GetRow();
6138 int col = coords.GetCol();
60ff3b99 6139
7c1cb261 6140 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
6141 return;
6142
6143 // we draw the cell border ourselves
9496deb5 6144#if !WXGRID_DRAW_LINES
2d66e025
MB
6145 if ( m_gridLinesEnabled )
6146 DrawCellBorder( dc, coords );
796df70a 6147#endif
f85afd4e 6148
189d0213
VZ
6149 wxGridCellAttr* attr = GetCellAttr(row, col);
6150
6151 bool isCurrent = coords == m_currentCellCoords;
508011ce 6152
f6bcfd97 6153 wxRect rect = CellToRect( row, col );
f85afd4e 6154
189d0213 6155 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
6156 // Note: However, only if it is really _shown_, i.e. not hidden!
6157 if ( isCurrent && IsCellEditControlShown() )
189d0213 6158 {
0b190b0f
VZ
6159 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6160 editor->PaintBackground(rect, attr);
6161 editor->DecRef();
189d0213
VZ
6162 }
6163 else
6164 {
6165 // but all the rest is drawn by the cell renderer and hence may be
6166 // customized
0b190b0f
VZ
6167 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
6168 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
6169 renderer->DecRef();
189d0213 6170 }
07296f0b 6171
283b7808
VZ
6172 attr->DecRef();
6173}
07296f0b 6174
283b7808 6175void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
6176{
6177 int row = m_currentCellCoords.GetRow();
6178 int col = m_currentCellCoords.GetCol();
6179
7c1cb261 6180 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
6181 return;
6182
f6bcfd97 6183 wxRect rect = CellToRect(row, col);
07296f0b 6184
b3a7510d
VZ
6185 // hmmm... what could we do here to show that the cell is disabled?
6186 // for now, I just draw a thinner border than for the other ones, but
6187 // it doesn't look really good
b3a7510d 6188
d2520c85
RD
6189 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
6190
6191 if (penWidth > 0) {
6192
6193 // The center of th drawn line is where the position/width/height of
6194 // the rectangle is actually at, (on wxMSW atr least,) so we will
6195 // reduce the size of the rectangle to compensate for the thickness of
6196 // the line. If this is too strange on non wxMSW platforms then
6197 // please #ifdef this appropriately.
6198 rect.x += penWidth/2;
6199 rect.y += penWidth/2;
6200 rect.width -= penWidth-1;
6201 rect.height -= penWidth-1;
6202
6203
6204 // Now draw the rectangle
6205 dc.SetPen(wxPen(m_cellHighlightColour, penWidth, wxSOLID));
6206 dc.SetBrush(*wxTRANSPARENT_BRUSH);
6207 dc.DrawRectangle(rect);
6208 }
07296f0b 6209
283b7808 6210#if 0
b3a7510d 6211 // VZ: my experiments with 3d borders...
283b7808 6212
b3a7510d 6213 // how to properly set colours for arbitrary bg?
283b7808
VZ
6214 wxCoord x1 = rect.x,
6215 y1 = rect.y,
00cf1208
RR
6216 x2 = rect.x + rect.width -1,
6217 y2 = rect.y + rect.height -1;
283b7808
VZ
6218
6219 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
6220 dc.DrawLine(x1, y1, x2, y1);
6221 dc.DrawLine(x1, y1, x1, y2);
283b7808 6222
283b7808 6223 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
00cf1208 6224 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
283b7808
VZ
6225
6226 dc.SetPen(*wxBLACK_PEN);
6227 dc.DrawLine(x1, y2, x2, y2);
00cf1208 6228 dc.DrawLine(x2, y1, x2, y2+1);
b3a7510d 6229#endif // 0
2d66e025 6230}
f85afd4e 6231
f2d76237 6232
2d66e025
MB
6233void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
6234{
2d66e025
MB
6235 int row = coords.GetRow();
6236 int col = coords.GetCol();
7c1cb261
VZ
6237 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6238 return;
6239
6240 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
60ff3b99 6241
2d66e025
MB
6242 // right hand border
6243 //
7c1cb261
VZ
6244 dc.DrawLine( GetColRight(col), GetRowTop(row),
6245 GetColRight(col), GetRowBottom(row) );
2d66e025
MB
6246
6247 // bottom border
6248 //
7c1cb261
VZ
6249 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
6250 GetColRight(col), GetRowBottom(row) );
f85afd4e
MB
6251}
6252
d10f4bf9 6253void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
b3a7510d 6254{
2a7750d9
MB
6255 // This if block was previously in wxGrid::OnPaint but that doesn't
6256 // seem to get called under wxGTK - MB
6257 //
6258 if ( m_currentCellCoords == wxGridNoCellCoords &&
6259 m_numRows && m_numCols )
6260 {
6261 m_currentCellCoords.Set(0, 0);
6262 }
6263
f6bcfd97 6264 if ( IsCellEditControlShown() )
99306db2
VZ
6265 {
6266 // don't show highlight when the edit control is shown
6267 return;
6268 }
6269
b3a7510d
VZ
6270 // if the active cell was repainted, repaint its highlight too because it
6271 // might have been damaged by the grid lines
d10f4bf9 6272 size_t count = cells.GetCount();
b3a7510d
VZ
6273 for ( size_t n = 0; n < count; n++ )
6274 {
d10f4bf9 6275 if ( cells[n] == m_currentCellCoords )
b3a7510d
VZ
6276 {
6277 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
6278 DrawCellHighlight(dc, attr);
6279 attr->DecRef();
6280
6281 break;
6282 }
6283 }
6284}
2d66e025 6285
2d66e025
MB
6286// TODO: remove this ???
6287// This is used to redraw all grid lines e.g. when the grid line colour
6288// has been changed
6289//
33ac7e6f 6290void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 6291{
60ff3b99
VZ
6292 if ( !m_gridLinesEnabled ||
6293 !m_numRows ||
2d66e025 6294 !m_numCols ) return;
f85afd4e 6295
2d66e025 6296 int top, bottom, left, right;
796df70a 6297
f6bcfd97 6298#if 0 //#ifndef __WXGTK__
508011ce
VZ
6299 if (reg.IsEmpty())
6300 {
796df70a
SN
6301 int cw, ch;
6302 m_gridWin->GetClientSize(&cw, &ch);
6303
6304 // virtual coords of visible area
6305 //
6306 CalcUnscrolledPosition( 0, 0, &left, &top );
6307 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6308 }
508011ce
VZ
6309 else
6310 {
796df70a
SN
6311 wxCoord x, y, w, h;
6312 reg.GetBox(x, y, w, h);
6313 CalcUnscrolledPosition( x, y, &left, &top );
6314 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
6315 }
8e217128
RR
6316#else
6317 int cw, ch;
6318 m_gridWin->GetClientSize(&cw, &ch);
6319 CalcUnscrolledPosition( 0, 0, &left, &top );
6320 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6321#endif
f85afd4e 6322
9496deb5
MB
6323 // avoid drawing grid lines past the last row and col
6324 //
7c1cb261
VZ
6325 right = wxMin( right, GetColRight(m_numCols - 1) );
6326 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 6327
2d66e025 6328 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
f85afd4e 6329
2d66e025 6330 // horizontal grid lines
f85afd4e 6331 //
60ff3b99 6332 int i;
33188aa4 6333 for ( i = internalYToRow(top); i < m_numRows; i++ )
f85afd4e 6334 {
6d55126d 6335 int bot = GetRowBottom(i) - 1;
7c1cb261 6336
6d55126d 6337 if ( bot > bottom )
2d66e025
MB
6338 {
6339 break;
6340 }
7c1cb261 6341
6d55126d 6342 if ( bot >= top )
2d66e025 6343 {
6d55126d 6344 dc.DrawLine( left, bot, right, bot );
2d66e025 6345 }
f85afd4e
MB
6346 }
6347
f85afd4e 6348
2d66e025
MB
6349 // vertical grid lines
6350 //
33188aa4 6351 for ( i = internalXToCol(left); i < m_numCols; i++ )
f85afd4e 6352 {
7c1cb261
VZ
6353 int colRight = GetColRight(i) - 1;
6354 if ( colRight > right )
2d66e025
MB
6355 {
6356 break;
6357 }
7c1cb261
VZ
6358
6359 if ( colRight >= left )
2d66e025 6360 {
7c1cb261 6361 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
6362 }
6363 }
6364}
f85afd4e 6365
8f177c8e 6366
d10f4bf9 6367void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
2d66e025 6368{
f6bcfd97 6369 if ( !m_numRows ) return;
60ff3b99 6370
2d66e025 6371 size_t i;
d10f4bf9 6372 size_t numLabels = rows.GetCount();
60ff3b99 6373
2d66e025
MB
6374 for ( i = 0; i < numLabels; i++ )
6375 {
d10f4bf9 6376 DrawRowLabel( dc, rows[i] );
60ff3b99 6377 }
f85afd4e
MB
6378}
6379
6380
2d66e025 6381void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 6382{
7c1cb261
VZ
6383 if ( GetRowHeight(row) <= 0 )
6384 return;
60ff3b99 6385
7c1cb261
VZ
6386 int rowTop = GetRowTop(row),
6387 rowBottom = GetRowBottom(row) - 1;
b99be8fb 6388
2d66e025 6389 dc.SetPen( *wxBLACK_PEN );
b0d6ff2f 6390 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7c1cb261 6391 m_rowLabelWidth-1, rowBottom );
b99be8fb 6392
7c1cb261 6393 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
b99be8fb 6394
2d66e025 6395 dc.SetPen( *wxWHITE_PEN );
7c1cb261 6396 dc.DrawLine( 0, rowTop, 0, rowBottom );
b0d6ff2f 6397 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
60ff3b99 6398
f85afd4e 6399 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
6400 dc.SetTextForeground( GetLabelTextColour() );
6401 dc.SetFont( GetLabelFont() );
8f177c8e 6402
f85afd4e 6403 int hAlign, vAlign;
2d66e025 6404 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 6405
2d66e025
MB
6406 wxRect rect;
6407 rect.SetX( 2 );
7c1cb261 6408 rect.SetY( GetRowTop(row) + 2 );
2d66e025 6409 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 6410 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 6411 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
6412}
6413
6414
d10f4bf9 6415void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 6416{
f6bcfd97 6417 if ( !m_numCols ) return;
60ff3b99 6418
2d66e025 6419 size_t i;
d10f4bf9 6420 size_t numLabels = cols.GetCount();
60ff3b99 6421
2d66e025 6422 for ( i = 0; i < numLabels; i++ )
f85afd4e 6423 {
d10f4bf9 6424 DrawColLabel( dc, cols[i] );
60ff3b99 6425 }
f85afd4e
MB
6426}
6427
6428
2d66e025 6429void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 6430{
7c1cb261
VZ
6431 if ( GetColWidth(col) <= 0 )
6432 return;
60ff3b99 6433
7c1cb261
VZ
6434 int colLeft = GetColLeft(col),
6435 colRight = GetColRight(col) - 1;
b99be8fb 6436
2d66e025 6437 dc.SetPen( *wxBLACK_PEN );
7c1cb261
VZ
6438 dc.DrawLine( colRight, 0,
6439 colRight, m_colLabelHeight-1 );
b99be8fb 6440
b0d6ff2f 6441 dc.DrawLine( colLeft, m_colLabelHeight-1,
7c1cb261 6442 colRight, m_colLabelHeight-1 );
b99be8fb 6443
f85afd4e 6444 dc.SetPen( *wxWHITE_PEN );
b0d6ff2f 6445 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
7c1cb261 6446 dc.DrawLine( colLeft, 0, colRight, 0 );
f85afd4e 6447
b0d6ff2f
MB
6448 dc.SetBackgroundMode( wxTRANSPARENT );
6449 dc.SetTextForeground( GetLabelTextColour() );
6450 dc.SetFont( GetLabelFont() );
8f177c8e 6451
f85afd4e 6452 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
6453 dc.SetTextForeground( GetLabelTextColour() );
6454 dc.SetFont( GetLabelFont() );
8f177c8e 6455
f85afd4e 6456 int hAlign, vAlign;
2d66e025 6457 GetColLabelAlignment( &hAlign, &vAlign );
60ff3b99 6458
2d66e025 6459 wxRect rect;
7c1cb261 6460 rect.SetX( colLeft + 2 );
2d66e025 6461 rect.SetY( 2 );
7c1cb261 6462 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 6463 rect.SetHeight( m_colLabelHeight - 4 );
60ff3b99 6464 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
f85afd4e
MB
6465}
6466
2d66e025
MB
6467void wxGrid::DrawTextRectangle( wxDC& dc,
6468 const wxString& value,
6469 const wxRect& rect,
6470 int horizAlign,
6471 int vertAlign )
f85afd4e 6472{
2d66e025 6473 wxArrayString lines;
f85afd4e 6474
2d66e025
MB
6475 dc.SetClippingRegion( rect );
6476 StringToLines( value, lines );
d10f4bf9
VZ
6477
6478
6479 //Forward to new API.
6480 DrawTextRectangle( dc,
6481 lines,
6482 rect,
6483 horizAlign,
6484 vertAlign );
6485
6486}
6487
6488void wxGrid::DrawTextRectangle( wxDC& dc,
6489 const wxArrayString& lines,
6490 const wxRect& rect,
6491 int horizAlign,
6492 int vertAlign )
6493{
6494 long textWidth, textHeight;
6495 long lineWidth, lineHeight;
6496
2d66e025
MB
6497 if ( lines.GetCount() )
6498 {
6499 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
6500 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
f85afd4e 6501
2d66e025
MB
6502 float x, y;
6503 switch ( horizAlign )
6504 {
4c7277db 6505 case wxALIGN_RIGHT:
2d66e025
MB
6506 x = rect.x + (rect.width - textWidth - 1);
6507 break;
f85afd4e 6508
4c7277db 6509 case wxALIGN_CENTRE:
2d66e025
MB
6510 x = rect.x + ((rect.width - textWidth)/2);
6511 break;
f85afd4e 6512
4c7277db 6513 case wxALIGN_LEFT:
2d66e025
MB
6514 default:
6515 x = rect.x + 1;
6516 break;
6517 }
f85afd4e 6518
2d66e025
MB
6519 switch ( vertAlign )
6520 {
4c7277db 6521 case wxALIGN_BOTTOM:
2d66e025
MB
6522 y = rect.y + (rect.height - textHeight - 1);
6523 break;
f85afd4e 6524
4c7277db 6525 case wxALIGN_CENTRE:
8f177c8e 6526 y = rect.y + ((rect.height - textHeight)/2);
f85afd4e
MB
6527 break;
6528
4c7277db 6529 case wxALIGN_TOP:
f85afd4e 6530 default:
8f177c8e 6531 y = rect.y + 1;
f85afd4e
MB
6532 break;
6533 }
6534
b540eb2b 6535 for ( size_t i = 0; i < lines.GetCount(); i++ )
f85afd4e 6536 {
4a64bee4 6537 dc.DrawText( lines[i], (int)x, (int)y );
f85afd4e
MB
6538 y += lineHeight;
6539 }
6540 }
8f177c8e 6541
f85afd4e 6542 dc.DestroyClippingRegion();
f85afd4e
MB
6543}
6544
6545
6546// Split multi line text up into an array of strings. Any existing
6547// contents of the string array are preserved.
6548//
6549void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
6550{
f85afd4e
MB
6551 int startPos = 0;
6552 int pos;
6d004f67 6553 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 6554 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 6555
2433bb2e 6556 while ( startPos < (int)tVal.Length() )
f85afd4e 6557 {
2433bb2e 6558 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
6559 if ( pos < 0 )
6560 {
6561 break;
6562 }
6563 else if ( pos == 0 )
6564 {
6565 lines.Add( wxEmptyString );
6566 }
6567 else
6568 {
2433bb2e 6569 lines.Add( value.Mid(startPos, pos) );
f85afd4e
MB
6570 }
6571 startPos += pos+1;
6572 }
b540eb2b 6573 if ( startPos < (int)value.Length() )
f85afd4e
MB
6574 {
6575 lines.Add( value.Mid( startPos ) );
6576 }
6577}
6578
6579
6580void wxGrid::GetTextBoxSize( wxDC& dc,
d10f4bf9 6581 const wxArrayString& lines,
f85afd4e
MB
6582 long *width, long *height )
6583{
6584 long w = 0;
6585 long h = 0;
6586 long lineW, lineH;
6587
b540eb2b 6588 size_t i;
f85afd4e
MB
6589 for ( i = 0; i < lines.GetCount(); i++ )
6590 {
6591 dc.GetTextExtent( lines[i], &lineW, &lineH );
6592 w = wxMax( w, lineW );
6593 h += lineH;
6594 }
6595
6596 *width = w;
6597 *height = h;
6598}
6599
f6bcfd97
BP
6600//
6601// ------ Batch processing.
6602//
6603void wxGrid::EndBatch()
6604{
6605 if ( m_batchCount > 0 )
6606 {
6607 m_batchCount--;
6608 if ( !m_batchCount )
6609 {
6610 CalcDimensions();
6611 m_rowLabelWin->Refresh();
6612 m_colLabelWin->Refresh();
6613 m_cornerLabelWin->Refresh();
6614 m_gridWin->Refresh();
6615 }
6616 }
6617}
f85afd4e 6618
d8232393
MB
6619// Use this, rather than wxWindow::Refresh(), to force an immediate
6620// repainting of the grid. Has no effect if you are already inside a
6621// BeginBatch / EndBatch block.
6622//
6623void wxGrid::ForceRefresh()
6624{
6625 BeginBatch();
6626 EndBatch();
6627}
6628
6629
f85afd4e 6630//
2d66e025 6631// ------ Edit control functions
f85afd4e
MB
6632//
6633
2d66e025
MB
6634
6635void wxGrid::EnableEditing( bool edit )
f85afd4e 6636{
2d66e025
MB
6637 // TODO: improve this ?
6638 //
6639 if ( edit != m_editable )
f85afd4e 6640 {
632efa47 6641 if(!edit) EnableCellEditControl(edit);
2d66e025 6642 m_editable = edit;
f85afd4e 6643 }
f85afd4e
MB
6644}
6645
6646
2d66e025 6647void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 6648{
2c9a89e0
RD
6649 if (! m_editable)
6650 return;
6651
dcfe4c3d
SN
6652 if ( m_currentCellCoords == wxGridNoCellCoords )
6653 SetCurrentCell( 0, 0 );
60ff3b99 6654
2c9a89e0
RD
6655 if ( enable != m_cellEditCtrlEnabled )
6656 {
dcfe4c3d 6657 if ( enable )
f85afd4e 6658 {
97a9929e
VZ
6659 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
6660 return;
fe7b9ed6 6661
97a9929e 6662 // this should be checked by the caller!
b54ba671
VZ
6663 wxASSERT_MSG( CanEnableCellControl(),
6664 _T("can't enable editing for this cell!") );
6665
6666 // do it before ShowCellEditControl()
dcfe4c3d 6667 m_cellEditCtrlEnabled = enable;
b54ba671 6668
2d66e025 6669 ShowCellEditControl();
2d66e025
MB
6670 }
6671 else
6672 {
97a9929e
VZ
6673 //FIXME:add veto support
6674 SendEvent( wxEVT_GRID_EDITOR_HIDDEN);
fe7b9ed6 6675
97a9929e 6676 HideCellEditControl();
2d66e025 6677 SaveEditControlValue();
b54ba671
VZ
6678
6679 // do it after HideCellEditControl()
dcfe4c3d 6680 m_cellEditCtrlEnabled = enable;
f85afd4e 6681 }
f85afd4e 6682 }
f85afd4e 6683}
f85afd4e 6684
b54ba671 6685bool wxGrid::IsCurrentCellReadOnly() const
283b7808 6686{
b54ba671
VZ
6687 // const_cast
6688 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
6689 bool readonly = attr->IsReadOnly();
6690 attr->DecRef();
283b7808 6691
b54ba671
VZ
6692 return readonly;
6693}
283b7808 6694
b54ba671
VZ
6695bool wxGrid::CanEnableCellControl() const
6696{
6697 return m_editable && !IsCurrentCellReadOnly();
6698}
6699
6700bool wxGrid::IsCellEditControlEnabled() const
6701{
6702 // the cell edit control might be disable for all cells or just for the
6703 // current one if it's read only
6704 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
283b7808
VZ
6705}
6706
f6bcfd97
BP
6707bool wxGrid::IsCellEditControlShown() const
6708{
6709 bool isShown = FALSE;
6710
6711 if ( m_cellEditCtrlEnabled )
6712 {
6713 int row = m_currentCellCoords.GetRow();
6714 int col = m_currentCellCoords.GetCol();
6715 wxGridCellAttr* attr = GetCellAttr(row, col);
6716 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
6717 attr->DecRef();
6718
6719 if ( editor )
6720 {
6721 if ( editor->IsCreated() )
6722 {
6723 isShown = editor->GetControl()->IsShown();
6724 }
6725
6726 editor->DecRef();
6727 }
6728 }
6729
6730 return isShown;
6731}
6732
2d66e025 6733void wxGrid::ShowCellEditControl()
f85afd4e 6734{
2d66e025 6735 if ( IsCellEditControlEnabled() )
f85afd4e 6736 {
2d66e025
MB
6737 if ( !IsVisible( m_currentCellCoords ) )
6738 {
3ca6a5f0 6739 m_cellEditCtrlEnabled = FALSE;
2d66e025
MB
6740 return;
6741 }
6742 else
6743 {
2c9a89e0
RD
6744 wxRect rect = CellToRect( m_currentCellCoords );
6745 int row = m_currentCellCoords.GetRow();
6746 int col = m_currentCellCoords.GetCol();
2d66e025
MB
6747
6748 // convert to scrolled coords
6749 //
99306db2 6750 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
508011ce 6751
99306db2
VZ
6752 // done in PaintBackground()
6753#if 0
6754 // erase the highlight and the cell contents because the editor
6755 // might not cover the entire cell
6756 wxClientDC dc( m_gridWin );
6757 PrepareDC( dc );
6758 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6759 dc.SetPen(*wxTRANSPARENT_PEN);
6760 dc.DrawRectangle(rect);
6761#endif // 0
d2fdd8d2 6762
99306db2 6763 // cell is shifted by one pixel
68c5a31c
SN
6764 // However, don't allow x or y to become negative
6765 // since the SetSize() method interprets that as
6766 // "don't change."
6767 if (rect.x > 0)
6768 rect.x--;
6769 if (rect.y > 0)
6770 rect.y--;
f0102d2a 6771
508011ce 6772 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6773 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
6774 if ( !editor->IsCreated() )
6775 {
2c9a89e0
RD
6776 editor->Create(m_gridWin, -1,
6777 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
6778
6779 wxGridEditorCreatedEvent evt(GetId(),
6780 wxEVT_GRID_EDITOR_CREATED,
6781 this,
6782 row,
6783 col,
6784 editor->GetControl());
6785 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
6786 }
6787
b0e282b3 6788 editor->Show( TRUE, attr );
0b190b0f 6789
2c9a89e0 6790 editor->SetSize( rect );
99306db2 6791
3da93aae 6792 editor->BeginEdit(row, col, this);
0b190b0f
VZ
6793
6794 editor->DecRef();
2c9a89e0
RD
6795 attr->DecRef();
6796 }
f85afd4e
MB
6797 }
6798}
6799
6800
2d66e025 6801void wxGrid::HideCellEditControl()
f85afd4e 6802{
2d66e025 6803 if ( IsCellEditControlEnabled() )
f85afd4e 6804 {
2c9a89e0
RD
6805 int row = m_currentCellCoords.GetRow();
6806 int col = m_currentCellCoords.GetCol();
6807
3da93aae 6808 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f
VZ
6809 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6810 editor->Show( FALSE );
6811 editor->DecRef();
2c9a89e0
RD
6812 attr->DecRef();
6813 m_gridWin->SetFocus();
f6bcfd97
BP
6814 wxRect rect( CellToRect( row, col ) );
6815 m_gridWin->Refresh( FALSE, &rect );
f85afd4e 6816 }
2d66e025 6817}
8f177c8e 6818
2d66e025 6819
2d66e025
MB
6820void wxGrid::SaveEditControlValue()
6821{
3da93aae
VZ
6822 if ( IsCellEditControlEnabled() )
6823 {
2c9a89e0
RD
6824 int row = m_currentCellCoords.GetRow();
6825 int col = m_currentCellCoords.GetCol();
8f177c8e 6826
fe7b9ed6
VZ
6827 wxString oldval = GetCellValue(row,col);
6828
3da93aae 6829 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6830 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 6831 bool changed = editor->EndEdit(row, col, this);
2d66e025 6832
0b190b0f 6833 editor->DecRef();
2c9a89e0 6834 attr->DecRef();
2d66e025 6835
3da93aae
VZ
6836 if (changed)
6837 {
fe7b9ed6 6838 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025 6839 m_currentCellCoords.GetRow(),
fe7b9ed6
VZ
6840 m_currentCellCoords.GetCol() ) < 0 ) {
6841
97a9929e
VZ
6842 // Event has been vetoed, set the data back.
6843 SetCellValue(row,col,oldval);
6844 }
2d66e025 6845 }
f85afd4e
MB
6846 }
6847}
6848
2d66e025
MB
6849
6850//
60ff3b99
VZ
6851// ------ Grid location functions
6852// Note that all of these functions work with the logical coordinates of
2d66e025
MB
6853// grid cells and labels so you will need to convert from device
6854// coordinates for mouse events etc.
6855//
6856
6857void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 6858{
58dd5b3b
MB
6859 int row = YToRow(y);
6860 int col = XToCol(x);
6861
6862 if ( row == -1 || col == -1 )
6863 {
6864 coords = wxGridNoCellCoords;
6865 }
6866 else
6867 {
6868 coords.Set( row, col );
6869 }
2d66e025 6870}
f85afd4e 6871
8f177c8e 6872
b1da8107
SN
6873// Internal Helper function for computing row or column from some
6874// (unscrolled) coordinate value, using either
6875// m_defaultRowHeight/m_defaultColWidth or binary search on array
6876// of m_rowBottoms/m_ColRights to speed up the search!
6877
6878static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
1af546bf 6879 const wxArrayInt& BorderArray, bool maxOnOverflow)
2d66e025 6880{
b1da8107
SN
6881 if (!defaultDist)
6882 defaultDist = 1;
1af546bf
VZ
6883 size_t i_max = coord / defaultDist,
6884 i_min = 0;
b1da8107
SN
6885 if (BorderArray.IsEmpty())
6886 {
6887 return i_max;
6888 }
8f177c8e 6889
b1da8107
SN
6890 if ( i_max >= BorderArray.GetCount())
6891 i_max = BorderArray.GetCount() - 1;
6892 else
f85afd4e 6893 {
b1da8107
SN
6894 if ( coord >= BorderArray[i_max])
6895 {
6896 i_min = i_max;
6897 i_max = coord / minDist;
6898 }
6899 if ( i_max >= BorderArray.GetCount())
6900 i_max = BorderArray.GetCount() - 1;
f85afd4e 6901 }
33188aa4 6902 if ( coord >= BorderArray[i_max])
1af546bf 6903 return maxOnOverflow ? (int)i_max : -1;
68c5a31c
SN
6904 if ( coord < BorderArray[0] )
6905 return 0;
2d66e025 6906
68c5a31c 6907 while ( i_max - i_min > 0 )
b1da8107
SN
6908 {
6909 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
33188aa4 6910 0, _T("wxGrid: internal error in CoordToRowOrCol"));
b1da8107 6911 if (coord >= BorderArray[ i_max - 1])
b1da8107 6912 return i_max;
b1da8107
SN
6913 else
6914 i_max--;
6915 int median = i_min + (i_max - i_min + 1) / 2;
6916 if (coord < BorderArray[median])
6917 i_max = median;
6918 else
6919 i_min = median;
6920 }
6921 return i_max;
f85afd4e
MB
6922}
6923
b1da8107 6924int wxGrid::YToRow( int y )
f85afd4e 6925{
b1da8107 6926 return CoordToRowOrCol(y, m_defaultRowHeight,
33188aa4 6927 WXGRID_MIN_ROW_HEIGHT, m_rowBottoms, FALSE);
b1da8107 6928}
8f177c8e 6929
f85afd4e 6930
b1da8107
SN
6931int wxGrid::XToCol( int x )
6932{
6933 return CoordToRowOrCol(x, m_defaultColWidth,
33188aa4 6934 WXGRID_MIN_COL_WIDTH, m_colRights, FALSE);
2d66e025 6935}
8f177c8e 6936
2d66e025
MB
6937
6938// return the row number that that the y coord is near the edge of, or
6939// -1 if not near an edge
6940//
6941int wxGrid::YToEdgeOfRow( int y )
6942{
33188aa4
SN
6943 int i;
6944 i = internalYToRow(y);
6945
6946 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 6947 {
33188aa4
SN
6948 // We know that we are in row i, test whether we are
6949 // close enough to lower or upper border, respectively.
6950 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
6951 return i;
6952 else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
6953 return i - 1;
f85afd4e 6954 }
2d66e025
MB
6955
6956 return -1;
6957}
6958
6959
6960// return the col number that that the x coord is near the edge of, or
6961// -1 if not near an edge
6962//
6963int wxGrid::XToEdgeOfCol( int x )
6964{
33188aa4
SN
6965 int i;
6966 i = internalXToCol(x);
6967
6968 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 6969 {
33188aa4
SN
6970 // We know that we are in column i, test whether we are
6971 // close enough to right or left border, respectively.
6972 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
6973 return i;
6974 else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
6975 return i - 1;
f85afd4e 6976 }
2d66e025
MB
6977
6978 return -1;
f85afd4e
MB
6979}
6980
2d66e025
MB
6981
6982wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 6983{
2d66e025 6984 wxRect rect( -1, -1, -1, -1 );
f85afd4e 6985
2d66e025
MB
6986 if ( row >= 0 && row < m_numRows &&
6987 col >= 0 && col < m_numCols )
f85afd4e 6988 {
7c1cb261
VZ
6989 rect.x = GetColLeft(col);
6990 rect.y = GetRowTop(row);
6991 rect.width = GetColWidth(col);
6992 rect.height = GetRowHeight(row);
f85afd4e
MB
6993 }
6994
f6bcfd97
BP
6995 // if grid lines are enabled, then the area of the cell is a bit smaller
6996 if (m_gridLinesEnabled) {
6997 rect.width -= 1;
6998 rect.height -= 1;
6999 }
2d66e025
MB
7000 return rect;
7001}
7002
7003
7004bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
7005{
7006 // get the cell rectangle in logical coords
7007 //
7008 wxRect r( CellToRect( row, col ) );
60ff3b99 7009
2d66e025
MB
7010 // convert to device coords
7011 //
7012 int left, top, right, bottom;
7013 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7014 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 7015
2d66e025
MB
7016 // check against the client area of the grid window
7017 //
7018 int cw, ch;
7019 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7020
2d66e025 7021 if ( wholeCellVisible )
f85afd4e 7022 {
2d66e025 7023 // is the cell wholly visible ?
8f177c8e 7024 //
2d66e025
MB
7025 return ( left >= 0 && right <= cw &&
7026 top >= 0 && bottom <= ch );
7027 }
7028 else
7029 {
7030 // is the cell partly visible ?
7031 //
7032 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
7033 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
7034 }
7035}
7036
7037
7038// make the specified cell location visible by doing a minimal amount
7039// of scrolling
7040//
7041void wxGrid::MakeCellVisible( int row, int col )
7042{
275c4ae4 7043
2d66e025 7044 int i;
60ff3b99 7045 int xpos = -1, ypos = -1;
2d66e025
MB
7046
7047 if ( row >= 0 && row < m_numRows &&
7048 col >= 0 && col < m_numCols )
7049 {
7050 // get the cell rectangle in logical coords
7051 //
7052 wxRect r( CellToRect( row, col ) );
60ff3b99 7053
2d66e025
MB
7054 // convert to device coords
7055 //
7056 int left, top, right, bottom;
7057 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7058 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 7059
2d66e025
MB
7060 int cw, ch;
7061 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7062
2d66e025 7063 if ( top < 0 )
3f296516 7064 {
2d66e025 7065 ypos = r.GetTop();
3f296516 7066 }
2d66e025
MB
7067 else if ( bottom > ch )
7068 {
7069 int h = r.GetHeight();
7070 ypos = r.GetTop();
7071 for ( i = row-1; i >= 0; i-- )
7072 {
7c1cb261
VZ
7073 int rowHeight = GetRowHeight(i);
7074 if ( h + rowHeight > ch )
7075 break;
2d66e025 7076
7c1cb261
VZ
7077 h += rowHeight;
7078 ypos -= rowHeight;
2d66e025 7079 }
f0102d2a
VZ
7080
7081 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7082 // have rounding errors (this is important, because if we do, we
7083 // might not scroll at all and some cells won't be redrawn)
275c4ae4
RD
7084 //
7085 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7086 // scroll unit...
97a9929e 7087 ypos += GRID_SCROLL_LINE_Y;
2d66e025
MB
7088 }
7089
7090 if ( left < 0 )
7091 {
7092 xpos = r.GetLeft();
7093 }
7094 else if ( right > cw )
7095 {
7096 int w = r.GetWidth();
7097 xpos = r.GetLeft();
7098 for ( i = col-1; i >= 0; i-- )
7099 {
7c1cb261
VZ
7100 int colWidth = GetColWidth(i);
7101 if ( w + colWidth > cw )
7102 break;
2d66e025 7103
7c1cb261
VZ
7104 w += colWidth;
7105 xpos -= colWidth;
2d66e025 7106 }
f0102d2a
VZ
7107
7108 // see comment for ypos above
97a9929e 7109 xpos += GRID_SCROLL_LINE_X;
2d66e025
MB
7110 }
7111
7112 if ( xpos != -1 || ypos != -1 )
7113 {
97a9929e
VZ
7114 if ( xpos != -1 )
7115 xpos /= GRID_SCROLL_LINE_X;
7116 if ( ypos != -1 )
7117 ypos /= GRID_SCROLL_LINE_Y;
2d66e025
MB
7118 Scroll( xpos, ypos );
7119 AdjustScrollbars();
7120 }
7121 }
7122}
7123
7124
7125//
7126// ------ Grid cursor movement functions
7127//
7128
5c8fc7c1 7129bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025
MB
7130{
7131 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7132 m_currentCellCoords.GetRow() >= 0 )
2d66e025 7133 {
f6bcfd97 7134 if ( expandSelection)
d95b0c2b
SN
7135 {
7136 if ( m_selectingKeyboard == wxGridNoCellCoords )
7137 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7138 if ( m_selectingKeyboard.GetRow() > 0 )
7139 {
7140 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
7141 MakeCellVisible( m_selectingKeyboard.GetRow(),
7142 m_selectingKeyboard.GetCol() );
c9097836 7143 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7144 }
d95b0c2b 7145 }
f6bcfd97 7146 else if ( m_currentCellCoords.GetRow() > 0 )
aa5e1f75
SN
7147 {
7148 ClearSelection();
7149 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
7150 m_currentCellCoords.GetCol() );
d95b0c2b
SN
7151 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
7152 m_currentCellCoords.GetCol() );
aa5e1f75 7153 }
f6bcfd97
BP
7154 else
7155 return FALSE;
8f177c8e 7156 return TRUE;
f85afd4e 7157 }
2d66e025
MB
7158
7159 return FALSE;
7160}
7161
7162
5c8fc7c1 7163bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025 7164{
2d66e025 7165 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7166 m_currentCellCoords.GetRow() < m_numRows )
f85afd4e 7167 {
5c8fc7c1 7168 if ( expandSelection )
d95b0c2b
SN
7169 {
7170 if ( m_selectingKeyboard == wxGridNoCellCoords )
7171 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7172 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
7173 {
7174 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
7175 MakeCellVisible( m_selectingKeyboard.GetRow(),
7176 m_selectingKeyboard.GetCol() );
c9097836 7177 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7178 }
d95b0c2b 7179 }
f6bcfd97 7180 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
aa5e1f75
SN
7181 {
7182 ClearSelection();
7183 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
7184 m_currentCellCoords.GetCol() );
d95b0c2b
SN
7185 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
7186 m_currentCellCoords.GetCol() );
aa5e1f75 7187 }
f6bcfd97
BP
7188 else
7189 return FALSE;
2d66e025 7190 return TRUE;
f85afd4e 7191 }
2d66e025
MB
7192
7193 return FALSE;
f85afd4e
MB
7194}
7195
2d66e025 7196
5c8fc7c1 7197bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 7198{
2d66e025 7199 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7200 m_currentCellCoords.GetCol() >= 0 )
2d66e025 7201 {
5c8fc7c1 7202 if ( expandSelection )
d95b0c2b
SN
7203 {
7204 if ( m_selectingKeyboard == wxGridNoCellCoords )
7205 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7206 if ( m_selectingKeyboard.GetCol() > 0 )
7207 {
7208 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
7209 MakeCellVisible( m_selectingKeyboard.GetRow(),
7210 m_selectingKeyboard.GetCol() );
c9097836 7211 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7212 }
d95b0c2b 7213 }
f6bcfd97 7214 else if ( m_currentCellCoords.GetCol() > 0 )
aa5e1f75
SN
7215 {
7216 ClearSelection();
7217 MakeCellVisible( m_currentCellCoords.GetRow(),
7218 m_currentCellCoords.GetCol() - 1 );
d95b0c2b
SN
7219 SetCurrentCell( m_currentCellCoords.GetRow(),
7220 m_currentCellCoords.GetCol() - 1 );
aa5e1f75 7221 }
f6bcfd97
BP
7222 else
7223 return FALSE;
2d66e025
MB
7224 return TRUE;
7225 }
7226
7227 return FALSE;
7228}
7229
7230
5c8fc7c1 7231bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025
MB
7232{
7233 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7234 m_currentCellCoords.GetCol() < m_numCols )
f85afd4e 7235 {
5c8fc7c1 7236 if ( expandSelection )
d95b0c2b
SN
7237 {
7238 if ( m_selectingKeyboard == wxGridNoCellCoords )
7239 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7240 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
7241 {
7242 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
7243 MakeCellVisible( m_selectingKeyboard.GetRow(),
7244 m_selectingKeyboard.GetCol() );
c9097836 7245 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7246 }
d95b0c2b 7247 }
f6bcfd97 7248 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
aa5e1f75
SN
7249 {
7250 ClearSelection();
7251 MakeCellVisible( m_currentCellCoords.GetRow(),
7252 m_currentCellCoords.GetCol() + 1 );
7253 SetCurrentCell( m_currentCellCoords.GetRow(),
d95b0c2b 7254 m_currentCellCoords.GetCol() + 1 );
aa5e1f75 7255 }
f6bcfd97
BP
7256 else
7257 return FALSE;
2d66e025 7258 return TRUE;
f85afd4e 7259 }
8f177c8e 7260
2d66e025
MB
7261 return FALSE;
7262}
7263
7264
7265bool wxGrid::MovePageUp()
7266{
7267 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 7268
2d66e025
MB
7269 int row = m_currentCellCoords.GetRow();
7270 if ( row > 0 )
f85afd4e 7271 {
2d66e025
MB
7272 int cw, ch;
7273 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7274
7c1cb261 7275 int y = GetRowTop(row);
2d66e025
MB
7276 int newRow = YToRow( y - ch + 1 );
7277 if ( newRow == -1 )
7278 {
7279 newRow = 0;
7280 }
60ff3b99 7281 else if ( newRow == row )
2d66e025
MB
7282 {
7283 newRow = row - 1;
7284 }
8f177c8e 7285
2d66e025
MB
7286 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7287 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 7288
f85afd4e
MB
7289 return TRUE;
7290 }
2d66e025
MB
7291
7292 return FALSE;
7293}
7294
7295bool wxGrid::MovePageDown()
7296{
7297 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 7298
2d66e025
MB
7299 int row = m_currentCellCoords.GetRow();
7300 if ( row < m_numRows )
f85afd4e 7301 {
2d66e025
MB
7302 int cw, ch;
7303 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7304
7c1cb261 7305 int y = GetRowTop(row);
2d66e025
MB
7306 int newRow = YToRow( y + ch );
7307 if ( newRow == -1 )
7308 {
7309 newRow = m_numRows - 1;
7310 }
60ff3b99 7311 else if ( newRow == row )
2d66e025
MB
7312 {
7313 newRow = row + 1;
7314 }
7315
7316 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7317 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 7318
2d66e025 7319 return TRUE;
f85afd4e 7320 }
2d66e025
MB
7321
7322 return FALSE;
f85afd4e 7323}
8f177c8e 7324
5c8fc7c1 7325bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
7326{
7327 if ( m_table &&
7328 m_currentCellCoords != wxGridNoCellCoords &&
7329 m_currentCellCoords.GetRow() > 0 )
7330 {
7331 int row = m_currentCellCoords.GetRow();
7332 int col = m_currentCellCoords.GetCol();
7333
7334 if ( m_table->IsEmptyCell(row, col) )
7335 {
7336 // starting in an empty cell: find the next block of
7337 // non-empty cells
7338 //
7339 while ( row > 0 )
7340 {
7341 row-- ;
7342 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7343 }
7344 }
7345 else if ( m_table->IsEmptyCell(row-1, col) )
7346 {
7347 // starting at the top of a block: find the next block
7348 //
7349 row--;
7350 while ( row > 0 )
7351 {
7352 row-- ;
7353 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7354 }
7355 }
7356 else
7357 {
7358 // starting within a block: find the top of the block
7359 //
7360 while ( row > 0 )
7361 {
7362 row-- ;
7363 if ( m_table->IsEmptyCell(row, col) )
7364 {
7365 row++ ;
7366 break;
7367 }
7368 }
7369 }
f85afd4e 7370
2d66e025 7371 MakeCellVisible( row, col );
5c8fc7c1 7372 if ( expandSelection )
d95b0c2b
SN
7373 {
7374 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7375 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7376 }
7377 else
aa5e1f75
SN
7378 {
7379 ClearSelection();
7380 SetCurrentCell( row, col );
7381 }
2d66e025
MB
7382 return TRUE;
7383 }
f85afd4e 7384
2d66e025
MB
7385 return FALSE;
7386}
f85afd4e 7387
5c8fc7c1 7388bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 7389{
2d66e025
MB
7390 if ( m_table &&
7391 m_currentCellCoords != wxGridNoCellCoords &&
7392 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 7393 {
2d66e025
MB
7394 int row = m_currentCellCoords.GetRow();
7395 int col = m_currentCellCoords.GetCol();
7396
7397 if ( m_table->IsEmptyCell(row, col) )
7398 {
7399 // starting in an empty cell: find the next block of
7400 // non-empty cells
7401 //
7402 while ( row < m_numRows-1 )
7403 {
7404 row++ ;
7405 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7406 }
7407 }
7408 else if ( m_table->IsEmptyCell(row+1, col) )
7409 {
7410 // starting at the bottom of a block: find the next block
7411 //
7412 row++;
7413 while ( row < m_numRows-1 )
7414 {
7415 row++ ;
7416 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7417 }
7418 }
7419 else
7420 {
7421 // starting within a block: find the bottom of the block
7422 //
7423 while ( row < m_numRows-1 )
7424 {
7425 row++ ;
7426 if ( m_table->IsEmptyCell(row, col) )
7427 {
7428 row-- ;
7429 break;
7430 }
7431 }
7432 }
7433
7434 MakeCellVisible( row, col );
5c8fc7c1 7435 if ( expandSelection )
d95b0c2b
SN
7436 {
7437 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7438 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7439 }
7440 else
aa5e1f75
SN
7441 {
7442 ClearSelection();
7443 SetCurrentCell( row, col );
7444 }
2d66e025
MB
7445
7446 return TRUE;
f85afd4e 7447 }
f85afd4e 7448
2d66e025
MB
7449 return FALSE;
7450}
f85afd4e 7451
5c8fc7c1 7452bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 7453{
2d66e025
MB
7454 if ( m_table &&
7455 m_currentCellCoords != wxGridNoCellCoords &&
7456 m_currentCellCoords.GetCol() > 0 )
f85afd4e 7457 {
2d66e025
MB
7458 int row = m_currentCellCoords.GetRow();
7459 int col = m_currentCellCoords.GetCol();
7460
7461 if ( m_table->IsEmptyCell(row, col) )
7462 {
7463 // starting in an empty cell: find the next block of
7464 // non-empty cells
7465 //
7466 while ( col > 0 )
7467 {
7468 col-- ;
7469 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7470 }
7471 }
7472 else if ( m_table->IsEmptyCell(row, col-1) )
7473 {
7474 // starting at the left of a block: find the next block
7475 //
7476 col--;
7477 while ( col > 0 )
7478 {
7479 col-- ;
7480 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7481 }
7482 }
7483 else
7484 {
7485 // starting within a block: find the left of the block
7486 //
7487 while ( col > 0 )
7488 {
7489 col-- ;
7490 if ( m_table->IsEmptyCell(row, col) )
7491 {
7492 col++ ;
7493 break;
7494 }
7495 }
7496 }
f85afd4e 7497
2d66e025 7498 MakeCellVisible( row, col );
5c8fc7c1 7499 if ( expandSelection )
d95b0c2b
SN
7500 {
7501 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7502 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7503 }
7504 else
aa5e1f75
SN
7505 {
7506 ClearSelection();
7507 SetCurrentCell( row, col );
7508 }
8f177c8e 7509
2d66e025 7510 return TRUE;
f85afd4e 7511 }
2d66e025
MB
7512
7513 return FALSE;
f85afd4e
MB
7514}
7515
5c8fc7c1 7516bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 7517{
2d66e025
MB
7518 if ( m_table &&
7519 m_currentCellCoords != wxGridNoCellCoords &&
7520 m_currentCellCoords.GetCol() < m_numCols-1 )
f85afd4e 7521 {
2d66e025
MB
7522 int row = m_currentCellCoords.GetRow();
7523 int col = m_currentCellCoords.GetCol();
8f177c8e 7524
2d66e025
MB
7525 if ( m_table->IsEmptyCell(row, col) )
7526 {
7527 // starting in an empty cell: find the next block of
7528 // non-empty cells
7529 //
7530 while ( col < m_numCols-1 )
7531 {
7532 col++ ;
7533 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7534 }
7535 }
7536 else if ( m_table->IsEmptyCell(row, col+1) )
7537 {
7538 // starting at the right of a block: find the next block
7539 //
7540 col++;
7541 while ( col < m_numCols-1 )
7542 {
7543 col++ ;
7544 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7545 }
7546 }
7547 else
7548 {
7549 // starting within a block: find the right of the block
7550 //
7551 while ( col < m_numCols-1 )
7552 {
7553 col++ ;
7554 if ( m_table->IsEmptyCell(row, col) )
7555 {
7556 col-- ;
7557 break;
7558 }
7559 }
7560 }
8f177c8e 7561
2d66e025 7562 MakeCellVisible( row, col );
5c8fc7c1 7563 if ( expandSelection )
d95b0c2b
SN
7564 {
7565 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7566 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7567 }
7568 else
aa5e1f75
SN
7569 {
7570 ClearSelection();
7571 SetCurrentCell( row, col );
7572 }
f85afd4e 7573
2d66e025 7574 return TRUE;
f85afd4e 7575 }
2d66e025
MB
7576
7577 return FALSE;
f85afd4e
MB
7578}
7579
7580
2d66e025 7581
f85afd4e 7582//
2d66e025 7583// ------ Label values and formatting
f85afd4e
MB
7584//
7585
7586void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
7587{
7588 *horiz = m_rowLabelHorizAlign;
7589 *vert = m_rowLabelVertAlign;
7590}
7591
7592void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
7593{
7594 *horiz = m_colLabelHorizAlign;
7595 *vert = m_colLabelVertAlign;
7596}
7597
7598wxString wxGrid::GetRowLabelValue( int row )
7599{
7600 if ( m_table )
7601 {
7602 return m_table->GetRowLabelValue( row );
7603 }
7604 else
7605 {
7606 wxString s;
7607 s << row;
7608 return s;
7609 }
7610}
7611
7612wxString wxGrid::GetColLabelValue( int col )
7613{
7614 if ( m_table )
7615 {
7616 return m_table->GetColLabelValue( col );
7617 }
7618 else
7619 {
7620 wxString s;
7621 s << col;
7622 return s;
7623 }
7624}
7625
2e8cd977 7626
f85afd4e
MB
7627void wxGrid::SetRowLabelSize( int width )
7628{
2e8cd977
MB
7629 width = wxMax( width, 0 );
7630 if ( width != m_rowLabelWidth )
7631 {
2e8cd977
MB
7632 if ( width == 0 )
7633 {
7634 m_rowLabelWin->Show( FALSE );
7807d81c 7635 m_cornerLabelWin->Show( FALSE );
2e8cd977 7636 }
7807d81c 7637 else if ( m_rowLabelWidth == 0 )
2e8cd977 7638 {
7807d81c
MB
7639 m_rowLabelWin->Show( TRUE );
7640 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 7641 }
b99be8fb 7642
2e8cd977 7643 m_rowLabelWidth = width;
7807d81c
MB
7644 CalcWindowSizes();
7645 Refresh( TRUE );
2e8cd977 7646 }
f85afd4e
MB
7647}
7648
2e8cd977 7649
f85afd4e
MB
7650void wxGrid::SetColLabelSize( int height )
7651{
7807d81c 7652 height = wxMax( height, 0 );
2e8cd977
MB
7653 if ( height != m_colLabelHeight )
7654 {
2e8cd977
MB
7655 if ( height == 0 )
7656 {
2e8cd977 7657 m_colLabelWin->Show( FALSE );
7807d81c 7658 m_cornerLabelWin->Show( FALSE );
2e8cd977 7659 }
7807d81c 7660 else if ( m_colLabelHeight == 0 )
2e8cd977 7661 {
7807d81c
MB
7662 m_colLabelWin->Show( TRUE );
7663 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 7664 }
7807d81c 7665
2e8cd977 7666 m_colLabelHeight = height;
7807d81c
MB
7667 CalcWindowSizes();
7668 Refresh( TRUE );
2e8cd977 7669 }
f85afd4e
MB
7670}
7671
2e8cd977 7672
f85afd4e
MB
7673void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
7674{
2d66e025
MB
7675 if ( m_labelBackgroundColour != colour )
7676 {
7677 m_labelBackgroundColour = colour;
7678 m_rowLabelWin->SetBackgroundColour( colour );
7679 m_colLabelWin->SetBackgroundColour( colour );
7680 m_cornerLabelWin->SetBackgroundColour( colour );
7681
7682 if ( !GetBatchCount() )
7683 {
7684 m_rowLabelWin->Refresh();
7685 m_colLabelWin->Refresh();
7686 m_cornerLabelWin->Refresh();
7687 }
7688 }
f85afd4e
MB
7689}
7690
7691void wxGrid::SetLabelTextColour( const wxColour& colour )
7692{
2d66e025
MB
7693 if ( m_labelTextColour != colour )
7694 {
7695 m_labelTextColour = colour;
7696 if ( !GetBatchCount() )
7697 {
7698 m_rowLabelWin->Refresh();
7699 m_colLabelWin->Refresh();
7700 }
7701 }
f85afd4e
MB
7702}
7703
7704void wxGrid::SetLabelFont( const wxFont& font )
7705{
7706 m_labelFont = font;
2d66e025
MB
7707 if ( !GetBatchCount() )
7708 {
7709 m_rowLabelWin->Refresh();
7710 m_colLabelWin->Refresh();
7711 }
f85afd4e
MB
7712}
7713
7714void wxGrid::SetRowLabelAlignment( int horiz, int vert )
7715{
4c7277db
MB
7716 // allow old (incorrect) defs to be used
7717 switch ( horiz )
7718 {
7719 case wxLEFT: horiz = wxALIGN_LEFT; break;
7720 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7721 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7722 }
84912ef8 7723
4c7277db
MB
7724 switch ( vert )
7725 {
7726 case wxTOP: vert = wxALIGN_TOP; break;
7727 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7728 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7729 }
84912ef8 7730
4c7277db 7731 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7732 {
7733 m_rowLabelHorizAlign = horiz;
7734 }
8f177c8e 7735
4c7277db 7736 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7737 {
7738 m_rowLabelVertAlign = vert;
7739 }
7740
2d66e025
MB
7741 if ( !GetBatchCount() )
7742 {
7743 m_rowLabelWin->Refresh();
60ff3b99 7744 }
f85afd4e
MB
7745}
7746
7747void wxGrid::SetColLabelAlignment( int horiz, int vert )
7748{
4c7277db
MB
7749 // allow old (incorrect) defs to be used
7750 switch ( horiz )
7751 {
7752 case wxLEFT: horiz = wxALIGN_LEFT; break;
7753 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7754 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7755 }
84912ef8 7756
4c7277db
MB
7757 switch ( vert )
7758 {
7759 case wxTOP: vert = wxALIGN_TOP; break;
7760 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7761 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7762 }
84912ef8 7763
4c7277db 7764 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7765 {
7766 m_colLabelHorizAlign = horiz;
7767 }
8f177c8e 7768
4c7277db 7769 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7770 {
7771 m_colLabelVertAlign = vert;
7772 }
7773
2d66e025
MB
7774 if ( !GetBatchCount() )
7775 {
2d66e025 7776 m_colLabelWin->Refresh();
60ff3b99 7777 }
f85afd4e
MB
7778}
7779
7780void wxGrid::SetRowLabelValue( int row, const wxString& s )
7781{
7782 if ( m_table )
7783 {
7784 m_table->SetRowLabelValue( row, s );
2d66e025
MB
7785 if ( !GetBatchCount() )
7786 {
70c7a608
SN
7787 wxRect rect = CellToRect( row, 0);
7788 if ( rect.height > 0 )
7789 {
cb309039 7790 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 7791 rect.x = 0;
70c7a608
SN
7792 rect.width = m_rowLabelWidth;
7793 m_rowLabelWin->Refresh( TRUE, &rect );
7794 }
2d66e025 7795 }
f85afd4e
MB
7796 }
7797}
7798
7799void wxGrid::SetColLabelValue( int col, const wxString& s )
7800{
7801 if ( m_table )
7802 {
7803 m_table->SetColLabelValue( col, s );
2d66e025
MB
7804 if ( !GetBatchCount() )
7805 {
70c7a608
SN
7806 wxRect rect = CellToRect( 0, col );
7807 if ( rect.width > 0 )
7808 {
cb309039 7809 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
b1944ebc 7810 rect.y = 0;
70c7a608
SN
7811 rect.height = m_colLabelHeight;
7812 m_colLabelWin->Refresh( TRUE, &rect );
7813 }
2d66e025 7814 }
f85afd4e
MB
7815 }
7816}
7817
7818void wxGrid::SetGridLineColour( const wxColour& colour )
7819{
2d66e025
MB
7820 if ( m_gridLineColour != colour )
7821 {
7822 m_gridLineColour = colour;
60ff3b99 7823
2d66e025
MB
7824 wxClientDC dc( m_gridWin );
7825 PrepareDC( dc );
c6a51dcd 7826 DrawAllGridLines( dc, wxRegion() );
2d66e025 7827 }
f85afd4e
MB
7828}
7829
f6bcfd97
BP
7830
7831void wxGrid::SetCellHighlightColour( const wxColour& colour )
7832{
7833 if ( m_cellHighlightColour != colour )
7834 {
7835 m_cellHighlightColour = colour;
7836
7837 wxClientDC dc( m_gridWin );
7838 PrepareDC( dc );
7839 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7840 DrawCellHighlight(dc, attr);
7841 attr->DecRef();
7842 }
7843}
7844
d2520c85
RD
7845void wxGrid::SetCellHighlightPenWidth(int width)
7846{
7847 if (m_cellHighlightPenWidth != width) {
7848 m_cellHighlightPenWidth = width;
7849
7850 // Just redrawing the cell highlight is not enough since that won't
7851 // make any visible change if the the thickness is getting smaller.
7852 int row = m_currentCellCoords.GetRow();
7853 int col = m_currentCellCoords.GetCol();
7854 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7855 return;
7856 wxRect rect = CellToRect(row, col);
7857 m_gridWin->Refresh(TRUE, &rect);
7858 }
7859}
7860
7861void wxGrid::SetCellHighlightROPenWidth(int width)
7862{
7863 if (m_cellHighlightROPenWidth != width) {
7864 m_cellHighlightROPenWidth = width;
7865
7866 // Just redrawing the cell highlight is not enough since that won't
7867 // make any visible change if the the thickness is getting smaller.
7868 int row = m_currentCellCoords.GetRow();
7869 int col = m_currentCellCoords.GetCol();
7870 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7871 return;
7872 wxRect rect = CellToRect(row, col);
7873 m_gridWin->Refresh(TRUE, &rect);
7874 }
7875}
7876
f85afd4e
MB
7877void wxGrid::EnableGridLines( bool enable )
7878{
7879 if ( enable != m_gridLinesEnabled )
7880 {
7881 m_gridLinesEnabled = enable;
2d66e025
MB
7882
7883 if ( !GetBatchCount() )
7884 {
7885 if ( enable )
7886 {
7887 wxClientDC dc( m_gridWin );
7888 PrepareDC( dc );
c6a51dcd 7889 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
7890 }
7891 else
7892 {
7893 m_gridWin->Refresh();
7894 }
7895 }
f85afd4e
MB
7896 }
7897}
7898
7899
7900int wxGrid::GetDefaultRowSize()
7901{
7902 return m_defaultRowHeight;
7903}
7904
7905int wxGrid::GetRowSize( int row )
7906{
b99be8fb
VZ
7907 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
7908
7c1cb261 7909 return GetRowHeight(row);
f85afd4e
MB
7910}
7911
7912int wxGrid::GetDefaultColSize()
7913{
7914 return m_defaultColWidth;
7915}
7916
7917int wxGrid::GetColSize( int col )
7918{
b99be8fb
VZ
7919 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
7920
7c1cb261 7921 return GetColWidth(col);
f85afd4e
MB
7922}
7923
2e9a6788
VZ
7924// ============================================================================
7925// access to the grid attributes: each of them has a default value in the grid
7926// itself and may be overidden on a per-cell basis
7927// ============================================================================
7928
7929// ----------------------------------------------------------------------------
7930// setting default attributes
7931// ----------------------------------------------------------------------------
7932
7933void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
7934{
2796cce3 7935 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
7936#ifdef __WXGTK__
7937 m_gridWin->SetBackgroundColour(col);
7938#endif
2e9a6788
VZ
7939}
7940
7941void wxGrid::SetDefaultCellTextColour( const wxColour& col )
7942{
2796cce3 7943 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
7944}
7945
7946void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
7947{
2796cce3 7948 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
7949}
7950
7951void wxGrid::SetDefaultCellFont( const wxFont& font )
7952{
2796cce3
RD
7953 m_defaultCellAttr->SetFont(font);
7954}
7955
0ba143c9
RD
7956void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
7957{
7958 m_defaultCellAttr->SetRenderer(renderer);
7959}
2e9a6788 7960
0ba143c9
RD
7961void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
7962{
7963 m_defaultCellAttr->SetEditor(editor);
7964}
9b4aede2 7965
2e9a6788
VZ
7966// ----------------------------------------------------------------------------
7967// access to the default attrbiutes
7968// ----------------------------------------------------------------------------
7969
f85afd4e
MB
7970wxColour wxGrid::GetDefaultCellBackgroundColour()
7971{
2796cce3 7972 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
7973}
7974
2e9a6788
VZ
7975wxColour wxGrid::GetDefaultCellTextColour()
7976{
2796cce3 7977 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
7978}
7979
7980wxFont wxGrid::GetDefaultCellFont()
7981{
2796cce3 7982 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
7983}
7984
7985void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
7986{
2796cce3 7987 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
7988}
7989
0ba143c9
RD
7990wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
7991{
0b190b0f 7992 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 7993}
ab79958a 7994
0ba143c9
RD
7995wxGridCellEditor *wxGrid::GetDefaultEditor() const
7996{
28a77bc4 7997 return m_defaultCellAttr->GetEditor(NULL,0,0);
0ba143c9 7998}
9b4aede2 7999
2e9a6788
VZ
8000// ----------------------------------------------------------------------------
8001// access to cell attributes
8002// ----------------------------------------------------------------------------
8003
b99be8fb 8004wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 8005{
0a976765 8006 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8007 wxColour colour = attr->GetBackgroundColour();
39bcce60 8008 attr->DecRef();
b99be8fb 8009 return colour;
f85afd4e
MB
8010}
8011
b99be8fb 8012wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 8013{
0a976765 8014 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8015 wxColour colour = attr->GetTextColour();
39bcce60 8016 attr->DecRef();
b99be8fb 8017 return colour;
f85afd4e
MB
8018}
8019
b99be8fb 8020wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 8021{
0a976765 8022 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8023 wxFont font = attr->GetFont();
39bcce60 8024 attr->DecRef();
b99be8fb 8025 return font;
f85afd4e
MB
8026}
8027
b99be8fb 8028void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 8029{
0a976765 8030 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 8031 attr->GetAlignment(horiz, vert);
39bcce60 8032 attr->DecRef();
2e9a6788
VZ
8033}
8034
2796cce3
RD
8035wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
8036{
8037 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8038 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 8039 attr->DecRef();
0b190b0f 8040
2796cce3
RD
8041 return renderer;
8042}
8043
9b4aede2
RD
8044wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
8045{
8046 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8047 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 8048 attr->DecRef();
0b190b0f 8049
9b4aede2
RD
8050 return editor;
8051}
8052
283b7808
VZ
8053bool wxGrid::IsReadOnly(int row, int col) const
8054{
8055 wxGridCellAttr* attr = GetCellAttr(row, col);
8056 bool isReadOnly = attr->IsReadOnly();
8057 attr->DecRef();
8058 return isReadOnly;
8059}
8060
2e9a6788 8061// ----------------------------------------------------------------------------
758cbedf 8062// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
8063// ----------------------------------------------------------------------------
8064
8065bool wxGrid::CanHaveAttributes()
8066{
8067 if ( !m_table )
8068 {
8069 return FALSE;
8070 }
8071
f2d76237 8072 return m_table->CanHaveAttributes();
2e9a6788
VZ
8073}
8074
0a976765
VZ
8075void wxGrid::ClearAttrCache()
8076{
8077 if ( m_attrCache.row != -1 )
8078 {
39bcce60 8079 wxSafeDecRef(m_attrCache.attr);
19d7140e 8080 m_attrCache.attr = NULL;
0a976765
VZ
8081 m_attrCache.row = -1;
8082 }
8083}
8084
8085void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
8086{
8087 wxGrid *self = (wxGrid *)this; // const_cast
8088
8089 self->ClearAttrCache();
8090 self->m_attrCache.row = row;
8091 self->m_attrCache.col = col;
8092 self->m_attrCache.attr = attr;
39bcce60 8093 wxSafeIncRef(attr);
0a976765
VZ
8094}
8095
8096bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
8097{
8098 if ( row == m_attrCache.row && col == m_attrCache.col )
8099 {
8100 *attr = m_attrCache.attr;
39bcce60 8101 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
8102
8103#ifdef DEBUG_ATTR_CACHE
8104 gs_nAttrCacheHits++;
8105#endif
8106
8107 return TRUE;
8108 }
8109 else
8110 {
8111#ifdef DEBUG_ATTR_CACHE
8112 gs_nAttrCacheMisses++;
8113#endif
8114 return FALSE;
8115 }
8116}
8117
2e9a6788
VZ
8118wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
8119{
0a976765
VZ
8120 wxGridCellAttr *attr;
8121 if ( !LookupAttr(row, col, &attr) )
2e9a6788 8122 {
19d7140e 8123 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any) : (wxGridCellAttr *)NULL;
0a976765
VZ
8124 CacheAttr(row, col, attr);
8125 }
508011ce
VZ
8126 if (attr)
8127 {
2796cce3 8128 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
8129 }
8130 else
8131 {
2796cce3
RD
8132 attr = m_defaultCellAttr;
8133 attr->IncRef();
8134 }
2e9a6788 8135
0a976765
VZ
8136 return attr;
8137}
8138
8139wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
8140{
19d7140e 8141 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
0a976765 8142
1df4050d
VZ
8143 wxCHECK_MSG( m_table, attr,
8144 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8145
8146 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
8147 if ( !attr )
8148 {
8149 attr = new wxGridCellAttr(m_defaultCellAttr);
8150
8151 // artificially inc the ref count to match DecRef() in caller
8152 attr->IncRef();
8153 m_table->SetAttr(attr, row, col);
8154 }
0a976765 8155
2e9a6788
VZ
8156 return attr;
8157}
8158
0b190b0f
VZ
8159// ----------------------------------------------------------------------------
8160// setting column attributes (wrappers around SetColAttr)
8161// ----------------------------------------------------------------------------
8162
8163void wxGrid::SetColFormatBool(int col)
8164{
8165 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
8166}
8167
8168void wxGrid::SetColFormatNumber(int col)
8169{
8170 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
8171}
8172
8173void wxGrid::SetColFormatFloat(int col, int width, int precision)
8174{
8175 wxString typeName = wxGRID_VALUE_FLOAT;
8176 if ( (width != -1) || (precision != -1) )
8177 {
8178 typeName << _T(':') << width << _T(',') << precision;
8179 }
8180
8181 SetColFormatCustom(col, typeName);
8182}
8183
8184void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
8185{
19d7140e
VZ
8186 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
8187
8188 attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
8189 if(!attr)
8190 attr = new wxGridCellAttr;
0b190b0f
VZ
8191 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
8192 attr->SetRenderer(renderer);
8193
8194 SetColAttr(col, attr);
19d7140e 8195
0b190b0f
VZ
8196}
8197
758cbedf
VZ
8198// ----------------------------------------------------------------------------
8199// setting cell attributes: this is forwarded to the table
8200// ----------------------------------------------------------------------------
8201
8202void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
8203{
8204 if ( CanHaveAttributes() )
8205 {
8206 m_table->SetRowAttr(attr, row);
19d7140e 8207 ClearAttrCache();
758cbedf
VZ
8208 }
8209 else
8210 {
39bcce60 8211 wxSafeDecRef(attr);
758cbedf
VZ
8212 }
8213}
8214
8215void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
8216{
8217 if ( CanHaveAttributes() )
8218 {
8219 m_table->SetColAttr(attr, col);
19d7140e 8220 ClearAttrCache();
758cbedf
VZ
8221 }
8222 else
8223 {
39bcce60 8224 wxSafeDecRef(attr);
758cbedf
VZ
8225 }
8226}
8227
2e9a6788
VZ
8228void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
8229{
8230 if ( CanHaveAttributes() )
8231 {
0a976765 8232 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8233 attr->SetBackgroundColour(colour);
8234 attr->DecRef();
8235 }
8236}
8237
8238void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
8239{
8240 if ( CanHaveAttributes() )
8241 {
0a976765 8242 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8243 attr->SetTextColour(colour);
8244 attr->DecRef();
8245 }
8246}
8247
8248void wxGrid::SetCellFont( int row, int col, const wxFont& font )
8249{
8250 if ( CanHaveAttributes() )
8251 {
0a976765 8252 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8253 attr->SetFont(font);
8254 attr->DecRef();
8255 }
8256}
8257
8258void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
8259{
8260 if ( CanHaveAttributes() )
8261 {
0a976765 8262 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8263 attr->SetAlignment(horiz, vert);
8264 attr->DecRef();
ab79958a
VZ
8265 }
8266}
8267
8268void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
8269{
8270 if ( CanHaveAttributes() )
8271 {
0a976765 8272 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
8273 attr->SetRenderer(renderer);
8274 attr->DecRef();
9b4aede2
RD
8275 }
8276}
8277
8278void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
8279{
8280 if ( CanHaveAttributes() )
8281 {
8282 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8283 attr->SetEditor(editor);
8284 attr->DecRef();
283b7808
VZ
8285 }
8286}
8287
8288void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
8289{
8290 if ( CanHaveAttributes() )
8291 {
8292 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8293 attr->SetReadOnly(isReadOnly);
8294 attr->DecRef();
2e9a6788 8295 }
f85afd4e
MB
8296}
8297
f2d76237
RD
8298// ----------------------------------------------------------------------------
8299// Data type registration
8300// ----------------------------------------------------------------------------
8301
8302void wxGrid::RegisterDataType(const wxString& typeName,
8303 wxGridCellRenderer* renderer,
8304 wxGridCellEditor* editor)
8305{
8306 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
8307}
8308
8309
99306db2 8310wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
8311{
8312 wxString typeName = m_table->GetTypeName(row, col);
8313 return GetDefaultEditorForType(typeName);
8314}
8315
99306db2 8316wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
8317{
8318 wxString typeName = m_table->GetTypeName(row, col);
8319 return GetDefaultRendererForType(typeName);
8320}
8321
99306db2
VZ
8322wxGridCellEditor*
8323wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 8324{
c4608a8a 8325 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8326 if ( index == wxNOT_FOUND )
8327 {
8328 wxFAIL_MSG(wxT("Unknown data type name"));
8329
f2d76237
RD
8330 return NULL;
8331 }
0b190b0f 8332
f2d76237
RD
8333 return m_typeRegistry->GetEditor(index);
8334}
8335
99306db2
VZ
8336wxGridCellRenderer*
8337wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 8338{
c4608a8a 8339 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8340 if ( index == wxNOT_FOUND )
8341 {
c4608a8a 8342 wxFAIL_MSG(wxT("Unknown data type name"));
0b190b0f 8343
c4608a8a 8344 return NULL;
e72b4213 8345 }
0b190b0f 8346
c4608a8a 8347 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
8348}
8349
8350
2e9a6788
VZ
8351// ----------------------------------------------------------------------------
8352// row/col size
8353// ----------------------------------------------------------------------------
8354
6e8524b1
MB
8355void wxGrid::EnableDragRowSize( bool enable )
8356{
8357 m_canDragRowSize = enable;
8358}
8359
8360
8361void wxGrid::EnableDragColSize( bool enable )
8362{
8363 m_canDragColSize = enable;
8364}
8365
4cfa5de6
RD
8366void wxGrid::EnableDragGridSize( bool enable )
8367{
8368 m_canDragGridSize = enable;
8369}
8370
6e8524b1 8371
f85afd4e
MB
8372void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
8373{
8374 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
8375
8376 if ( resizeExistingRows )
8377 {
b1da8107
SN
8378 // since we are resizing all rows to the default row size,
8379 // we can simply clear the row heights and row bottoms
8380 // arrays (which also allows us to take advantage of
8381 // some speed optimisations)
8382 m_rowHeights.Empty();
8383 m_rowBottoms.Empty();
edb89f7e
VZ
8384 if ( !GetBatchCount() )
8385 CalcDimensions();
f85afd4e
MB
8386 }
8387}
8388
8389void wxGrid::SetRowSize( int row, int height )
8390{
b99be8fb 8391 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 8392
7c1cb261
VZ
8393 if ( m_rowHeights.IsEmpty() )
8394 {
8395 // need to really create the array
8396 InitRowHeights();
8397 }
60ff3b99 8398
b99be8fb
VZ
8399 int h = wxMax( 0, height );
8400 int diff = h - m_rowHeights[row];
60ff3b99 8401
b99be8fb 8402 m_rowHeights[row] = h;
7c1cb261 8403 int i;
b99be8fb 8404 for ( i = row; i < m_numRows; i++ )
f85afd4e 8405 {
b99be8fb 8406 m_rowBottoms[i] += diff;
f85afd4e 8407 }
faec5a43
SN
8408 if ( !GetBatchCount() )
8409 CalcDimensions();
f85afd4e
MB
8410}
8411
8412void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
8413{
8414 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
8415
8416 if ( resizeExistingCols )
8417 {
b1da8107
SN
8418 // since we are resizing all columns to the default column size,
8419 // we can simply clear the col widths and col rights
8420 // arrays (which also allows us to take advantage of
8421 // some speed optimisations)
8422 m_colWidths.Empty();
8423 m_colRights.Empty();
edb89f7e
VZ
8424 if ( !GetBatchCount() )
8425 CalcDimensions();
f85afd4e
MB
8426 }
8427}
8428
8429void wxGrid::SetColSize( int col, int width )
8430{
b99be8fb 8431 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 8432
43947979
VZ
8433 // should we check that it's bigger than GetColMinimalWidth(col) here?
8434
7c1cb261
VZ
8435 if ( m_colWidths.IsEmpty() )
8436 {
8437 // need to really create the array
8438 InitColWidths();
8439 }
f85afd4e 8440
b99be8fb
VZ
8441 int w = wxMax( 0, width );
8442 int diff = w - m_colWidths[col];
8443 m_colWidths[col] = w;
60ff3b99 8444
7c1cb261 8445 int i;
b99be8fb 8446 for ( i = col; i < m_numCols; i++ )
f85afd4e 8447 {
b99be8fb 8448 m_colRights[i] += diff;
f85afd4e 8449 }
faec5a43
SN
8450 if ( !GetBatchCount() )
8451 CalcDimensions();
f85afd4e
MB
8452}
8453
2d66e025 8454
43947979
VZ
8455void wxGrid::SetColMinimalWidth( int col, int width )
8456{
af547d51
VZ
8457 m_colMinWidths.Put(col, width);
8458}
8459
8460void wxGrid::SetRowMinimalHeight( int row, int width )
8461{
8462 m_rowMinHeights.Put(row, width);
43947979
VZ
8463}
8464
8465int wxGrid::GetColMinimalWidth(int col) const
8466{
af547d51
VZ
8467 long value = m_colMinWidths.Get(col);
8468 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
8469}
8470
8471int wxGrid::GetRowMinimalHeight(int row) const
8472{
8473 long value = m_rowMinHeights.Get(row);
8474 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
43947979
VZ
8475}
8476
57c086ef
VZ
8477// ----------------------------------------------------------------------------
8478// auto sizing
8479// ----------------------------------------------------------------------------
8480
af547d51 8481void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
8482{
8483 wxClientDC dc(m_gridWin);
8484
a95e38c0
VZ
8485 // init both of them to avoid compiler warnings, even if weo nly need one
8486 int row = -1,
8487 col = -1;
af547d51
VZ
8488 if ( column )
8489 col = colOrRow;
8490 else
8491 row = colOrRow;
8492
8493 wxCoord extent, extentMax = 0;
8494 int max = column ? m_numRows : m_numCols;
39bcce60 8495 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 8496 {
af547d51
VZ
8497 if ( column )
8498 row = rowOrCol;
8499 else
8500 col = rowOrCol;
8501
65e4e78e 8502 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8503 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
8504 if ( renderer )
8505 {
af547d51
VZ
8506 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
8507 extent = column ? size.x : size.y;
8508 if ( extent > extentMax )
65e4e78e 8509 {
af547d51 8510 extentMax = extent;
65e4e78e 8511 }
0b190b0f
VZ
8512
8513 renderer->DecRef();
65e4e78e
VZ
8514 }
8515
8516 attr->DecRef();
8517 }
8518
af547d51
VZ
8519 // now also compare with the column label extent
8520 wxCoord w, h;
65e4e78e 8521 dc.SetFont( GetLabelFont() );
294d195c
MB
8522
8523 if ( column )
8524 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
8525 else
ee495e4c 8526 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
294d195c 8527
af547d51
VZ
8528 extent = column ? w : h;
8529 if ( extent > extentMax )
65e4e78e 8530 {
af547d51 8531 extentMax = extent;
65e4e78e
VZ
8532 }
8533
af547d51 8534 if ( !extentMax )
65e4e78e 8535 {
af547d51
VZ
8536 // empty column - give default extent (notice that if extentMax is less
8537 // than default extent but != 0, it's ok)
8538 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
8539 }
8540 else
8541 {
a95e38c0
VZ
8542 if ( column )
8543 {
8544 // leave some space around text
8545 extentMax += 10;
8546 }
f6bcfd97
BP
8547 else
8548 {
8549 extentMax += 6;
8550 }
65e4e78e
VZ
8551 }
8552
edb89f7e
VZ
8553 if ( column )
8554 {
af547d51 8555 SetColSize(col, extentMax);
faec5a43
SN
8556 if ( !GetBatchCount() )
8557 {
edb89f7e
VZ
8558 int cw, ch, dummy;
8559 m_gridWin->GetClientSize( &cw, &ch );
8560 wxRect rect ( CellToRect( 0, col ) );
8561 rect.y = 0;
8562 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
8563 rect.width = cw - rect.x;
8564 rect.height = m_colLabelHeight;
8565 m_colLabelWin->Refresh( TRUE, &rect );
8566 }
8567 }
8568 else
8569 {
39bcce60 8570 SetRowSize(row, extentMax);
faec5a43
SN
8571 if ( !GetBatchCount() )
8572 {
edb89f7e
VZ
8573 int cw, ch, dummy;
8574 m_gridWin->GetClientSize( &cw, &ch );
8575 wxRect rect ( CellToRect( row, 0 ) );
8576 rect.x = 0;
8577 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
8578 rect.width = m_rowLabelWidth;
faec5a43 8579 rect.height = ch - rect.y;
edb89f7e
VZ
8580 m_rowLabelWin->Refresh( TRUE, &rect );
8581 }
faec5a43 8582 }
65e4e78e
VZ
8583 if ( setAsMin )
8584 {
af547d51
VZ
8585 if ( column )
8586 SetColMinimalWidth(col, extentMax);
8587 else
39bcce60 8588 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
8589 }
8590}
8591
266e8367 8592int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 8593{
57c086ef
VZ
8594 int width = m_rowLabelWidth;
8595
faec5a43
SN
8596 if ( !calcOnly )
8597 BeginBatch();
97a9929e 8598
65e4e78e
VZ
8599 for ( int col = 0; col < m_numCols; col++ )
8600 {
266e8367
VZ
8601 if ( !calcOnly )
8602 {
8603 AutoSizeColumn(col, setAsMin);
8604 }
57c086ef
VZ
8605
8606 width += GetColWidth(col);
65e4e78e 8607 }
97a9929e 8608
faec5a43
SN
8609 if ( !calcOnly )
8610 EndBatch();
97a9929e 8611
266e8367 8612 return width;
65e4e78e
VZ
8613}
8614
266e8367 8615int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
8616{
8617 int height = m_colLabelHeight;
8618
faec5a43
SN
8619 if ( !calcOnly )
8620 BeginBatch();
97a9929e 8621
57c086ef
VZ
8622 for ( int row = 0; row < m_numRows; row++ )
8623 {
af547d51
VZ
8624 if ( !calcOnly )
8625 {
8626 AutoSizeRow(row, setAsMin);
8627 }
57c086ef
VZ
8628
8629 height += GetRowHeight(row);
8630 }
97a9929e 8631
faec5a43
SN
8632 if ( !calcOnly )
8633 EndBatch();
97a9929e 8634
266e8367 8635 return height;
57c086ef
VZ
8636}
8637
8638void wxGrid::AutoSize()
8639{
97a9929e
VZ
8640 BeginBatch();
8641
8642 wxSize size(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
8643
8644 // round up the size to a multiple of scroll step - this ensures that we
8645 // won't get the scrollbars if we're sized exactly to this width
8646 wxSize sizeFit(GetScrollX(size.x) * GRID_SCROLL_LINE_X,
8647 GetScrollY(size.y) * GRID_SCROLL_LINE_Y);
8648
8649 // distribute the extra space between teh columns/rows to avoid having
8650 // extra white space
8651 wxCoord diff = sizeFit.x - size.x;
8652 if ( diff )
8653 {
8654 // try to resize the columns uniformly
8655 wxCoord diffPerCol = diff / m_numCols;
8656 if ( diffPerCol )
8657 {
8658 for ( int col = 0; col < m_numCols; col++ )
8659 {
8660 SetColSize(col, GetColWidth(col) + diffPerCol);
8661 }
8662 }
8663
8664 // add remaining amount to the last columns
8665 diff -= diffPerCol * m_numCols;
8666 if ( diff )
8667 {
8668 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
8669 {
8670 SetColSize(col, GetColWidth(col) + 1);
8671 }
8672 }
8673 }
8674
8675 // same for rows
8676 diff = sizeFit.y - size.y;
8677 if ( diff )
8678 {
8679 // try to resize the columns uniformly
8680 wxCoord diffPerRow = diff / m_numRows;
8681 if ( diffPerRow )
8682 {
8683 for ( int row = 0; row < m_numRows; row++ )
8684 {
8685 SetRowSize(row, GetRowHeight(row) + diffPerRow);
8686 }
8687 }
8688
8689 // add remaining amount to the last rows
8690 diff -= diffPerRow * m_numRows;
8691 if ( diff )
8692 {
8693 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
8694 {
8695 SetRowSize(row, GetRowHeight(row) + 1);
8696 }
8697 }
8698 }
8699
8700 EndBatch();
8701
8702 SetClientSize(sizeFit);
266e8367
VZ
8703}
8704
8705wxSize wxGrid::DoGetBestSize() const
8706{
8707 // don't set sizes, only calculate them
8708 wxGrid *self = (wxGrid *)this; // const_cast
8709
8710 return wxSize(self->SetOrCalcColumnSizes(TRUE),
8711 self->SetOrCalcRowSizes(TRUE));
8712}
8713
8714void wxGrid::Fit()
8715{
8716 AutoSize();
57c086ef
VZ
8717}
8718
6fc0f38f
SN
8719
8720wxPen& wxGrid::GetDividerPen() const
8721{
8722 return wxNullPen;
8723}
8724
57c086ef
VZ
8725// ----------------------------------------------------------------------------
8726// cell value accessor functions
8727// ----------------------------------------------------------------------------
f85afd4e
MB
8728
8729void wxGrid::SetCellValue( int row, int col, const wxString& s )
8730{
8731 if ( m_table )
8732 {
f6bcfd97 8733 m_table->SetValue( row, col, s );
2d66e025
MB
8734 if ( !GetBatchCount() )
8735 {
8736 wxClientDC dc( m_gridWin );
8737 PrepareDC( dc );
8738 DrawCell( dc, wxGridCellCoords(row, col) );
8739 }
60ff3b99 8740
f85afd4e 8741 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 8742 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
8743 IsCellEditControlShown())
8744 // Note: If we are using IsCellEditControlEnabled,
8745 // this interacts badly with calling SetCellValue from
8746 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 8747 {
4cfa5de6
RD
8748 HideCellEditControl();
8749 ShowCellEditControl(); // will reread data from table
f85afd4e 8750 }
f85afd4e
MB
8751 }
8752}
8753
8754
8755//
2d66e025 8756// ------ Block, row and col selection
f85afd4e
MB
8757//
8758
8759void wxGrid::SelectRow( int row, bool addToSelected )
8760{
b5808881 8761 if ( IsSelection() && !addToSelected )
e32352cf 8762 ClearSelection();
70c7a608 8763
f6bcfd97 8764 m_selection->SelectRow( row, FALSE, addToSelected );
f85afd4e
MB
8765}
8766
8767
8768void wxGrid::SelectCol( int col, bool addToSelected )
8769{
b5808881 8770 if ( IsSelection() && !addToSelected )
e32352cf 8771 ClearSelection();
f85afd4e 8772
f6bcfd97 8773 m_selection->SelectCol( col, FALSE, addToSelected );
f85afd4e
MB
8774}
8775
8776
84912ef8 8777void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
c9097836 8778 bool addToSelected )
f85afd4e 8779{
c9097836
MB
8780 if ( IsSelection() && !addToSelected )
8781 ClearSelection();
f85afd4e 8782
84912ef8
RD
8783 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
8784 FALSE, addToSelected );
f85afd4e
MB
8785}
8786
c9097836 8787
f85afd4e
MB
8788void wxGrid::SelectAll()
8789{
f74d0b57
SN
8790 if ( m_numRows > 0 && m_numCols > 0 )
8791 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
b5808881 8792}
f85afd4e 8793
f7b4b343
VZ
8794//
8795// ------ Cell, row and col deselection
8796//
8797
8798void wxGrid::DeselectRow( int row )
8799{
8800 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
8801 {
8802 if ( m_selection->IsInSelection(row, 0 ) )
8803 m_selection->ToggleCellSelection( row, 0);
ffdd3c98 8804 }
f7b4b343
VZ
8805 else
8806 {
8807 int nCols = GetNumberCols();
8808 for ( int i = 0; i < nCols ; i++ )
8809 {
8810 if ( m_selection->IsInSelection(row, i ) )
8811 m_selection->ToggleCellSelection( row, i);
8812 }
8813 }
8814}
8815
8816void wxGrid::DeselectCol( int col )
8817{
8818 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
8819 {
8820 if ( m_selection->IsInSelection(0, col ) )
8821 m_selection->ToggleCellSelection( 0, col);
8822 }
8823 else
8824 {
8825 int nRows = GetNumberRows();
8826 for ( int i = 0; i < nRows ; i++ )
8827 {
8828 if ( m_selection->IsInSelection(i, col ) )
8829 m_selection->ToggleCellSelection(i, col);
8830 }
8831 }
8832}
8833
8834void wxGrid::DeselectCell( int row, int col )
8835{
8836 if ( m_selection->IsInSelection(row, col) )
8837 m_selection->ToggleCellSelection(row, col);
8838}
8839
b5808881
SN
8840bool wxGrid::IsSelection()
8841{
8842 return ( m_selection->IsSelection() ||
8843 ( m_selectingTopLeft != wxGridNoCellCoords &&
8844 m_selectingBottomRight != wxGridNoCellCoords ) );
f85afd4e
MB
8845}
8846
b5808881
SN
8847bool wxGrid::IsInSelection( int row, int col )
8848{
8849 return ( m_selection->IsInSelection( row, col ) ||
8850 ( row >= m_selectingTopLeft.GetRow() &&
8851 col >= m_selectingTopLeft.GetCol() &&
8852 row <= m_selectingBottomRight.GetRow() &&
8853 col <= m_selectingBottomRight.GetCol() ) );
8854}
f85afd4e
MB
8855
8856void wxGrid::ClearSelection()
8857{
b5808881
SN
8858 m_selectingTopLeft = wxGridNoCellCoords;
8859 m_selectingBottomRight = wxGridNoCellCoords;
8860 m_selection->ClearSelection();
8f177c8e 8861}
f85afd4e
MB
8862
8863
da6af900 8864// This function returns the rectangle that encloses the given block
2d66e025
MB
8865// in device coords clipped to the client size of the grid window.
8866//
58dd5b3b
MB
8867wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
8868 const wxGridCellCoords &bottomRight )
f85afd4e 8869{
58dd5b3b 8870 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
8871 wxRect cellRect;
8872
58dd5b3b
MB
8873 cellRect = CellToRect( topLeft );
8874 if ( cellRect != wxGridNoCellRect )
f85afd4e 8875 {
58dd5b3b
MB
8876 rect = cellRect;
8877 }
8878 else
8879 {
8880 rect = wxRect( 0, 0, 0, 0 );
8881 }
60ff3b99 8882
58dd5b3b
MB
8883 cellRect = CellToRect( bottomRight );
8884 if ( cellRect != wxGridNoCellRect )
8885 {
8886 rect += cellRect;
2d66e025
MB
8887 }
8888 else
8889 {
8890 return wxGridNoCellRect;
f85afd4e
MB
8891 }
8892
58dd5b3b
MB
8893 // convert to scrolled coords
8894 //
8895 int left, top, right, bottom;
8896 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
8897 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
8898
8899 int cw, ch;
8900 m_gridWin->GetClientSize( &cw, &ch );
8901
f6bcfd97
BP
8902 if (right < 0 || bottom < 0 || left > cw || top > ch)
8903 return wxRect( 0, 0, 0, 0);
8904
58dd5b3b
MB
8905 rect.SetLeft( wxMax(0, left) );
8906 rect.SetTop( wxMax(0, top) );
8907 rect.SetRight( wxMin(cw, right) );
8908 rect.SetBottom( wxMin(ch, bottom) );
8909
f85afd4e
MB
8910 return rect;
8911}
8912
8913
58dd5b3b 8914
f85afd4e
MB
8915//
8916// ------ Grid event classes
8917//
8918
bf7945ce 8919IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
8920
8921wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 8922 int row, int col, int x, int y, bool sel,
f85afd4e
MB
8923 bool control, bool shift, bool alt, bool meta )
8924 : wxNotifyEvent( type, id )
8925{
8926 m_row = row;
8927 m_col = col;
8928 m_x = x;
8929 m_y = y;
5c8fc7c1 8930 m_selecting = sel;
f85afd4e
MB
8931 m_control = control;
8932 m_shift = shift;
8933 m_alt = alt;
8934 m_meta = meta;
8f177c8e 8935
f85afd4e
MB
8936 SetEventObject(obj);
8937}
8938
8939
bf7945ce 8940IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
8941
8942wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
8943 int rowOrCol, int x, int y,
8944 bool control, bool shift, bool alt, bool meta )
8945 : wxNotifyEvent( type, id )
8946{
8947 m_rowOrCol = rowOrCol;
8948 m_x = x;
8949 m_y = y;
8950 m_control = control;
8951 m_shift = shift;
8952 m_alt = alt;
8953 m_meta = meta;
8f177c8e 8954
f85afd4e
MB
8955 SetEventObject(obj);
8956}
8957
8958
bf7945ce 8959IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
8960
8961wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
8962 const wxGridCellCoords& topLeft,
8963 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
8964 bool sel, bool control,
8965 bool shift, bool alt, bool meta )
8f177c8e 8966 : wxNotifyEvent( type, id )
f85afd4e
MB
8967{
8968 m_topLeft = topLeft;
8969 m_bottomRight = bottomRight;
5c8fc7c1 8970 m_selecting = sel;
f85afd4e
MB
8971 m_control = control;
8972 m_shift = shift;
8973 m_alt = alt;
8974 m_meta = meta;
8975
8976 SetEventObject(obj);
8977}
8978
8979
bf7945ce
RD
8980IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
8981
8982wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
8983 wxObject* obj, int row,
8984 int col, wxControl* ctrl)
8985 : wxCommandEvent(type, id)
8986{
8987 SetEventObject(obj);
8988 m_row = row;
8989 m_col = col;
8990 m_ctrl = ctrl;
8991}
8992
8993
27b92ca4
VZ
8994#endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
8995
8996#endif // wxUSE_GRID
8997