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