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