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