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