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