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