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