]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
added common notebookbase source file
[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{
606b005f
JS
3506 // Must do this or ~wxScrollHelper will pop the wrong event handler
3507 SetTargetWindow(this);
0a976765 3508 ClearAttrCache();
39bcce60 3509 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
3510
3511#ifdef DEBUG_ATTR_CACHE
3512 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3513 wxPrintf(_T("wxGrid attribute cache statistics: "
3514 "total: %u, hits: %u (%u%%)\n"),
3515 total, gs_nAttrCacheHits,
3516 total ? (gs_nAttrCacheHits*100) / total : 0);
3517#endif
3518
2796cce3
RD
3519 if (m_ownTable)
3520 delete m_table;
f2d76237
RD
3521
3522 delete m_typeRegistry;
b5808881 3523 delete m_selection;
58dd5b3b
MB
3524}
3525
2d66e025 3526
58dd5b3b
MB
3527//
3528// ----- internal init and update functions
3529//
3530
3531void wxGrid::Create()
f0102d2a 3532{
4634a5d6 3533 m_created = FALSE; // set to TRUE by CreateGrid
4634a5d6
MB
3534
3535 m_table = (wxGridTableBase *) NULL;
2796cce3 3536 m_ownTable = FALSE;
2c9a89e0
RD
3537
3538 m_cellEditCtrlEnabled = FALSE;
4634a5d6 3539
2796cce3
RD
3540 m_defaultCellAttr = new wxGridCellAttr;
3541 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
f2d76237
RD
3542
3543 // Set default cell attributes
19d7140e 3544 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 3545 m_defaultCellAttr->SetFont(GetFont());
4c7277db 3546 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
f2d76237
RD
3547 m_defaultCellAttr->SetTextColour(
3548 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
3549 m_defaultCellAttr->SetBackgroundColour(
3550 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
3551 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3552 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2796cce3
RD
3553
3554
4634a5d6
MB
3555 m_numRows = 0;
3556 m_numCols = 0;
3557 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 3558
18f9565d
MB
3559 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3560 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2d66e025 3561
c4608a8a 3562 // create the type registry
f2d76237 3563 m_typeRegistry = new wxGridTypeRegistry;
b5808881 3564 m_selection = 0;
f2d76237 3565 // subwindow components that make up the wxGrid
7807d81c
MB
3566 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3567 -1,
18f9565d
MB
3568 wxDefaultPosition,
3569 wxDefaultSize );
7807d81c 3570
60ff3b99
VZ
3571 m_rowLabelWin = new wxGridRowLabelWindow( this,
3572 -1,
18f9565d
MB
3573 wxDefaultPosition,
3574 wxDefaultSize );
2d66e025
MB
3575
3576 m_colLabelWin = new wxGridColLabelWindow( this,
3577 -1,
18f9565d
MB
3578 wxDefaultPosition,
3579 wxDefaultSize );
60ff3b99 3580
60ff3b99
VZ
3581 m_gridWin = new wxGridWindow( this,
3582 m_rowLabelWin,
3583 m_colLabelWin,
3584 -1,
18f9565d 3585 wxDefaultPosition,
2d66e025
MB
3586 wxDefaultSize );
3587
3588 SetTargetWindow( m_gridWin );
2d66e025 3589}
f85afd4e 3590
2d66e025 3591
b5808881
SN
3592bool wxGrid::CreateGrid( int numRows, int numCols,
3593 wxGrid::wxGridSelectionModes selmode )
2d66e025 3594{
f6bcfd97
BP
3595 wxCHECK_MSG( !m_created,
3596 FALSE,
3597 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3598
3599 m_numRows = numRows;
3600 m_numCols = numCols;
3601
3602 m_table = new wxGridStringTable( m_numRows, m_numCols );
3603 m_table->SetView( this );
3604 m_ownTable = TRUE;
3605 m_selection = new wxGridSelection( this, selmode );
3606 Init();
3607 m_created = TRUE;
2d66e025 3608
2796cce3
RD
3609 return m_created;
3610}
3611
f1567cdd
SN
3612void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
3613{
3614 if ( !m_created )
3615 {
3616 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3617 }
3618 else
3619 m_selection->SetSelectionMode( selmode );
3620}
3621
043d16b2
SN
3622bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
3623 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
3624{
3625 if ( m_created )
3626 {
fb295790
RD
3627 // RD: Actually, this should probably be allowed. I think it would be
3628 // nice to be able to switch multiple Tables in and out of a single
3629 // View at runtime. Is there anything in the implmentation that would
3630 // prevent this?
3631
d95b0c2b 3632 // At least, you now have to cope with m_selection
2796cce3
RD
3633 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3634 return FALSE;
3635 }
3636 else
3637 {
3638 m_numRows = table->GetNumberRows();
3639 m_numCols = table->GetNumberCols();
3640
3641 m_table = table;
3642 m_table->SetView( this );
3643 if (takeOwnership)
3644 m_ownTable = TRUE;
043d16b2 3645 m_selection = new wxGridSelection( this, selmode );
f6bcfd97 3646 Init();
2d66e025
MB
3647 m_created = TRUE;
3648 }
f85afd4e 3649
2d66e025 3650 return m_created;
f85afd4e
MB
3651}
3652
2d66e025 3653
f85afd4e
MB
3654void wxGrid::Init()
3655{
f85afd4e
MB
3656 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3657 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3658
60ff3b99
VZ
3659 if ( m_rowLabelWin )
3660 {
3661 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
3662 }
3663 else
3664 {
3665 m_labelBackgroundColour = wxColour( _T("WHITE") );
3666 }
3667
3668 m_labelTextColour = wxColour( _T("BLACK") );
f85afd4e 3669
0a976765
VZ
3670 // init attr cache
3671 m_attrCache.row = -1;
3672
f85afd4e
MB
3673 // TODO: something better than this ?
3674 //
3675 m_labelFont = this->GetFont();
3676 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
8f177c8e 3677
4c7277db
MB
3678 m_rowLabelHorizAlign = wxALIGN_LEFT;
3679 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 3680
4c7277db
MB
3681 m_colLabelHorizAlign = wxALIGN_CENTRE;
3682 m_colLabelVertAlign = wxALIGN_TOP;
f85afd4e 3683
f85afd4e 3684 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
3685 m_defaultRowHeight = m_gridWin->GetCharHeight();
3686
d2fdd8d2 3687#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
3688 m_defaultRowHeight += 8;
3689#else
3690 m_defaultRowHeight += 4;
3691#endif
3692
2d66e025 3693 m_gridLineColour = wxColour( 128, 128, 255 );
f85afd4e 3694 m_gridLinesEnabled = TRUE;
f6bcfd97 3695 m_cellHighlightColour = m_gridLineColour;
bf7945ce 3696 m_cellHighlightPenWidth = 2;
d2520c85 3697 m_cellHighlightROPenWidth = 1;
8f177c8e 3698
58dd5b3b 3699 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 3700 m_winCapture = (wxWindow *)NULL;
6e8524b1
MB
3701 m_canDragRowSize = TRUE;
3702 m_canDragColSize = TRUE;
4cfa5de6 3703 m_canDragGridSize = TRUE;
f85afd4e
MB
3704 m_dragLastPos = -1;
3705 m_dragRowOrCol = -1;
3706 m_isDragging = FALSE;
07296f0b 3707 m_startDragPos = wxDefaultPosition;
f85afd4e 3708
07296f0b 3709 m_waitForSlowClick = FALSE;
025562fe 3710
f85afd4e
MB
3711 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
3712 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
3713
3714 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 3715
b5808881
SN
3716 m_selectingTopLeft = wxGridNoCellCoords;
3717 m_selectingBottomRight = wxGridNoCellCoords;
2796cce3
RD
3718 m_selectionBackground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
3719 m_selectionForeground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 3720
f85afd4e
MB
3721 m_editable = TRUE; // default for whole grid
3722
2d66e025
MB
3723 m_inOnKeyDown = FALSE;
3724 m_batchCount = 0;
3c79cf49 3725
266e8367
VZ
3726 m_extraWidth =
3727 m_extraHeight = 50;
3728
3c79cf49 3729 CalcDimensions();
7c1cb261
VZ
3730}
3731
3732// ----------------------------------------------------------------------------
3733// the idea is to call these functions only when necessary because they create
3734// quite big arrays which eat memory mostly unnecessary - in particular, if
3735// default widths/heights are used for all rows/columns, we may not use these
3736// arrays at all
3737//
3738// with some extra code, it should be possible to only store the
3739// widths/heights different from default ones but this will be done later...
3740// ----------------------------------------------------------------------------
3741
3742void wxGrid::InitRowHeights()
3743{
3744 m_rowHeights.Empty();
3745 m_rowBottoms.Empty();
3746
3747 m_rowHeights.Alloc( m_numRows );
3748 m_rowBottoms.Alloc( m_numRows );
3749
3750 int rowBottom = 0;
3751 for ( int i = 0; i < m_numRows; i++ )
3752 {
3753 m_rowHeights.Add( m_defaultRowHeight );
3754 rowBottom += m_defaultRowHeight;
3755 m_rowBottoms.Add( rowBottom );
3756 }
3757}
3758
3759void wxGrid::InitColWidths()
3760{
3761 m_colWidths.Empty();
3762 m_colRights.Empty();
3763
3764 m_colWidths.Alloc( m_numCols );
3765 m_colRights.Alloc( m_numCols );
3766 int colRight = 0;
3767 for ( int i = 0; i < m_numCols; i++ )
3768 {
3769 m_colWidths.Add( m_defaultColWidth );
3770 colRight += m_defaultColWidth;
3771 m_colRights.Add( colRight );
3772 }
3773}
3774
3775int wxGrid::GetColWidth(int col) const
3776{
3777 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
3778}
3779
3780int wxGrid::GetColLeft(int col) const
3781{
3782 return m_colRights.IsEmpty() ? col * m_defaultColWidth
3783 : m_colRights[col] - m_colWidths[col];
3784}
3785
3786int wxGrid::GetColRight(int col) const
3787{
3788 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3789 : m_colRights[col];
3790}
3791
3792int wxGrid::GetRowHeight(int row) const
3793{
3794 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3795}
2d66e025 3796
7c1cb261
VZ
3797int wxGrid::GetRowTop(int row) const
3798{
3799 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3800 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
3801}
3802
7c1cb261
VZ
3803int wxGrid::GetRowBottom(int row) const
3804{
3805 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3806 : m_rowBottoms[row];
3807}
f85afd4e
MB
3808
3809void wxGrid::CalcDimensions()
3810{
f85afd4e 3811 int cw, ch;
2d66e025 3812 GetClientSize( &cw, &ch );
f85afd4e 3813
faec5a43
SN
3814 if ( m_colLabelWin->IsShown() )
3815 cw -= m_rowLabelWidth;
3816 if ( m_rowLabelWin->IsShown() )
3817 ch -= m_colLabelHeight;
60ff3b99 3818
faec5a43
SN
3819 // grid total size
3820 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
3821 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
3822
3823 // preserve (more or less) the previous position
3824 int x, y;
3825 GetViewStart( &x, &y );
3826 // maybe we don't need scrollbars at all? and if we do, transform w and h
3827 // from pixels into logical units
3828 if ( w <= cw )
3829 {
3830 w = 0; x= 0;
3831 }
3832 else
3833 {
3834 w = (w + GRID_SCROLL_LINE - 1)/GRID_SCROLL_LINE;
3835 if ( x >= w )
3836 x = w - 1;
f85afd4e 3837 }
faec5a43
SN
3838 if ( h <= ch )
3839 {
3840 h = 0; y = 0;
3841 }
3842 else
3843 {
3844 h = (h + GRID_SCROLL_LINE - 1)/GRID_SCROLL_LINE;
3845 if ( y >= h )
3846 y = h - 1;
3847 }
3848
3849 // do set scrollbar parameters
3850 SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE,
389b961e 3851 w, h, x, y, (GetBatchCount() != 0));
f85afd4e
MB
3852}
3853
3854
7807d81c
MB
3855void wxGrid::CalcWindowSizes()
3856{
3857 int cw, ch;
3858 GetClientSize( &cw, &ch );
b99be8fb 3859
7807d81c
MB
3860 if ( m_cornerLabelWin->IsShown() )
3861 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3862
3863 if ( m_colLabelWin->IsShown() )
3864 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3865
3866 if ( m_rowLabelWin->IsShown() )
3867 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3868
3869 if ( m_gridWin->IsShown() )
3870 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3871}
3872
3873
f85afd4e
MB
3874// this is called when the grid table sends a message to say that it
3875// has been redimensioned
3876//
3877bool wxGrid::Redimension( wxGridTableMessage& msg )
3878{
3879 int i;
f6bcfd97 3880 bool result = FALSE;
8f177c8e 3881
f6bcfd97 3882#if 0
7c1cb261
VZ
3883 // if we were using the default widths/heights so far, we must change them
3884 // now
3885 if ( m_colWidths.IsEmpty() )
3886 {
3887 InitColWidths();
3888 }
3889
3890 if ( m_rowHeights.IsEmpty() )
3891 {
3892 InitRowHeights();
3893 }
f6bcfd97 3894#endif
7c1cb261 3895
f85afd4e
MB
3896 switch ( msg.GetId() )
3897 {
3898 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3899 {
3900 size_t pos = msg.GetCommandInt();
3901 int numRows = msg.GetCommandInt2();
f6bcfd97 3902
f85afd4e 3903 m_numRows += numRows;
2d66e025 3904
f6bcfd97
BP
3905 if ( !m_rowHeights.IsEmpty() )
3906 {
3907 for ( i = 0; i < numRows; i++ )
3908 {
3909 m_rowHeights.Insert( m_defaultRowHeight, pos );
3910 m_rowBottoms.Insert( 0, pos );
3911 }
3912
3913 int bottom = 0;
3914 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
60ff3b99 3915
f6bcfd97
BP
3916 for ( i = pos; i < m_numRows; i++ )
3917 {
3918 bottom += m_rowHeights[i];
3919 m_rowBottoms[i] = bottom;
3920 }
3921 }
3922 if ( m_currentCellCoords == wxGridNoCellCoords )
3923 {
3924 // if we have just inserted cols into an empty grid the current
3925 // cell will be undefined...
3926 //
3927 SetCurrentCell( 0, 0 );
3928 }
3929 m_selection->UpdateRows( pos, numRows );
3930 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
3931 if (attrProvider)
3932 attrProvider->UpdateAttrRows( pos, numRows );
3933
3934 if ( !GetBatchCount() )
2d66e025 3935 {
f6bcfd97
BP
3936 CalcDimensions();
3937 m_rowLabelWin->Refresh();
2d66e025 3938 }
f85afd4e 3939 }
f6bcfd97
BP
3940 result = TRUE;
3941 break;
f85afd4e
MB
3942
3943 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3944 {
3945 int numRows = msg.GetCommandInt();
2d66e025 3946 int oldNumRows = m_numRows;
f85afd4e 3947 m_numRows += numRows;
2d66e025 3948
f6bcfd97
BP
3949 if ( !m_rowHeights.IsEmpty() )
3950 {
3951 for ( i = 0; i < numRows; i++ )
3952 {
3953 m_rowHeights.Add( m_defaultRowHeight );
3954 m_rowBottoms.Add( 0 );
3955 }
60ff3b99 3956
f6bcfd97
BP
3957 int bottom = 0;
3958 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
3959
3960 for ( i = oldNumRows; i < m_numRows; i++ )
3961 {
3962 bottom += m_rowHeights[i];
3963 m_rowBottoms[i] = bottom;
3964 }
3965 }
3966 if ( m_currentCellCoords == wxGridNoCellCoords )
3967 {
3968 // if we have just inserted cols into an empty grid the current
3969 // cell will be undefined...
3970 //
3971 SetCurrentCell( 0, 0 );
3972 }
3973 if ( !GetBatchCount() )
2d66e025 3974 {
f6bcfd97
BP
3975 CalcDimensions();
3976 m_rowLabelWin->Refresh();
2d66e025 3977 }
f85afd4e 3978 }
f6bcfd97
BP
3979 result = TRUE;
3980 break;
f85afd4e
MB
3981
3982 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
3983 {
3984 size_t pos = msg.GetCommandInt();
3985 int numRows = msg.GetCommandInt2();
f85afd4e
MB
3986 m_numRows -= numRows;
3987
f6bcfd97 3988 if ( !m_rowHeights.IsEmpty() )
f85afd4e 3989 {
f6bcfd97
BP
3990 for ( i = 0; i < numRows; i++ )
3991 {
3992 m_rowHeights.Remove( pos );
3993 m_rowBottoms.Remove( pos );
3994 }
2d66e025
MB
3995
3996 int h = 0;
3997 for ( i = 0; i < m_numRows; i++ )
3998 {
3999 h += m_rowHeights[i];
4000 m_rowBottoms[i] = h;
4001 }
f85afd4e 4002 }
f6bcfd97
BP
4003 if ( !m_numRows )
4004 {
4005 m_currentCellCoords = wxGridNoCellCoords;
4006 }
4007 else
4008 {
4009 if ( m_currentCellCoords.GetRow() >= m_numRows )
4010 m_currentCellCoords.Set( 0, 0 );
4011 }
4012 m_selection->UpdateRows( pos, -((int)numRows) );
4013 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4014 if (attrProvider) {
4015 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
84912ef8
RD
4016// ifdef'd out following patch from Paul Gammans
4017#if 0
3ca6a5f0 4018 // No need to touch column attributes, unless we
f6bcfd97
BP
4019 // removed _all_ rows, in this case, we remove
4020 // all column attributes.
4021 // I hate to do this here, but the
4022 // needed data is not available inside UpdateAttrRows.
4023 if ( !GetNumberRows() )
4024 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 4025#endif
f6bcfd97
BP
4026 }
4027 if ( !GetBatchCount() )
4028 {
4029 CalcDimensions();
4030 m_rowLabelWin->Refresh();
4031 }
f85afd4e 4032 }
f6bcfd97
BP
4033 result = TRUE;
4034 break;
f85afd4e
MB
4035
4036 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4037 {
4038 size_t pos = msg.GetCommandInt();
4039 int numCols = msg.GetCommandInt2();
f85afd4e 4040 m_numCols += numCols;
2d66e025 4041
f6bcfd97
BP
4042 if ( !m_colWidths.IsEmpty() )
4043 {
4044 for ( i = 0; i < numCols; i++ )
4045 {
4046 m_colWidths.Insert( m_defaultColWidth, pos );
4047 m_colRights.Insert( 0, pos );
4048 }
4049
4050 int right = 0;
4051 if ( pos > 0 ) right = m_colRights[pos-1];
60ff3b99 4052
f6bcfd97
BP
4053 for ( i = pos; i < m_numCols; i++ )
4054 {
4055 right += m_colWidths[i];
4056 m_colRights[i] = right;
4057 }
4058 }
4059 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4060 {
f6bcfd97
BP
4061 // if we have just inserted cols into an empty grid the current
4062 // cell will be undefined...
4063 //
4064 SetCurrentCell( 0, 0 );
2d66e025 4065 }
f6bcfd97
BP
4066 m_selection->UpdateCols( pos, numCols );
4067 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4068 if (attrProvider)
4069 attrProvider->UpdateAttrCols( pos, numCols );
4070 if ( !GetBatchCount() )
4071 {
4072 CalcDimensions();
4073 m_colLabelWin->Refresh();
4074 }
4075
f85afd4e 4076 }
f6bcfd97
BP
4077 result = TRUE;
4078 break;
f85afd4e
MB
4079
4080 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4081 {
4082 int numCols = msg.GetCommandInt();
2d66e025 4083 int oldNumCols = m_numCols;
f85afd4e 4084 m_numCols += numCols;
f6bcfd97
BP
4085 if ( !m_colWidths.IsEmpty() )
4086 {
4087 for ( i = 0; i < numCols; i++ )
4088 {
4089 m_colWidths.Add( m_defaultColWidth );
4090 m_colRights.Add( 0 );
4091 }
2d66e025 4092
f6bcfd97
BP
4093 int right = 0;
4094 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
60ff3b99 4095
f6bcfd97
BP
4096 for ( i = oldNumCols; i < m_numCols; i++ )
4097 {
4098 right += m_colWidths[i];
4099 m_colRights[i] = right;
4100 }
4101 }
4102 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4103 {
f6bcfd97
BP
4104 // if we have just inserted cols into an empty grid the current
4105 // cell will be undefined...
4106 //
4107 SetCurrentCell( 0, 0 );
4108 }
4109 if ( !GetBatchCount() )
4110 {
4111 CalcDimensions();
4112 m_colLabelWin->Refresh();
2d66e025 4113 }
f85afd4e 4114 }
f6bcfd97
BP
4115 result = TRUE;
4116 break;
f85afd4e
MB
4117
4118 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4119 {
4120 size_t pos = msg.GetCommandInt();
4121 int numCols = msg.GetCommandInt2();
f85afd4e 4122 m_numCols -= numCols;
f85afd4e 4123
f6bcfd97 4124 if ( !m_colWidths.IsEmpty() )
f85afd4e 4125 {
f6bcfd97
BP
4126 for ( i = 0; i < numCols; i++ )
4127 {
4128 m_colWidths.Remove( pos );
4129 m_colRights.Remove( pos );
4130 }
2d66e025
MB
4131
4132 int w = 0;
4133 for ( i = 0; i < m_numCols; i++ )
4134 {
4135 w += m_colWidths[i];
4136 m_colRights[i] = w;
4137 }
f85afd4e 4138 }
f6bcfd97
BP
4139 if ( !m_numCols )
4140 {
4141 m_currentCellCoords = wxGridNoCellCoords;
4142 }
4143 else
4144 {
4145 if ( m_currentCellCoords.GetCol() >= m_numCols )
4146 m_currentCellCoords.Set( 0, 0 );
4147 }
4148 m_selection->UpdateCols( pos, -((int)numCols) );
4149 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4150 if (attrProvider) {
4151 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
84912ef8
RD
4152// ifdef'd out following patch from Paul Gammans
4153#if 0
f6bcfd97
BP
4154 // No need to touch row attributes, unless we
4155 // removed _all_ columns, in this case, we remove
4156 // all row attributes.
4157 // I hate to do this here, but the
4158 // needed data is not available inside UpdateAttrCols.
4159 if ( !GetNumberCols() )
4160 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 4161#endif
f6bcfd97
BP
4162 }
4163 if ( !GetBatchCount() )
4164 {
4165 CalcDimensions();
4166 m_colLabelWin->Refresh();
4167 }
f85afd4e 4168 }
faec5a43 4169 result = TRUE;
f6bcfd97 4170 break;
f85afd4e
MB
4171 }
4172
f6bcfd97
BP
4173 if (result && !GetBatchCount() )
4174 m_gridWin->Refresh();
4175 return result;
f85afd4e
MB
4176}
4177
4178
d10f4bf9 4179wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
f85afd4e 4180{
2d66e025
MB
4181 wxRegionIterator iter( reg );
4182 wxRect r;
f85afd4e 4183
d10f4bf9
VZ
4184 wxArrayInt rowlabels;
4185
2d66e025
MB
4186 int top, bottom;
4187 while ( iter )
f85afd4e 4188 {
2d66e025 4189 r = iter.GetRect();
f85afd4e 4190
2d66e025
MB
4191 // TODO: remove this when we can...
4192 // There is a bug in wxMotif that gives garbage update
4193 // rectangles if you jump-scroll a long way by clicking the
4194 // scrollbar with middle button. This is a work-around
4195 //
4196#if defined(__WXMOTIF__)
4197 int cw, ch;
4198 m_gridWin->GetClientSize( &cw, &ch );
4199 if ( r.GetTop() > ch ) r.SetTop( 0 );
4200 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4201#endif
f85afd4e 4202
2d66e025
MB
4203 // logical bounds of update region
4204 //
4205 int dummy;
4206 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4207 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4208
4209 // find the row labels within these bounds
4210 //
4211 int row;
2d66e025
MB
4212 for ( row = 0; row < m_numRows; row++ )
4213 {
7c1cb261
VZ
4214 if ( GetRowBottom(row) < top )
4215 continue;
2d66e025 4216
6d55126d 4217 if ( GetRowTop(row) > bottom )
7c1cb261 4218 break;
60ff3b99 4219
d10f4bf9 4220 rowlabels.Add( row );
2d66e025 4221 }
60ff3b99 4222
2d66e025 4223 iter++ ;
f85afd4e 4224 }
d10f4bf9
VZ
4225
4226 return rowlabels;
f85afd4e
MB
4227}
4228
4229
d10f4bf9 4230wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
f85afd4e 4231{
2d66e025
MB
4232 wxRegionIterator iter( reg );
4233 wxRect r;
f85afd4e 4234
d10f4bf9 4235 wxArrayInt colLabels;
f85afd4e 4236
2d66e025
MB
4237 int left, right;
4238 while ( iter )
f85afd4e 4239 {
2d66e025 4240 r = iter.GetRect();
f85afd4e 4241
2d66e025
MB
4242 // TODO: remove this when we can...
4243 // There is a bug in wxMotif that gives garbage update
4244 // rectangles if you jump-scroll a long way by clicking the
4245 // scrollbar with middle button. This is a work-around
4246 //
4247#if defined(__WXMOTIF__)
4248 int cw, ch;
4249 m_gridWin->GetClientSize( &cw, &ch );
4250 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4251 r.SetRight( wxMin( r.GetRight(), cw ) );
4252#endif
4253
4254 // logical bounds of update region
4255 //
4256 int dummy;
4257 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4258 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4259
4260 // find the cells within these bounds
4261 //
4262 int col;
2d66e025
MB
4263 for ( col = 0; col < m_numCols; col++ )
4264 {
7c1cb261
VZ
4265 if ( GetColRight(col) < left )
4266 continue;
60ff3b99 4267
7c1cb261
VZ
4268 if ( GetColLeft(col) > right )
4269 break;
2d66e025 4270
d10f4bf9 4271 colLabels.Add( col );
2d66e025 4272 }
60ff3b99 4273
2d66e025 4274 iter++ ;
f85afd4e 4275 }
d10f4bf9 4276 return colLabels;
f85afd4e
MB
4277}
4278
4279
d10f4bf9 4280wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
f85afd4e 4281{
2d66e025
MB
4282 wxRegionIterator iter( reg );
4283 wxRect r;
f85afd4e 4284
d10f4bf9 4285 wxGridCellCoordsArray cellsExposed;
f85afd4e 4286
2d66e025
MB
4287 int left, top, right, bottom;
4288 while ( iter )
4289 {
4290 r = iter.GetRect();
f85afd4e 4291
2d66e025
MB
4292 // TODO: remove this when we can...
4293 // There is a bug in wxMotif that gives garbage update
4294 // rectangles if you jump-scroll a long way by clicking the
4295 // scrollbar with middle button. This is a work-around
4296 //
4297#if defined(__WXMOTIF__)
f85afd4e 4298 int cw, ch;
2d66e025
MB
4299 m_gridWin->GetClientSize( &cw, &ch );
4300 if ( r.GetTop() > ch ) r.SetTop( 0 );
4301 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4302 r.SetRight( wxMin( r.GetRight(), cw ) );
4303 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4304#endif
8f177c8e 4305
2d66e025
MB
4306 // logical bounds of update region
4307 //
4308 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
4309 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 4310
2d66e025 4311 // find the cells within these bounds
f85afd4e 4312 //
2d66e025 4313 int row, col;
2d66e025 4314 for ( row = 0; row < m_numRows; row++ )
f85afd4e 4315 {
7c1cb261
VZ
4316 if ( GetRowBottom(row) <= top )
4317 continue;
f85afd4e 4318
7c1cb261
VZ
4319 if ( GetRowTop(row) > bottom )
4320 break;
60ff3b99 4321
f85afd4e 4322
2d66e025
MB
4323 for ( col = 0; col < m_numCols; col++ )
4324 {
7c1cb261
VZ
4325 if ( GetColRight(col) <= left )
4326 continue;
60ff3b99 4327
7c1cb261
VZ
4328 if ( GetColLeft(col) > right )
4329 break;
60ff3b99 4330
d10f4bf9 4331 cellsExposed.Add( wxGridCellCoords( row, col ) );
2d66e025
MB
4332 }
4333 }
60ff3b99 4334
7c1cb261 4335 iter++;
f85afd4e 4336 }
d10f4bf9
VZ
4337
4338 return cellsExposed;
f85afd4e
MB
4339}
4340
4341
2d66e025 4342void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 4343{
2d66e025
MB
4344 int x, y, row;
4345 wxPoint pos( event.GetPosition() );
4346 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4347
2d66e025 4348 if ( event.Dragging() )
f85afd4e
MB
4349 {
4350 m_isDragging = TRUE;
8f177c8e 4351
2d66e025 4352 if ( event.LeftIsDown() )
f85afd4e
MB
4353 {
4354 switch( m_cursorMode )
4355 {
f85afd4e
MB
4356 case WXGRID_CURSOR_RESIZE_ROW:
4357 {
2d66e025
MB
4358 int cw, ch, left, dummy;
4359 m_gridWin->GetClientSize( &cw, &ch );
4360 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 4361
2d66e025
MB
4362 wxClientDC dc( m_gridWin );
4363 PrepareDC( dc );
af547d51
VZ
4364 y = wxMax( y,
4365 GetRowTop(m_dragRowOrCol) +
4366 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 4367 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
4368 if ( m_dragLastPos >= 0 )
4369 {
2d66e025 4370 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 4371 }
2d66e025
MB
4372 dc.DrawLine( left, y, left+cw, y );
4373 m_dragLastPos = y;
f85afd4e
MB
4374 }
4375 break;
4376
4377 case WXGRID_CURSOR_SELECT_ROW:
e32352cf 4378 if ( (row = YToRow( y )) >= 0 )
aa5e1f75
SN
4379 {
4380 m_selection->SelectRow( row,
4381 event.ControlDown(),
4382 event.ShiftDown(),
4383 event.AltDown(),
4384 event.MetaDown() );
f85afd4e 4385 }
e2b42eeb
VZ
4386
4387 // default label to suppress warnings about "enumeration value
4388 // 'xxx' not handled in switch
4389 default:
4390 break;
f85afd4e
MB
4391 }
4392 }
4393 return;
4394 }
4395
4396 m_isDragging = FALSE;
8f177c8e 4397
60ff3b99 4398
6d004f67
MB
4399 // ------------ Entering or leaving the window
4400 //
4401 if ( event.Entering() || event.Leaving() )
4402 {
e2b42eeb 4403 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
4404 }
4405
e2b42eeb 4406
2d66e025 4407 // ------------ Left button pressed
f85afd4e 4408 //
6d004f67 4409 else if ( event.LeftDown() )
f85afd4e 4410 {
2d66e025
MB
4411 // don't send a label click event for a hit on the
4412 // edge of the row label - this is probably the user
4413 // wanting to resize the row
4414 //
4415 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 4416 {
2d66e025 4417 row = YToRow(y);
58dd5b3b 4418 if ( row >= 0 &&
b54ba671 4419 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 4420 {
aa5e1f75
SN
4421 if ( !event.ShiftDown() && !event.ControlDown() )
4422 ClearSelection();
4423 if ( event.ShiftDown() )
4424 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4425 0,
4426 row,
4427 GetNumberCols() - 1,
4428 event.ControlDown(),
4429 event.ShiftDown(),
4430 event.AltDown(),
4431 event.MetaDown() );
4432 else
4433 m_selection->SelectRow( row,
4434 event.ControlDown(),
4435 event.ShiftDown(),
4436 event.AltDown(),
4437 event.MetaDown() );
e2b42eeb 4438 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 4439 }
2d66e025
MB
4440 }
4441 else
4442 {
4443 // starting to drag-resize a row
4444 //
6e8524b1
MB
4445 if ( CanDragRowSize() )
4446 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
4447 }
4448 }
f85afd4e 4449
f85afd4e 4450
2d66e025
MB
4451 // ------------ Left double click
4452 //
4453 else if (event.LeftDClick() )
4454 {
4455 if ( YToEdgeOfRow(y) < 0 )
4456 {
4457 row = YToRow(y);
b54ba671 4458 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
f85afd4e
MB
4459 }
4460 }
60ff3b99
VZ
4461
4462
2d66e025 4463 // ------------ Left button released
f85afd4e 4464 //
2d66e025 4465 else if ( event.LeftUp() )
f85afd4e 4466 {
2d66e025 4467 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 4468 {
6d004f67 4469 DoEndDragResizeRow();
60ff3b99 4470
6d004f67
MB
4471 // Note: we are ending the event *after* doing
4472 // default processing in this case
4473 //
b54ba671 4474 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 4475 }
f85afd4e 4476
e2b42eeb
VZ
4477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4478 m_dragLastPos = -1;
2d66e025 4479 }
f85afd4e 4480
f85afd4e 4481
2d66e025
MB
4482 // ------------ Right button down
4483 //
4484 else if ( event.RightDown() )
4485 {
4486 row = YToRow(y);
b54ba671 4487 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
4488 {
4489 // no default action at the moment
f85afd4e
MB
4490 }
4491 }
60ff3b99
VZ
4492
4493
2d66e025 4494 // ------------ Right double click
f85afd4e 4495 //
2d66e025
MB
4496 else if ( event.RightDClick() )
4497 {
4498 row = YToRow(y);
b54ba671 4499 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
4500 {
4501 // no default action at the moment
4502 }
4503 }
60ff3b99
VZ
4504
4505
2d66e025 4506 // ------------ No buttons down and mouse moving
f85afd4e 4507 //
2d66e025 4508 else if ( event.Moving() )
f85afd4e 4509 {
2d66e025
MB
4510 m_dragRowOrCol = YToEdgeOfRow( y );
4511 if ( m_dragRowOrCol >= 0 )
8f177c8e 4512 {
2d66e025 4513 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 4514 {
e2b42eeb 4515 // don't capture the mouse yet
6e8524b1
MB
4516 if ( CanDragRowSize() )
4517 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
8f177c8e 4518 }
2d66e025 4519 }
6d004f67 4520 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 4521 {
e2b42eeb 4522 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
8f177c8e 4523 }
f85afd4e 4524 }
2d66e025
MB
4525}
4526
4527
4528void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
4529{
4530 int x, y, col;
4531 wxPoint pos( event.GetPosition() );
4532 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4533
2d66e025 4534 if ( event.Dragging() )
f85afd4e 4535 {
2d66e025 4536 m_isDragging = TRUE;
8f177c8e 4537
2d66e025 4538 if ( event.LeftIsDown() )
8f177c8e 4539 {
2d66e025 4540 switch( m_cursorMode )
8f177c8e 4541 {
2d66e025 4542 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 4543 {
2d66e025
MB
4544 int cw, ch, dummy, top;
4545 m_gridWin->GetClientSize( &cw, &ch );
4546 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 4547
2d66e025
MB
4548 wxClientDC dc( m_gridWin );
4549 PrepareDC( dc );
43947979
VZ
4550
4551 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4552 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 4553 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
4554 if ( m_dragLastPos >= 0 )
4555 {
4556 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4557 }
4558 dc.DrawLine( x, top, x, top+ch );
4559 m_dragLastPos = x;
f85afd4e 4560 }
2d66e025 4561 break;
f85afd4e 4562
2d66e025 4563 case WXGRID_CURSOR_SELECT_COL:
e32352cf 4564 if ( (col = XToCol( x )) >= 0 )
aa5e1f75
SN
4565 {
4566 m_selection->SelectCol( col,
4567 event.ControlDown(),
4568 event.ShiftDown(),
4569 event.AltDown(),
4570 event.MetaDown() );
2d66e025 4571 }
e2b42eeb
VZ
4572
4573 // default label to suppress warnings about "enumeration value
4574 // 'xxx' not handled in switch
4575 default:
4576 break;
2d66e025 4577 }
f85afd4e 4578 }
2d66e025 4579 return;
f85afd4e 4580 }
2d66e025
MB
4581
4582 m_isDragging = FALSE;
4583
60ff3b99 4584
6d004f67
MB
4585 // ------------ Entering or leaving the window
4586 //
4587 if ( event.Entering() || event.Leaving() )
4588 {
e2b42eeb 4589 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
4590 }
4591
e2b42eeb 4592
2d66e025 4593 // ------------ Left button pressed
f85afd4e 4594 //
6d004f67 4595 else if ( event.LeftDown() )
f85afd4e 4596 {
2d66e025
MB
4597 // don't send a label click event for a hit on the
4598 // edge of the col label - this is probably the user
4599 // wanting to resize the col
4600 //
4601 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 4602 {
2d66e025 4603 col = XToCol(x);
58dd5b3b 4604 if ( col >= 0 &&
b54ba671 4605 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 4606 {
aa5e1f75
SN
4607 if ( !event.ShiftDown() && !event.ControlDown() )
4608 ClearSelection();
4609 if ( event.ShiftDown() )
4610 m_selection->SelectBlock( 0,
4611 m_currentCellCoords.GetCol(),
4612 GetNumberRows() - 1, col,
4613 event.ControlDown(),
4614 event.ShiftDown(),
4615 event.AltDown(),
4616 event.MetaDown() );
4617 else
4618 m_selection->SelectCol( col,
4619 event.ControlDown(),
4620 event.ShiftDown(),
4621 event.AltDown(),
4622 event.MetaDown() );
e2b42eeb 4623 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
f85afd4e 4624 }
2d66e025
MB
4625 }
4626 else
4627 {
4628 // starting to drag-resize a col
4629 //
6e8524b1
MB
4630 if ( CanDragColSize() )
4631 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
4632 }
4633 }
f85afd4e 4634
f85afd4e 4635
2d66e025
MB
4636 // ------------ Left double click
4637 //
4638 if ( event.LeftDClick() )
4639 {
4640 if ( XToEdgeOfCol(x) < 0 )
4641 {
4642 col = XToCol(x);
b54ba671 4643 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
2d66e025
MB
4644 }
4645 }
60ff3b99
VZ
4646
4647
2d66e025
MB
4648 // ------------ Left button released
4649 //
4650 else if ( event.LeftUp() )
4651 {
4652 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4653 {
6d004f67 4654 DoEndDragResizeCol();
e2b42eeb 4655
6d004f67
MB
4656 // Note: we are ending the event *after* doing
4657 // default processing in this case
4658 //
b54ba671 4659 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
2d66e025 4660 }
f85afd4e 4661
e2b42eeb 4662 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
2d66e025 4663 m_dragLastPos = -1;
60ff3b99
VZ
4664 }
4665
4666
2d66e025
MB
4667 // ------------ Right button down
4668 //
4669 else if ( event.RightDown() )
4670 {
4671 col = XToCol(x);
b54ba671 4672 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
4673 {
4674 // no default action at the moment
f85afd4e
MB
4675 }
4676 }
60ff3b99
VZ
4677
4678
2d66e025 4679 // ------------ Right double click
f85afd4e 4680 //
2d66e025
MB
4681 else if ( event.RightDClick() )
4682 {
4683 col = XToCol(x);
b54ba671 4684 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
4685 {
4686 // no default action at the moment
4687 }
4688 }
60ff3b99
VZ
4689
4690
2d66e025 4691 // ------------ No buttons down and mouse moving
f85afd4e 4692 //
2d66e025 4693 else if ( event.Moving() )
f85afd4e 4694 {
2d66e025
MB
4695 m_dragRowOrCol = XToEdgeOfCol( x );
4696 if ( m_dragRowOrCol >= 0 )
f85afd4e 4697 {
2d66e025 4698 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4699 {
e2b42eeb 4700 // don't capture the cursor yet
6e8524b1
MB
4701 if ( CanDragColSize() )
4702 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
f85afd4e 4703 }
2d66e025 4704 }
6d004f67 4705 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 4706 {
e2b42eeb 4707 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
8f177c8e 4708 }
f85afd4e
MB
4709 }
4710}
4711
4712
2d66e025 4713void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 4714{
2d66e025 4715 if ( event.LeftDown() )
f85afd4e 4716 {
2d66e025
MB
4717 // indicate corner label by having both row and
4718 // col args == -1
f85afd4e 4719 //
b54ba671 4720 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
4721 {
4722 SelectAll();
4723 }
f85afd4e
MB
4724 }
4725
2d66e025
MB
4726 else if ( event.LeftDClick() )
4727 {
b54ba671 4728 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 4729 }
8f177c8e 4730
2d66e025 4731 else if ( event.RightDown() )
f85afd4e 4732 {
b54ba671 4733 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 4734 {
2d66e025
MB
4735 // no default action at the moment
4736 }
4737 }
f85afd4e 4738
2d66e025
MB
4739 else if ( event.RightDClick() )
4740 {
b54ba671 4741 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
4742 {
4743 // no default action at the moment
4744 }
4745 }
4746}
f85afd4e 4747
e2b42eeb
VZ
4748void wxGrid::ChangeCursorMode(CursorMode mode,
4749 wxWindow *win,
4750 bool captureMouse)
4751{
4752#ifdef __WXDEBUG__
4753 static const wxChar *cursorModes[] =
4754 {
4755 _T("SELECT_CELL"),
4756 _T("RESIZE_ROW"),
4757 _T("RESIZE_COL"),
4758 _T("SELECT_ROW"),
4759 _T("SELECT_COL")
4760 };
4761
181bfffd
VZ
4762 wxLogTrace(_T("grid"),
4763 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
4764 win == m_colLabelWin ? _T("colLabelWin")
4765 : win ? _T("rowLabelWin")
4766 : _T("gridWin"),
4767 cursorModes[m_cursorMode], cursorModes[mode]);
4768#endif // __WXDEBUG__
4769
faec5a43
SN
4770 if ( mode == m_cursorMode &&
4771 win == m_winCapture &&
4772 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
4773 return;
4774
4775 if ( !win )
4776 {
4777 // by default use the grid itself
4778 win = m_gridWin;
4779 }
4780
4781 if ( m_winCapture )
4782 {
4783 m_winCapture->ReleaseMouse();
4784 m_winCapture = (wxWindow *)NULL;
4785 }
4786
4787 m_cursorMode = mode;
4788
4789 switch ( m_cursorMode )
4790 {
4791 case WXGRID_CURSOR_RESIZE_ROW:
4792 win->SetCursor( m_rowResizeCursor );
4793 break;
4794
4795 case WXGRID_CURSOR_RESIZE_COL:
4796 win->SetCursor( m_colResizeCursor );
4797 break;
4798
4799 default:
4800 win->SetCursor( *wxSTANDARD_CURSOR );
4801 }
4802
4803 // we need to capture mouse when resizing
4804 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
4805 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
4806
4807 if ( captureMouse && resize )
4808 {
4809 win->CaptureMouse();
4810 m_winCapture = win;
4811 }
4812}
8f177c8e 4813
2d66e025
MB
4814void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
4815{
4816 int x, y;
4817 wxPoint pos( event.GetPosition() );
4818 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4819
2d66e025
MB
4820 wxGridCellCoords coords;
4821 XYToCell( x, y, coords );
60ff3b99 4822
2d66e025
MB
4823 if ( event.Dragging() )
4824 {
07296f0b
RD
4825 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4826
4827 // Don't start doing anything until the mouse has been drug at
4828 // least 3 pixels in any direction...
508011ce
VZ
4829 if (! m_isDragging)
4830 {
4831 if (m_startDragPos == wxDefaultPosition)
4832 {
07296f0b
RD
4833 m_startDragPos = pos;
4834 return;
4835 }
4836 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
4837 return;
4838 }
4839
2d66e025 4840 m_isDragging = TRUE;
2d66e025
MB
4841 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4842 {
f0102d2a
VZ
4843 // Hide the edit control, so it
4844 // won't interfer with drag-shrinking.
f6bcfd97 4845 if ( IsCellEditControlShown() )
a5777624 4846 {
f0102d2a 4847 HideCellEditControl();
a5777624
RD
4848 SaveEditControlValue();
4849 }
07296f0b
RD
4850
4851 // Have we captured the mouse yet?
508011ce
VZ
4852 if (! m_winCapture)
4853 {
07296f0b
RD
4854 m_winCapture = m_gridWin;
4855 m_winCapture->CaptureMouse();
4856 }
4857
2d66e025
MB
4858 if ( coords != wxGridNoCellCoords )
4859 {
aa5e1f75
SN
4860 if ( event.ControlDown() )
4861 {
4862 if ( m_selectingKeyboard == wxGridNoCellCoords)
4863 m_selectingKeyboard = coords;
c9097836 4864 HighlightBlock ( m_selectingKeyboard, coords );
aa5e1f75
SN
4865 }
4866 else
4867 {
4868 if ( !IsSelection() )
4869 {
c9097836 4870 HighlightBlock( coords, coords );
aa5e1f75
SN
4871 }
4872 else
4873 {
c9097836 4874 HighlightBlock( m_currentCellCoords, coords );
aa5e1f75 4875 }
f85afd4e 4876 }
07296f0b 4877
508011ce
VZ
4878 if (! IsVisible(coords))
4879 {
07296f0b
RD
4880 MakeCellVisible(coords);
4881 // TODO: need to introduce a delay or something here. The
e32352cf 4882 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 4883 }
2d66e025
MB
4884 }
4885 }
6d004f67
MB
4886 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4887 {
4888 int cw, ch, left, dummy;
4889 m_gridWin->GetClientSize( &cw, &ch );
4890 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 4891
6d004f67
MB
4892 wxClientDC dc( m_gridWin );
4893 PrepareDC( dc );
a95e38c0
VZ
4894 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
4895 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
4896 dc.SetLogicalFunction(wxINVERT);
4897 if ( m_dragLastPos >= 0 )
4898 {
4899 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4900 }
4901 dc.DrawLine( left, y, left+cw, y );
4902 m_dragLastPos = y;
4903 }
4904 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4905 {
4906 int cw, ch, dummy, top;
4907 m_gridWin->GetClientSize( &cw, &ch );
4908 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 4909
6d004f67
MB
4910 wxClientDC dc( m_gridWin );
4911 PrepareDC( dc );
43947979
VZ
4912 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4913 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
4914 dc.SetLogicalFunction(wxINVERT);
4915 if ( m_dragLastPos >= 0 )
4916 {
4917 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4918 }
4919 dc.DrawLine( x, top, x, top+ch );
4920 m_dragLastPos = x;
4921 }
e2b42eeb 4922
2d66e025
MB
4923 return;
4924 }
66242c80 4925
2d66e025 4926 m_isDragging = FALSE;
07296f0b
RD
4927 m_startDragPos = wxDefaultPosition;
4928
a5777624
RD
4929 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4930 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4931 // wxGTK
e2b42eeb 4932#if 0
a5777624
RD
4933 if ( event.Entering() || event.Leaving() )
4934 {
4935 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4936 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
4937 }
4938 else
e2b42eeb
VZ
4939#endif // 0
4940
a5777624
RD
4941 // ------------ Left button pressed
4942 //
4943 if ( event.LeftDown() && coords != wxGridNoCellCoords )
f6bcfd97
BP
4944 {
4945 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
4946 coords.GetRow(),
4947 coords.GetCol(),
4948 event ) )
a5777624 4949 {
f6bcfd97
BP
4950 if ( !event.ControlDown() )
4951 ClearSelection();
4952 if ( event.ShiftDown() )
4953 {
4954 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4955 m_currentCellCoords.GetCol(),
4956 coords.GetRow(),
4957 coords.GetCol(),
4958 event.ControlDown(),
4959 event.ShiftDown(),
4960 event.AltDown(),
4961 event.MetaDown() );
4962 }
4963 else if ( XToEdgeOfCol(x) < 0 &&
4964 YToEdgeOfRow(y) < 0 )
58dd5b3b 4965 {
a5777624
RD
4966 DisableCellEditControl();
4967 MakeCellVisible( coords );
4968
4969 // if this is the second click on this cell then start
4970 // the edit control
4971 if ( m_waitForSlowClick &&
4972 (coords == m_currentCellCoords) &&
4973 CanEnableCellControl())
58dd5b3b 4974 {
a5777624 4975 EnableCellEditControl();
e195a54c 4976
a5777624 4977 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
0b190b0f
VZ
4978 wxGridCellEditor *editor = attr->GetEditor(this,
4979 coords.GetRow(),
4980 coords.GetCol());
4981 editor->StartingClick();
4982 editor->DecRef();
a5777624 4983 attr->DecRef();
e195a54c 4984
a5777624
RD
4985 m_waitForSlowClick = FALSE;
4986 }
4987 else
4988 {
aa5e1f75
SN
4989 if ( event.ControlDown() )
4990 {
4991 m_selection->ToggleCellSelection( coords.GetRow(),
4992 coords.GetCol(),
4993 event.ControlDown(),
4994 event.ShiftDown(),
4995 event.AltDown(),
4996 event.MetaDown() );
4997 m_selectingTopLeft = wxGridNoCellCoords;
4998 m_selectingBottomRight = wxGridNoCellCoords;
4999 m_selectingKeyboard = coords;
5000 }
5001 else
f6bcfd97 5002 {
aa5e1f75 5003 SetCurrentCell( coords );
f6bcfd97
BP
5004 if ( m_selection->GetSelectionMode()
5005 != wxGrid::wxGridSelectCells)
c9097836 5006 HighlightBlock( coords, coords );
f6bcfd97 5007 }
a5777624 5008 m_waitForSlowClick = TRUE;
58dd5b3b
MB
5009 }
5010 }
f85afd4e 5011 }
a5777624 5012 }
f85afd4e 5013
f85afd4e 5014
a5777624
RD
5015 // ------------ Left double click
5016 //
5017 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5018 {
5019 DisableCellEditControl();
5020
5021 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 5022 {
a5777624
RD
5023 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5024 coords.GetRow(),
5025 coords.GetCol(),
5026 event );
58dd5b3b 5027 }
a5777624 5028 }
f85afd4e 5029
8f177c8e 5030
a5777624
RD
5031 // ------------ Left button released
5032 //
5033 else if ( event.LeftUp() )
5034 {
5035 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5036 {
b5808881
SN
5037 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5038 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 5039 {
a5777624 5040 if (m_winCapture)
58dd5b3b 5041 {
a5777624
RD
5042 m_winCapture->ReleaseMouse();
5043 m_winCapture = NULL;
58dd5b3b 5044 }
5c8fc7c1
SN
5045 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5046 m_selectingTopLeft.GetCol(),
5047 m_selectingBottomRight.GetRow(),
5048 m_selectingBottomRight.GetCol(),
d95b0c2b
SN
5049 event.ControlDown(),
5050 event.ShiftDown(),
5051 event.AltDown(),
5052 event.MetaDown() );
5c8fc7c1
SN
5053 m_selectingTopLeft = wxGridNoCellCoords;
5054 m_selectingBottomRight = wxGridNoCellCoords;
6d004f67 5055 }
2d66e025 5056
a5777624
RD
5057 // Show the edit control, if it has been hidden for
5058 // drag-shrinking.
5059 ShowCellEditControl();
5060 }
5061 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5062 {
5063 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5064 DoEndDragResizeRow();
e2b42eeb 5065
a5777624
RD
5066 // Note: we are ending the event *after* doing
5067 // default processing in this case
5068 //
5069 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5070 }
5071 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5072 {
5073 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5074 DoEndDragResizeCol();
e2b42eeb 5075
a5777624
RD
5076 // Note: we are ending the event *after* doing
5077 // default processing in this case
5078 //
5079 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 5080 }
f85afd4e 5081
a5777624
RD
5082 m_dragLastPos = -1;
5083 }
5084
f85afd4e 5085
a5777624
RD
5086 // ------------ Right button down
5087 //
5088 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5089 {
5090 DisableCellEditControl();
5091 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5092 coords.GetRow(),
5093 coords.GetCol(),
5094 event ) )
f85afd4e 5095 {
a5777624 5096 // no default action at the moment
60ff3b99 5097 }
a5777624 5098 }
2d66e025 5099
60ff3b99 5100
a5777624
RD
5101 // ------------ Right double click
5102 //
5103 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5104 {
5105 DisableCellEditControl();
5106 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5107 coords.GetRow(),
5108 coords.GetCol(),
5109 event ) )
f85afd4e 5110 {
a5777624 5111 // no default action at the moment
60ff3b99 5112 }
a5777624
RD
5113 }
5114
5115 // ------------ Moving and no button action
5116 //
5117 else if ( event.Moving() && !event.IsButton() )
5118 {
5119 int dragRow = YToEdgeOfRow( y );
5120 int dragCol = XToEdgeOfCol( x );
790cc417 5121
a5777624
RD
5122 // Dragging on the corner of a cell to resize in both
5123 // directions is not implemented yet...
790cc417 5124 //
a5777624 5125 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 5126 {
a5777624
RD
5127 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5128 return;
5129 }
6d004f67 5130
a5777624
RD
5131 if ( dragRow >= 0 )
5132 {
5133 m_dragRowOrCol = dragRow;
6d004f67 5134
a5777624 5135 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5136 {
a5777624
RD
5137 if ( CanDragRowSize() && CanDragGridSize() )
5138 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 5139 }
e2b42eeb 5140
b94ae1ea
VZ
5141 if ( dragCol >= 0 )
5142 {
5143 m_dragRowOrCol = dragCol;
122603f1 5144 }
b94ae1ea 5145
a5777624
RD
5146 return;
5147 }
e2b42eeb 5148
a5777624
RD
5149 if ( dragCol >= 0 )
5150 {
5151 m_dragRowOrCol = dragCol;
e2b42eeb 5152
a5777624 5153 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5154 {
a5777624
RD
5155 if ( CanDragColSize() && CanDragGridSize() )
5156 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67 5157 }
a5777624
RD
5158
5159 return;
6d004f67 5160 }
a5777624
RD
5161
5162 // Neither on a row or col edge
5163 //
5164 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5165 {
5166 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5167 }
5168 }
6d004f67
MB
5169}
5170
5171
5172void wxGrid::DoEndDragResizeRow()
5173{
5174 if ( m_dragLastPos >= 0 )
5175 {
5176 // erase the last line and resize the row
5177 //
5178 int cw, ch, left, dummy;
5179 m_gridWin->GetClientSize( &cw, &ch );
5180 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5181
5182 wxClientDC dc( m_gridWin );
5183 PrepareDC( dc );
5184 dc.SetLogicalFunction( wxINVERT );
5185 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5186 HideCellEditControl();
a5777624 5187 SaveEditControlValue();
6d004f67 5188
7c1cb261 5189 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67
MB
5190 SetRowSize( m_dragRowOrCol,
5191 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
e2b42eeb 5192
6d004f67
MB
5193 if ( !GetBatchCount() )
5194 {
5195 // Only needed to get the correct rect.y:
5196 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5197 rect.x = 0;
5198 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5199 rect.width = m_rowLabelWidth;
5200 rect.height = ch - rect.y;
5201 m_rowLabelWin->Refresh( TRUE, &rect );
5202 rect.width = cw;
5203 m_gridWin->Refresh( FALSE, &rect );
5204 }
5205
5206 ShowCellEditControl();
5207 }
5208}
5209
5210
5211void wxGrid::DoEndDragResizeCol()
5212{
5213 if ( m_dragLastPos >= 0 )
5214 {
5215 // erase the last line and resize the col
5216 //
5217 int cw, ch, dummy, top;
5218 m_gridWin->GetClientSize( &cw, &ch );
5219 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5220
5221 wxClientDC dc( m_gridWin );
5222 PrepareDC( dc );
5223 dc.SetLogicalFunction( wxINVERT );
5224 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5225 HideCellEditControl();
a5777624 5226 SaveEditControlValue();
6d004f67 5227
7c1cb261 5228 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 5229 SetColSize( m_dragRowOrCol,
43947979
VZ
5230 wxMax( m_dragLastPos - colLeft,
5231 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
5232
5233 if ( !GetBatchCount() )
5234 {
5235 // Only needed to get the correct rect.x:
5236 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5237 rect.y = 0;
5238 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5239 rect.width = cw - rect.x;
5240 rect.height = m_colLabelHeight;
5241 m_colLabelWin->Refresh( TRUE, &rect );
5242 rect.height = ch;
5243 m_gridWin->Refresh( FALSE, &rect );
790cc417 5244 }
e2b42eeb 5245
6d004f67 5246 ShowCellEditControl();
f85afd4e 5247 }
f85afd4e
MB
5248}
5249
5250
6d004f67 5251
2d66e025
MB
5252//
5253// ------ interaction with data model
5254//
5255bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 5256{
2d66e025 5257 switch ( msg.GetId() )
17732cec 5258 {
2d66e025
MB
5259 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5260 return GetModelValues();
17732cec 5261
2d66e025
MB
5262 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5263 return SetModelValues();
f85afd4e 5264
2d66e025
MB
5265 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5266 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5267 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5268 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5269 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5270 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5271 return Redimension( msg );
5272
5273 default:
5274 return FALSE;
f85afd4e 5275 }
2d66e025 5276}
f85afd4e 5277
f85afd4e 5278
f85afd4e 5279
2d66e025
MB
5280// The behaviour of this function depends on the grid table class
5281// Clear() function. For the default wxGridStringTable class the
5282// behavious is to replace all cell contents with wxEmptyString but
5283// not to change the number of rows or cols.
5284//
5285void wxGrid::ClearGrid()
5286{
5287 if ( m_table )
f85afd4e 5288 {
4cfa5de6
RD
5289 if (IsCellEditControlEnabled())
5290 DisableCellEditControl();
5291
2d66e025 5292 m_table->Clear();
1f1ce288 5293 if ( !GetBatchCount() ) m_gridWin->Refresh();
f85afd4e
MB
5294 }
5295}
5296
5297
2d66e025 5298bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5299{
2d66e025 5300 // TODO: something with updateLabels flag
8f177c8e 5301
2d66e025 5302 if ( !m_created )
f85afd4e 5303 {
bd3c6758 5304 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2d66e025
MB
5305 return FALSE;
5306 }
8f177c8e 5307
2d66e025
MB
5308 if ( m_table )
5309 {
b7fff980 5310 if (IsCellEditControlEnabled())
b54ba671 5311 DisableCellEditControl();
b7fff980 5312
f6bcfd97 5313 return m_table->InsertRows( pos, numRows );
f85afd4e 5314
2d66e025
MB
5315 // the table will have sent the results of the insert row
5316 // operation to this view object as a grid table message
f85afd4e 5317 }
f6bcfd97 5318 return FALSE;
f85afd4e
MB
5319}
5320
5321
2d66e025 5322bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5323{
2d66e025
MB
5324 // TODO: something with updateLabels flag
5325
5326 if ( !m_created )
f85afd4e 5327 {
bd3c6758 5328 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2d66e025
MB
5329 return FALSE;
5330 }
5331
f6bcfd97
BP
5332 return ( m_table && m_table->AppendRows( numRows ) );
5333 // the table will have sent the results of the append row
5334 // operation to this view object as a grid table message
f85afd4e
MB
5335}
5336
2d66e025
MB
5337
5338bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 5339{
2d66e025
MB
5340 // TODO: something with updateLabels flag
5341
5342 if ( !m_created )
f85afd4e 5343 {
bd3c6758 5344 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2d66e025
MB
5345 return FALSE;
5346 }
5347
b7fff980 5348 if ( m_table )
2d66e025 5349 {
b7fff980 5350 if (IsCellEditControlEnabled())
b54ba671 5351 DisableCellEditControl();
f85afd4e 5352
f6bcfd97
BP
5353 return (m_table->DeleteRows( pos, numRows ));
5354 // the table will have sent the results of the delete row
5355 // operation to this view object as a grid table message
2d66e025 5356 }
b7fff980 5357 return FALSE;
2d66e025 5358}
f85afd4e 5359
f85afd4e 5360
2d66e025
MB
5361bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5362{
5363 // TODO: something with updateLabels flag
8f177c8e 5364
2d66e025
MB
5365 if ( !m_created )
5366 {
bd3c6758 5367 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2d66e025
MB
5368 return FALSE;
5369 }
f85afd4e 5370
2d66e025
MB
5371 if ( m_table )
5372 {
b7fff980 5373 if (IsCellEditControlEnabled())
b54ba671 5374 DisableCellEditControl();
b7fff980 5375
f6bcfd97 5376 return m_table->InsertCols( pos, numCols );
2d66e025
MB
5377 // the table will have sent the results of the insert col
5378 // operation to this view object as a grid table message
f85afd4e 5379 }
f6bcfd97 5380 return FALSE;
f85afd4e
MB
5381}
5382
2d66e025
MB
5383
5384bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 5385{
2d66e025
MB
5386 // TODO: something with updateLabels flag
5387
5388 if ( !m_created )
f85afd4e 5389 {
bd3c6758 5390 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2d66e025
MB
5391 return FALSE;
5392 }
f85afd4e 5393
f6bcfd97
BP
5394 return ( m_table && m_table->AppendCols( numCols ) );
5395 // the table will have sent the results of the append col
5396 // operation to this view object as a grid table message
f85afd4e
MB
5397}
5398
5399
2d66e025 5400bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 5401{
2d66e025 5402 // TODO: something with updateLabels flag
8f177c8e 5403
2d66e025 5404 if ( !m_created )
f85afd4e 5405 {
bd3c6758 5406 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2d66e025 5407 return FALSE;
f85afd4e
MB
5408 }
5409
b7fff980 5410 if ( m_table )
2d66e025 5411 {
b7fff980 5412 if (IsCellEditControlEnabled())
b54ba671 5413 DisableCellEditControl();
8f177c8e 5414
f6bcfd97
BP
5415 return ( m_table->DeleteCols( pos, numCols ) );
5416 // the table will have sent the results of the delete col
5417 // operation to this view object as a grid table message
f85afd4e 5418 }
b7fff980 5419 return FALSE;
f85afd4e
MB
5420}
5421
5422
2d66e025
MB
5423
5424//
5425// ----- event handlers
f85afd4e 5426//
8f177c8e 5427
2d66e025
MB
5428// Generate a grid event based on a mouse event and
5429// return the result of ProcessEvent()
5430//
5431bool wxGrid::SendEvent( const wxEventType type,
5432 int row, int col,
5433 wxMouseEvent& mouseEv )
5434{
b54ba671 5435 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 5436 {
2d66e025
MB
5437 int rowOrCol = (row == -1 ? col : row);
5438
5439 wxGridSizeEvent gridEvt( GetId(),
5440 type,
5441 this,
5442 rowOrCol,
faec5a43
SN
5443 mouseEv.GetX() + GetRowLabelSize(),
5444 mouseEv.GetY() + GetColLabelSize(),
2d66e025
MB
5445 mouseEv.ControlDown(),
5446 mouseEv.ShiftDown(),
5447 mouseEv.AltDown(),
5448 mouseEv.MetaDown() );
2d66e025 5449 return GetEventHandler()->ProcessEvent(gridEvt);
f85afd4e 5450 }
b54ba671 5451 else if ( type == wxEVT_GRID_RANGE_SELECT )
2d66e025 5452 {
f6bcfd97 5453 // Right now, it should _never_ end up here!
2d66e025
MB
5454 wxGridRangeSelectEvent gridEvt( GetId(),
5455 type,
5456 this,
b5808881
SN
5457 m_selectingTopLeft,
5458 m_selectingBottomRight,
5c8fc7c1 5459 TRUE,
2d66e025
MB
5460 mouseEv.ControlDown(),
5461 mouseEv.ShiftDown(),
5462 mouseEv.AltDown(),
5463 mouseEv.MetaDown() );
f85afd4e 5464
2d66e025
MB
5465 return GetEventHandler()->ProcessEvent(gridEvt);
5466 }
5467 else
5468 {
5469 wxGridEvent gridEvt( GetId(),
5470 type,
5471 this,
5472 row, col,
faec5a43
SN
5473 mouseEv.GetX() + GetRowLabelSize(),
5474 mouseEv.GetY() + GetColLabelSize(),
d7da9756 5475 FALSE,
2d66e025
MB
5476 mouseEv.ControlDown(),
5477 mouseEv.ShiftDown(),
5478 mouseEv.AltDown(),
5479 mouseEv.MetaDown() );
2d66e025
MB
5480 return GetEventHandler()->ProcessEvent(gridEvt);
5481 }
f85afd4e
MB
5482}
5483
5484
2d66e025
MB
5485// Generate a grid event of specified type and return the result
5486// of ProcessEvent().
f85afd4e 5487//
2d66e025
MB
5488bool wxGrid::SendEvent( const wxEventType type,
5489 int row, int col )
f85afd4e 5490{
b54ba671 5491 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 5492 {
2d66e025 5493 int rowOrCol = (row == -1 ? col : row);
f85afd4e 5494
2d66e025
MB
5495 wxGridSizeEvent gridEvt( GetId(),
5496 type,
5497 this,
5498 rowOrCol );
5499
5500 return GetEventHandler()->ProcessEvent(gridEvt);
5501 }
5502 else
5503 {
5504 wxGridEvent gridEvt( GetId(),
5505 type,
5506 this,
5507 row, col );
8f177c8e 5508
2d66e025
MB
5509 return GetEventHandler()->ProcessEvent(gridEvt);
5510 }
f85afd4e
MB
5511}
5512
5513
2d66e025 5514void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 5515{
f6bcfd97 5516 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
8f177c8e 5517}
f85afd4e
MB
5518
5519
18f9565d
MB
5520// This is just here to make sure that CalcDimensions gets called when
5521// the grid view is resized... then the size event is skipped to allow
5522// the box sizers to handle everything
5523//
aa5e1f75 5524void wxGrid::OnSize( wxSizeEvent& WXUNUSED(event) )
f85afd4e 5525{
7807d81c 5526 CalcWindowSizes();
2d66e025 5527 CalcDimensions();
f85afd4e
MB
5528}
5529
f85afd4e 5530
2d66e025 5531void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 5532{
2d66e025 5533 if ( m_inOnKeyDown )
f85afd4e 5534 {
2d66e025
MB
5535 // shouldn't be here - we are going round in circles...
5536 //
07296f0b 5537 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
5538 }
5539
2d66e025 5540 m_inOnKeyDown = TRUE;
f85afd4e 5541
2d66e025
MB
5542 // propagate the event up and see if it gets processed
5543 //
5544 wxWindow *parent = GetParent();
5545 wxKeyEvent keyEvt( event );
5546 keyEvt.SetEventObject( parent );
5547
5548 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 5549 {
9b4aede2 5550
2d66e025
MB
5551 // try local handlers
5552 //
5553 switch ( event.KeyCode() )
5554 {
5555 case WXK_UP:
5556 if ( event.ControlDown() )
5557 {
5c8fc7c1 5558 MoveCursorUpBlock( event.ShiftDown() );
2d66e025
MB
5559 }
5560 else
5561 {
5c8fc7c1 5562 MoveCursorUp( event.ShiftDown() );
2d66e025
MB
5563 }
5564 break;
f85afd4e 5565
2d66e025
MB
5566 case WXK_DOWN:
5567 if ( event.ControlDown() )
5568 {
5c8fc7c1 5569 MoveCursorDownBlock( event.ShiftDown() );
2d66e025
MB
5570 }
5571 else
5572 {
5c8fc7c1 5573 MoveCursorDown( event.ShiftDown() );
2d66e025
MB
5574 }
5575 break;
8f177c8e 5576
2d66e025
MB
5577 case WXK_LEFT:
5578 if ( event.ControlDown() )
5579 {
5c8fc7c1 5580 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025
MB
5581 }
5582 else
5583 {
5c8fc7c1 5584 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
5585 }
5586 break;
5587
5588 case WXK_RIGHT:
5589 if ( event.ControlDown() )
5590 {
5c8fc7c1 5591 MoveCursorRightBlock( event.ShiftDown() );
2d66e025
MB
5592 }
5593 else
5594 {
5c8fc7c1 5595 MoveCursorRight( event.ShiftDown() );
2d66e025
MB
5596 }
5597 break;
b99be8fb 5598
2d66e025 5599 case WXK_RETURN:
a4f7bf58 5600 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
5601 if ( event.ControlDown() )
5602 {
5603 event.Skip(); // to let the edit control have the return
5604 }
5605 else
5606 {
f6bcfd97
BP
5607 if ( GetGridCursorRow() < GetNumberRows()-1 )
5608 {
5609 MoveCursorDown( event.ShiftDown() );
5610 }
5611 else
5612 {
5613 // at the bottom of a column
5614 HideCellEditControl();
5615 SaveEditControlValue();
5616 }
58dd5b3b 5617 }
2d66e025
MB
5618 break;
5619
5c8fc7c1 5620 case WXK_ESCAPE:
e32352cf 5621 ClearSelection();
5c8fc7c1
SN
5622 break;
5623
2c9a89e0
RD
5624 case WXK_TAB:
5625 if (event.ShiftDown())
f6bcfd97
BP
5626 {
5627 if ( GetGridCursorCol() > 0 )
5628 {
5629 MoveCursorLeft( FALSE );
5630 }
5631 else
5632 {
5633 // at left of grid
5634 HideCellEditControl();
5635 SaveEditControlValue();
5636 }
5637 }
2c9a89e0 5638 else
f6bcfd97
BP
5639 {
5640 if ( GetGridCursorCol() < GetNumberCols()-1 )
5641 {
5642 MoveCursorRight( FALSE );
5643 }
5644 else
5645 {
5646 // at right of grid
5647 HideCellEditControl();
5648 SaveEditControlValue();
5649 }
5650 }
2c9a89e0
RD
5651 break;
5652
2d66e025
MB
5653 case WXK_HOME:
5654 if ( event.ControlDown() )
5655 {
5656 MakeCellVisible( 0, 0 );
5657 SetCurrentCell( 0, 0 );
5658 }
5659 else
5660 {
5661 event.Skip();
5662 }
5663 break;
5664
5665 case WXK_END:
5666 if ( event.ControlDown() )
5667 {
5668 MakeCellVisible( m_numRows-1, m_numCols-1 );
5669 SetCurrentCell( m_numRows-1, m_numCols-1 );
5670 }
5671 else
5672 {
5673 event.Skip();
5674 }
5675 break;
5676
5677 case WXK_PRIOR:
5678 MovePageUp();
5679 break;
5680
5681 case WXK_NEXT:
5682 MovePageDown();
5683 break;
5684
07296f0b 5685 case WXK_SPACE:
aa5e1f75
SN
5686 if ( event.ControlDown() )
5687 {
5688 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
5689 m_currentCellCoords.GetCol(),
5690 event.ControlDown(),
5691 event.ShiftDown(),
5692 event.AltDown(),
5693 event.MetaDown() );
5694 break;
5695 }
07296f0b
RD
5696 if ( !IsEditable() )
5697 {
5c8fc7c1 5698 MoveCursorRight( FALSE );
07296f0b
RD
5699 break;
5700 }
5701 // Otherwise fall through to default
5702
2d66e025 5703 default:
f6bcfd97
BP
5704 // is it possible to edit the current cell at all?
5705 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
b54ba671 5706 {
f6bcfd97 5707 // yes, now check whether the cells editor accepts the key
f2d76237
RD
5708 int row = m_currentCellCoords.GetRow();
5709 int col = m_currentCellCoords.GetCol();
5710 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f 5711 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
f6bcfd97
BP
5712
5713 // <F2> is special and will always start editing, for
5714 // other keys - ask the editor itself
5715 if ( (event.KeyCode() == WXK_F2 && !event.HasModifiers())
5716 || editor->IsAcceptedKey(event) )
5717 {
5718 EnableCellEditControl();
5719 editor->StartingKey(event);
5720 }
5721 else
5722 {
5723 event.Skip();
5724 }
5725
0b190b0f 5726 editor->DecRef();
2c9a89e0
RD
5727 attr->DecRef();
5728 }
b3a7510d
VZ
5729 else
5730 {
b94ae1ea
VZ
5731 // let others process char events with modifiers or all
5732 // char events for readonly cells
b3a7510d
VZ
5733 event.Skip();
5734 }
025562fe 5735 break;
2d66e025 5736 }
f85afd4e
MB
5737 }
5738
2d66e025 5739 m_inOnKeyDown = FALSE;
f85afd4e
MB
5740}
5741
f6bcfd97
BP
5742void wxGrid::OnKeyUp( wxKeyEvent& event )
5743{
5744 // try local handlers
5745 //
5746 if ( event.KeyCode() == WXK_SHIFT )
5747 {
5748 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5749 m_selectingBottomRight != wxGridNoCellCoords )
5750 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5751 m_selectingTopLeft.GetCol(),
5752 m_selectingBottomRight.GetRow(),
5753 m_selectingBottomRight.GetCol(),
5754 event.ControlDown(),
5755 TRUE,
5756 event.AltDown(),
5757 event.MetaDown() );
5758 m_selectingTopLeft = wxGridNoCellCoords;
5759 m_selectingBottomRight = wxGridNoCellCoords;
5760 m_selectingKeyboard = wxGridNoCellCoords;
5761 }
5762}
5763
2796cce3 5764void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
5765{
5766}
07296f0b 5767
2d66e025 5768void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 5769{
f6bcfd97
BP
5770 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
5771 {
5772 // the event has been intercepted - do nothing
5773 return;
5774 }
5775
5776 wxClientDC dc(m_gridWin);
5777 PrepareDC(dc);
5778
2a7750d9 5779 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 5780 {
2d66e025 5781 HideCellEditControl();
b54ba671 5782 DisableCellEditControl();
07296f0b 5783
3ca6a5f0 5784 if ( IsVisible( m_currentCellCoords, FALSE ) )
f6bcfd97
BP
5785 {
5786 wxRect r;
d10f4bf9 5787 r = BlockToDeviceRect(m_currentCellCoords, coords);
f6bcfd97
BP
5788 if ( !m_gridLinesEnabled )
5789 {
5790 r.x--;
5791 r.y--;
5792 r.width++;
5793 r.height++;
5794 }
d1c0b4f9 5795
d10f4bf9 5796 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 5797
f6bcfd97
BP
5798 // Otherwise refresh redraws the highlight!
5799 m_currentCellCoords = coords;
d10f4bf9
VZ
5800
5801 DrawGridCellArea(dc,cells);
f6bcfd97
BP
5802 DrawAllGridLines( dc, r );
5803 }
66242c80 5804 }
8f177c8e 5805
2d66e025
MB
5806 m_currentCellCoords = coords;
5807
2a7750d9
MB
5808 wxGridCellAttr* attr = GetCellAttr(coords);
5809 DrawCellHighlight(dc, attr);
5810 attr->DecRef();
66242c80
MB
5811}
5812
2d66e025 5813
c9097836
MB
5814void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
5815{
5816 int temp;
5817 wxGridCellCoords updateTopLeft, updateBottomRight;
5818
5819 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
5820 {
5821 leftCol = 0;
5822 rightCol = GetNumberCols() - 1;
5823 }
5824 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
5825 {
5826 topRow = 0;
5827 bottomRow = GetNumberRows() - 1;
5828 }
5829 if ( topRow > bottomRow )
5830 {
5831 temp = topRow;
5832 topRow = bottomRow;
5833 bottomRow = temp;
5834 }
5835
5836 if ( leftCol > rightCol )
5837 {
5838 temp = leftCol;
5839 leftCol = rightCol;
5840 rightCol = temp;
5841 }
5842
5843 updateTopLeft = wxGridCellCoords( topRow, leftCol );
5844 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
5845
5846 if ( m_selectingTopLeft != updateTopLeft ||
5847 m_selectingBottomRight != updateBottomRight )
5848 {
5849 // Compute two optimal update rectangles:
5850 // Either one rectangle is a real subset of the
5851 // other, or they are (almost) disjoint!
5852 wxRect rect[4];
5853 bool need_refresh[4];
5854 need_refresh[0] =
5855 need_refresh[1] =
5856 need_refresh[2] =
5857 need_refresh[3] = FALSE;
5858 int i;
5859
5860 // Store intermediate values
5861 wxCoord oldLeft = m_selectingTopLeft.GetCol();
5862 wxCoord oldTop = m_selectingTopLeft.GetRow();
5863 wxCoord oldRight = m_selectingBottomRight.GetCol();
5864 wxCoord oldBottom = m_selectingBottomRight.GetRow();
5865
5866 // Determine the outer/inner coordinates.
5867 if (oldLeft > leftCol)
5868 {
5869 temp = oldLeft;
5870 oldLeft = leftCol;
5871 leftCol = temp;
5872 }
5873 if (oldTop > topRow )
5874 {
5875 temp = oldTop;
5876 oldTop = topRow;
5877 topRow = temp;
5878 }
5879 if (oldRight < rightCol )
5880 {
5881 temp = oldRight;
5882 oldRight = rightCol;
5883 rightCol = temp;
5884 }
5885 if (oldBottom < bottomRow)
5886 {
5887 temp = oldBottom;
5888 oldBottom = bottomRow;
5889 bottomRow = temp;
5890 }
5891
5892 // Now, either the stuff marked old is the outer
5893 // rectangle or we don't have a situation where one
5894 // is contained in the other.
5895
5896 if ( oldLeft < leftCol )
5897 {
5898 need_refresh[0] = TRUE;
5899 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
5900 oldLeft ),
5901 wxGridCellCoords ( oldBottom,
5902 leftCol - 1 ) );
5903 }
5904
5905 if ( oldTop < topRow )
5906 {
5907 need_refresh[1] = TRUE;
5908 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
5909 leftCol ),
5910 wxGridCellCoords ( topRow - 1,
5911 rightCol ) );
5912 }
5913
5914 if ( oldRight > rightCol )
5915 {
5916 need_refresh[2] = TRUE;
5917 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
5918 rightCol + 1 ),
5919 wxGridCellCoords ( oldBottom,
5920 oldRight ) );
5921 }
5922
5923 if ( oldBottom > bottomRow )
5924 {
5925 need_refresh[3] = TRUE;
5926 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
5927 leftCol ),
5928 wxGridCellCoords ( oldBottom,
5929 rightCol ) );
5930 }
5931
5932
5933 // Change Selection
5934 m_selectingTopLeft = updateTopLeft;
5935 m_selectingBottomRight = updateBottomRight;
5936
5937 // various Refresh() calls
5938 for (i = 0; i < 4; i++ )
5939 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
5940 m_gridWin->Refresh( FALSE, &(rect[i]) );
5941 }
5942
5943 // never generate an event as it will be generated from
5944 // wxGridSelection::SelectBlock!
5945 // (old comment from when this was the body of SelectBlock)
5946}
5947
2d66e025
MB
5948//
5949// ------ functions to get/send data (see also public functions)
5950//
5951
5952bool wxGrid::GetModelValues()
66242c80 5953{
2d66e025 5954 if ( m_table )
66242c80 5955 {
2d66e025 5956 // all we need to do is repaint the grid
66242c80 5957 //
2d66e025 5958 m_gridWin->Refresh();
66242c80
MB
5959 return TRUE;
5960 }
8f177c8e 5961
66242c80
MB
5962 return FALSE;
5963}
5964
2d66e025
MB
5965
5966bool wxGrid::SetModelValues()
f85afd4e 5967{
2d66e025 5968 int row, col;
8f177c8e 5969
2d66e025
MB
5970 if ( m_table )
5971 {
5972 for ( row = 0; row < m_numRows; row++ )
f85afd4e 5973 {
2d66e025 5974 for ( col = 0; col < m_numCols; col++ )
f85afd4e 5975 {
2d66e025 5976 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
5977 }
5978 }
8f177c8e 5979
f85afd4e
MB
5980 return TRUE;
5981 }
5982
5983 return FALSE;
5984}
5985
2d66e025
MB
5986
5987
5988// Note - this function only draws cells that are in the list of
5989// exposed cells (usually set from the update region by
5990// CalcExposedCells)
5991//
d10f4bf9 5992void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 5993{
2d66e025 5994 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5995
2d66e025 5996 size_t i;
d10f4bf9 5997 size_t numCells = cells.GetCount();
60ff3b99 5998
2d66e025 5999 for ( i = 0; i < numCells; i++ )
f85afd4e 6000 {
d10f4bf9 6001 DrawCell( dc, cells[i] );
2d66e025
MB
6002 }
6003}
8f177c8e 6004
8f177c8e 6005
7c8a8ad5
MB
6006void wxGrid::DrawGridSpace( wxDC& dc )
6007{
f6bcfd97
BP
6008 int cw, ch;
6009 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 6010
f6bcfd97
BP
6011 int right, bottom;
6012 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 6013
f6bcfd97
BP
6014 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6015 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
7c8a8ad5 6016
f6bcfd97
BP
6017 if ( right > rightCol || bottom > bottomRow )
6018 {
6019 int left, top;
6020 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 6021
f6bcfd97
BP
6022 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6023 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 6024
f6bcfd97
BP
6025 if ( right > rightCol )
6026 {
6027 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6028 }
6029
6030 if ( bottom > bottomRow )
6031 {
6032 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6033 }
6034 }
7c8a8ad5
MB
6035}
6036
6037
2d66e025
MB
6038void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6039{
ab79958a
VZ
6040 int row = coords.GetRow();
6041 int col = coords.GetCol();
60ff3b99 6042
7c1cb261 6043 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
6044 return;
6045
6046 // we draw the cell border ourselves
9496deb5 6047#if !WXGRID_DRAW_LINES
2d66e025
MB
6048 if ( m_gridLinesEnabled )
6049 DrawCellBorder( dc, coords );
796df70a 6050#endif
f85afd4e 6051
189d0213
VZ
6052 wxGridCellAttr* attr = GetCellAttr(row, col);
6053
6054 bool isCurrent = coords == m_currentCellCoords;
508011ce 6055
f6bcfd97 6056 wxRect rect = CellToRect( row, col );
f85afd4e 6057
189d0213 6058 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
6059 // Note: However, only if it is really _shown_, i.e. not hidden!
6060 if ( isCurrent && IsCellEditControlShown() )
189d0213 6061 {
0b190b0f
VZ
6062 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6063 editor->PaintBackground(rect, attr);
6064 editor->DecRef();
189d0213
VZ
6065 }
6066 else
6067 {
6068 // but all the rest is drawn by the cell renderer and hence may be
6069 // customized
0b190b0f
VZ
6070 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
6071 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
6072 renderer->DecRef();
189d0213 6073 }
07296f0b 6074
283b7808
VZ
6075 attr->DecRef();
6076}
07296f0b 6077
283b7808 6078void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
6079{
6080 int row = m_currentCellCoords.GetRow();
6081 int col = m_currentCellCoords.GetCol();
6082
7c1cb261 6083 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
6084 return;
6085
f6bcfd97 6086 wxRect rect = CellToRect(row, col);
07296f0b 6087
b3a7510d
VZ
6088 // hmmm... what could we do here to show that the cell is disabled?
6089 // for now, I just draw a thinner border than for the other ones, but
6090 // it doesn't look really good
b3a7510d 6091
d2520c85
RD
6092 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
6093
6094 if (penWidth > 0) {
6095
6096 // The center of th drawn line is where the position/width/height of
6097 // the rectangle is actually at, (on wxMSW atr least,) so we will
6098 // reduce the size of the rectangle to compensate for the thickness of
6099 // the line. If this is too strange on non wxMSW platforms then
6100 // please #ifdef this appropriately.
6101 rect.x += penWidth/2;
6102 rect.y += penWidth/2;
6103 rect.width -= penWidth-1;
6104 rect.height -= penWidth-1;
6105
6106
6107 // Now draw the rectangle
6108 dc.SetPen(wxPen(m_cellHighlightColour, penWidth, wxSOLID));
6109 dc.SetBrush(*wxTRANSPARENT_BRUSH);
6110 dc.DrawRectangle(rect);
6111 }
07296f0b 6112
283b7808 6113#if 0
b3a7510d 6114 // VZ: my experiments with 3d borders...
283b7808 6115
b3a7510d 6116 // how to properly set colours for arbitrary bg?
283b7808
VZ
6117 wxCoord x1 = rect.x,
6118 y1 = rect.y,
00cf1208
RR
6119 x2 = rect.x + rect.width -1,
6120 y2 = rect.y + rect.height -1;
283b7808
VZ
6121
6122 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
6123 dc.DrawLine(x1, y1, x2, y1);
6124 dc.DrawLine(x1, y1, x1, y2);
283b7808 6125
283b7808 6126 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
00cf1208 6127 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
283b7808
VZ
6128
6129 dc.SetPen(*wxBLACK_PEN);
6130 dc.DrawLine(x1, y2, x2, y2);
00cf1208 6131 dc.DrawLine(x2, y1, x2, y2+1);
b3a7510d 6132#endif // 0
2d66e025 6133}
f85afd4e 6134
f2d76237 6135
2d66e025
MB
6136void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
6137{
2d66e025
MB
6138 int row = coords.GetRow();
6139 int col = coords.GetCol();
7c1cb261
VZ
6140 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6141 return;
6142
6143 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
60ff3b99 6144
2d66e025
MB
6145 // right hand border
6146 //
7c1cb261
VZ
6147 dc.DrawLine( GetColRight(col), GetRowTop(row),
6148 GetColRight(col), GetRowBottom(row) );
2d66e025
MB
6149
6150 // bottom border
6151 //
7c1cb261
VZ
6152 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
6153 GetColRight(col), GetRowBottom(row) );
f85afd4e
MB
6154}
6155
d10f4bf9 6156void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
b3a7510d 6157{
2a7750d9
MB
6158 // This if block was previously in wxGrid::OnPaint but that doesn't
6159 // seem to get called under wxGTK - MB
6160 //
6161 if ( m_currentCellCoords == wxGridNoCellCoords &&
6162 m_numRows && m_numCols )
6163 {
6164 m_currentCellCoords.Set(0, 0);
6165 }
6166
f6bcfd97 6167 if ( IsCellEditControlShown() )
99306db2
VZ
6168 {
6169 // don't show highlight when the edit control is shown
6170 return;
6171 }
6172
b3a7510d
VZ
6173 // if the active cell was repainted, repaint its highlight too because it
6174 // might have been damaged by the grid lines
d10f4bf9 6175 size_t count = cells.GetCount();
b3a7510d
VZ
6176 for ( size_t n = 0; n < count; n++ )
6177 {
d10f4bf9 6178 if ( cells[n] == m_currentCellCoords )
b3a7510d
VZ
6179 {
6180 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
6181 DrawCellHighlight(dc, attr);
6182 attr->DecRef();
6183
6184 break;
6185 }
6186 }
6187}
2d66e025 6188
2d66e025
MB
6189// TODO: remove this ???
6190// This is used to redraw all grid lines e.g. when the grid line colour
6191// has been changed
6192//
33ac7e6f 6193void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 6194{
60ff3b99
VZ
6195 if ( !m_gridLinesEnabled ||
6196 !m_numRows ||
2d66e025 6197 !m_numCols ) return;
f85afd4e 6198
2d66e025 6199 int top, bottom, left, right;
796df70a 6200
f6bcfd97 6201#if 0 //#ifndef __WXGTK__
508011ce
VZ
6202 if (reg.IsEmpty())
6203 {
796df70a
SN
6204 int cw, ch;
6205 m_gridWin->GetClientSize(&cw, &ch);
6206
6207 // virtual coords of visible area
6208 //
6209 CalcUnscrolledPosition( 0, 0, &left, &top );
6210 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6211 }
508011ce
VZ
6212 else
6213 {
796df70a
SN
6214 wxCoord x, y, w, h;
6215 reg.GetBox(x, y, w, h);
6216 CalcUnscrolledPosition( x, y, &left, &top );
6217 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
6218 }
8e217128
RR
6219#else
6220 int cw, ch;
6221 m_gridWin->GetClientSize(&cw, &ch);
6222 CalcUnscrolledPosition( 0, 0, &left, &top );
6223 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6224#endif
f85afd4e 6225
9496deb5
MB
6226 // avoid drawing grid lines past the last row and col
6227 //
7c1cb261
VZ
6228 right = wxMin( right, GetColRight(m_numCols - 1) );
6229 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 6230
2d66e025 6231 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
f85afd4e 6232
2d66e025 6233 // horizontal grid lines
f85afd4e 6234 //
60ff3b99 6235 int i;
9496deb5 6236 for ( i = 0; i < m_numRows; i++ )
f85afd4e 6237 {
6d55126d 6238 int bot = GetRowBottom(i) - 1;
7c1cb261 6239
6d55126d 6240 if ( bot > bottom )
2d66e025
MB
6241 {
6242 break;
6243 }
7c1cb261 6244
6d55126d 6245 if ( bot >= top )
2d66e025 6246 {
6d55126d 6247 dc.DrawLine( left, bot, right, bot );
2d66e025 6248 }
f85afd4e
MB
6249 }
6250
f85afd4e 6251
2d66e025
MB
6252 // vertical grid lines
6253 //
9496deb5 6254 for ( i = 0; i < m_numCols; i++ )
f85afd4e 6255 {
7c1cb261
VZ
6256 int colRight = GetColRight(i) - 1;
6257 if ( colRight > right )
2d66e025
MB
6258 {
6259 break;
6260 }
7c1cb261
VZ
6261
6262 if ( colRight >= left )
2d66e025 6263 {
7c1cb261 6264 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
6265 }
6266 }
6267}
f85afd4e 6268
8f177c8e 6269
d10f4bf9 6270void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
2d66e025 6271{
f6bcfd97 6272 if ( !m_numRows ) return;
60ff3b99 6273
2d66e025 6274 size_t i;
d10f4bf9 6275 size_t numLabels = rows.GetCount();
60ff3b99 6276
2d66e025
MB
6277 for ( i = 0; i < numLabels; i++ )
6278 {
d10f4bf9 6279 DrawRowLabel( dc, rows[i] );
60ff3b99 6280 }
f85afd4e
MB
6281}
6282
6283
2d66e025 6284void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 6285{
7c1cb261
VZ
6286 if ( GetRowHeight(row) <= 0 )
6287 return;
60ff3b99 6288
7c1cb261
VZ
6289 int rowTop = GetRowTop(row),
6290 rowBottom = GetRowBottom(row) - 1;
b99be8fb 6291
2d66e025 6292 dc.SetPen( *wxBLACK_PEN );
b0d6ff2f 6293 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7c1cb261 6294 m_rowLabelWidth-1, rowBottom );
b99be8fb 6295
7c1cb261 6296 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
b99be8fb 6297
2d66e025 6298 dc.SetPen( *wxWHITE_PEN );
7c1cb261 6299 dc.DrawLine( 0, rowTop, 0, rowBottom );
b0d6ff2f 6300 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
60ff3b99 6301
f85afd4e 6302 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
6303 dc.SetTextForeground( GetLabelTextColour() );
6304 dc.SetFont( GetLabelFont() );
8f177c8e 6305
f85afd4e 6306 int hAlign, vAlign;
2d66e025 6307 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 6308
2d66e025
MB
6309 wxRect rect;
6310 rect.SetX( 2 );
7c1cb261 6311 rect.SetY( GetRowTop(row) + 2 );
2d66e025 6312 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 6313 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 6314 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
6315}
6316
6317
d10f4bf9 6318void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 6319{
f6bcfd97 6320 if ( !m_numCols ) return;
60ff3b99 6321
2d66e025 6322 size_t i;
d10f4bf9 6323 size_t numLabels = cols.GetCount();
60ff3b99 6324
2d66e025 6325 for ( i = 0; i < numLabels; i++ )
f85afd4e 6326 {
d10f4bf9 6327 DrawColLabel( dc, cols[i] );
60ff3b99 6328 }
f85afd4e
MB
6329}
6330
6331
2d66e025 6332void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 6333{
7c1cb261
VZ
6334 if ( GetColWidth(col) <= 0 )
6335 return;
60ff3b99 6336
7c1cb261
VZ
6337 int colLeft = GetColLeft(col),
6338 colRight = GetColRight(col) - 1;
b99be8fb 6339
2d66e025 6340 dc.SetPen( *wxBLACK_PEN );
7c1cb261
VZ
6341 dc.DrawLine( colRight, 0,
6342 colRight, m_colLabelHeight-1 );
b99be8fb 6343
b0d6ff2f 6344 dc.DrawLine( colLeft, m_colLabelHeight-1,
7c1cb261 6345 colRight, m_colLabelHeight-1 );
b99be8fb 6346
f85afd4e 6347 dc.SetPen( *wxWHITE_PEN );
b0d6ff2f 6348 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
7c1cb261 6349 dc.DrawLine( colLeft, 0, colRight, 0 );
f85afd4e 6350
b0d6ff2f
MB
6351 dc.SetBackgroundMode( wxTRANSPARENT );
6352 dc.SetTextForeground( GetLabelTextColour() );
6353 dc.SetFont( GetLabelFont() );
8f177c8e 6354
f85afd4e 6355 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
6356 dc.SetTextForeground( GetLabelTextColour() );
6357 dc.SetFont( GetLabelFont() );
8f177c8e 6358
f85afd4e 6359 int hAlign, vAlign;
2d66e025 6360 GetColLabelAlignment( &hAlign, &vAlign );
60ff3b99 6361
2d66e025 6362 wxRect rect;
7c1cb261 6363 rect.SetX( colLeft + 2 );
2d66e025 6364 rect.SetY( 2 );
7c1cb261 6365 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 6366 rect.SetHeight( m_colLabelHeight - 4 );
60ff3b99 6367 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
f85afd4e
MB
6368}
6369
2d66e025
MB
6370void wxGrid::DrawTextRectangle( wxDC& dc,
6371 const wxString& value,
6372 const wxRect& rect,
6373 int horizAlign,
6374 int vertAlign )
f85afd4e 6375{
2d66e025 6376 wxArrayString lines;
f85afd4e 6377
2d66e025
MB
6378 dc.SetClippingRegion( rect );
6379 StringToLines( value, lines );
d10f4bf9
VZ
6380
6381
6382 //Forward to new API.
6383 DrawTextRectangle( dc,
6384 lines,
6385 rect,
6386 horizAlign,
6387 vertAlign );
6388
6389}
6390
6391void wxGrid::DrawTextRectangle( wxDC& dc,
6392 const wxArrayString& lines,
6393 const wxRect& rect,
6394 int horizAlign,
6395 int vertAlign )
6396{
6397 long textWidth, textHeight;
6398 long lineWidth, lineHeight;
6399
2d66e025
MB
6400 if ( lines.GetCount() )
6401 {
6402 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
6403 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
f85afd4e 6404
2d66e025
MB
6405 float x, y;
6406 switch ( horizAlign )
6407 {
4c7277db 6408 case wxALIGN_RIGHT:
2d66e025
MB
6409 x = rect.x + (rect.width - textWidth - 1);
6410 break;
f85afd4e 6411
4c7277db 6412 case wxALIGN_CENTRE:
2d66e025
MB
6413 x = rect.x + ((rect.width - textWidth)/2);
6414 break;
f85afd4e 6415
4c7277db 6416 case wxALIGN_LEFT:
2d66e025
MB
6417 default:
6418 x = rect.x + 1;
6419 break;
6420 }
f85afd4e 6421
2d66e025
MB
6422 switch ( vertAlign )
6423 {
4c7277db 6424 case wxALIGN_BOTTOM:
2d66e025
MB
6425 y = rect.y + (rect.height - textHeight - 1);
6426 break;
f85afd4e 6427
4c7277db 6428 case wxALIGN_CENTRE:
8f177c8e 6429 y = rect.y + ((rect.height - textHeight)/2);
f85afd4e
MB
6430 break;
6431
4c7277db 6432 case wxALIGN_TOP:
f85afd4e 6433 default:
8f177c8e 6434 y = rect.y + 1;
f85afd4e
MB
6435 break;
6436 }
6437
b540eb2b 6438 for ( size_t i = 0; i < lines.GetCount(); i++ )
f85afd4e 6439 {
4a64bee4 6440 dc.DrawText( lines[i], (int)x, (int)y );
f85afd4e
MB
6441 y += lineHeight;
6442 }
6443 }
8f177c8e 6444
f85afd4e 6445 dc.DestroyClippingRegion();
f85afd4e
MB
6446}
6447
6448
6449// Split multi line text up into an array of strings. Any existing
6450// contents of the string array are preserved.
6451//
6452void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
6453{
f85afd4e
MB
6454 int startPos = 0;
6455 int pos;
6d004f67 6456 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 6457 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 6458
2433bb2e 6459 while ( startPos < (int)tVal.Length() )
f85afd4e 6460 {
2433bb2e 6461 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
6462 if ( pos < 0 )
6463 {
6464 break;
6465 }
6466 else if ( pos == 0 )
6467 {
6468 lines.Add( wxEmptyString );
6469 }
6470 else
6471 {
2433bb2e 6472 lines.Add( value.Mid(startPos, pos) );
f85afd4e
MB
6473 }
6474 startPos += pos+1;
6475 }
b540eb2b 6476 if ( startPos < (int)value.Length() )
f85afd4e
MB
6477 {
6478 lines.Add( value.Mid( startPos ) );
6479 }
6480}
6481
6482
6483void wxGrid::GetTextBoxSize( wxDC& dc,
d10f4bf9 6484 const wxArrayString& lines,
f85afd4e
MB
6485 long *width, long *height )
6486{
6487 long w = 0;
6488 long h = 0;
6489 long lineW, lineH;
6490
b540eb2b 6491 size_t i;
f85afd4e
MB
6492 for ( i = 0; i < lines.GetCount(); i++ )
6493 {
6494 dc.GetTextExtent( lines[i], &lineW, &lineH );
6495 w = wxMax( w, lineW );
6496 h += lineH;
6497 }
6498
6499 *width = w;
6500 *height = h;
6501}
6502
f6bcfd97
BP
6503//
6504// ------ Batch processing.
6505//
6506void wxGrid::EndBatch()
6507{
6508 if ( m_batchCount > 0 )
6509 {
6510 m_batchCount--;
6511 if ( !m_batchCount )
6512 {
6513 CalcDimensions();
6514 m_rowLabelWin->Refresh();
6515 m_colLabelWin->Refresh();
6516 m_cornerLabelWin->Refresh();
6517 m_gridWin->Refresh();
6518 }
6519 }
6520}
f85afd4e 6521
d8232393
MB
6522// Use this, rather than wxWindow::Refresh(), to force an immediate
6523// repainting of the grid. Has no effect if you are already inside a
6524// BeginBatch / EndBatch block.
6525//
6526void wxGrid::ForceRefresh()
6527{
6528 BeginBatch();
6529 EndBatch();
6530}
6531
6532
f85afd4e 6533//
2d66e025 6534// ------ Edit control functions
f85afd4e
MB
6535//
6536
2d66e025
MB
6537
6538void wxGrid::EnableEditing( bool edit )
f85afd4e 6539{
2d66e025
MB
6540 // TODO: improve this ?
6541 //
6542 if ( edit != m_editable )
f85afd4e 6543 {
632efa47 6544 if(!edit) EnableCellEditControl(edit);
2d66e025 6545 m_editable = edit;
f85afd4e 6546 }
f85afd4e
MB
6547}
6548
6549
2d66e025 6550void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 6551{
2c9a89e0
RD
6552 if (! m_editable)
6553 return;
6554
dcfe4c3d
SN
6555 if ( m_currentCellCoords == wxGridNoCellCoords )
6556 SetCurrentCell( 0, 0 );
60ff3b99 6557
2c9a89e0
RD
6558 if ( enable != m_cellEditCtrlEnabled )
6559 {
b54ba671
VZ
6560 // TODO allow the app to Veto() this event?
6561 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
6562
dcfe4c3d 6563 if ( enable )
f85afd4e 6564 {
b54ba671
VZ
6565 // this should be checked by the caller!
6566 wxASSERT_MSG( CanEnableCellControl(),
6567 _T("can't enable editing for this cell!") );
6568
6569 // do it before ShowCellEditControl()
dcfe4c3d 6570 m_cellEditCtrlEnabled = enable;
b54ba671 6571
2d66e025 6572 ShowCellEditControl();
2d66e025
MB
6573 }
6574 else
6575 {
2d66e025
MB
6576 HideCellEditControl();
6577 SaveEditControlValue();
b54ba671
VZ
6578
6579 // do it after HideCellEditControl()
dcfe4c3d 6580 m_cellEditCtrlEnabled = enable;
f85afd4e 6581 }
f85afd4e 6582 }
f85afd4e 6583}
f85afd4e 6584
b54ba671 6585bool wxGrid::IsCurrentCellReadOnly() const
283b7808 6586{
b54ba671
VZ
6587 // const_cast
6588 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
6589 bool readonly = attr->IsReadOnly();
6590 attr->DecRef();
283b7808 6591
b54ba671
VZ
6592 return readonly;
6593}
283b7808 6594
b54ba671
VZ
6595bool wxGrid::CanEnableCellControl() const
6596{
6597 return m_editable && !IsCurrentCellReadOnly();
6598}
6599
6600bool wxGrid::IsCellEditControlEnabled() const
6601{
6602 // the cell edit control might be disable for all cells or just for the
6603 // current one if it's read only
6604 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
283b7808
VZ
6605}
6606
f6bcfd97
BP
6607bool wxGrid::IsCellEditControlShown() const
6608{
6609 bool isShown = FALSE;
6610
6611 if ( m_cellEditCtrlEnabled )
6612 {
6613 int row = m_currentCellCoords.GetRow();
6614 int col = m_currentCellCoords.GetCol();
6615 wxGridCellAttr* attr = GetCellAttr(row, col);
6616 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
6617 attr->DecRef();
6618
6619 if ( editor )
6620 {
6621 if ( editor->IsCreated() )
6622 {
6623 isShown = editor->GetControl()->IsShown();
6624 }
6625
6626 editor->DecRef();
6627 }
6628 }
6629
6630 return isShown;
6631}
6632
2d66e025 6633void wxGrid::ShowCellEditControl()
f85afd4e 6634{
2d66e025 6635 if ( IsCellEditControlEnabled() )
f85afd4e 6636 {
2d66e025
MB
6637 if ( !IsVisible( m_currentCellCoords ) )
6638 {
3ca6a5f0 6639 m_cellEditCtrlEnabled = FALSE;
2d66e025
MB
6640 return;
6641 }
6642 else
6643 {
2c9a89e0
RD
6644 wxRect rect = CellToRect( m_currentCellCoords );
6645 int row = m_currentCellCoords.GetRow();
6646 int col = m_currentCellCoords.GetCol();
2d66e025
MB
6647
6648 // convert to scrolled coords
6649 //
99306db2 6650 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
508011ce 6651
99306db2
VZ
6652 // done in PaintBackground()
6653#if 0
6654 // erase the highlight and the cell contents because the editor
6655 // might not cover the entire cell
6656 wxClientDC dc( m_gridWin );
6657 PrepareDC( dc );
6658 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6659 dc.SetPen(*wxTRANSPARENT_PEN);
6660 dc.DrawRectangle(rect);
6661#endif // 0
d2fdd8d2 6662
99306db2
VZ
6663 // cell is shifted by one pixel
6664 rect.x--;
6665 rect.y--;
f0102d2a 6666
508011ce 6667 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6668 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
6669 if ( !editor->IsCreated() )
6670 {
2c9a89e0
RD
6671 editor->Create(m_gridWin, -1,
6672 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
6673
6674 wxGridEditorCreatedEvent evt(GetId(),
6675 wxEVT_GRID_EDITOR_CREATED,
6676 this,
6677 row,
6678 col,
6679 editor->GetControl());
6680 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
6681 }
6682
b0e282b3 6683 editor->Show( TRUE, attr );
0b190b0f 6684
2c9a89e0 6685 editor->SetSize( rect );
99306db2 6686
3da93aae 6687 editor->BeginEdit(row, col, this);
0b190b0f
VZ
6688
6689 editor->DecRef();
2c9a89e0
RD
6690 attr->DecRef();
6691 }
f85afd4e
MB
6692 }
6693}
6694
6695
2d66e025 6696void wxGrid::HideCellEditControl()
f85afd4e 6697{
2d66e025 6698 if ( IsCellEditControlEnabled() )
f85afd4e 6699 {
2c9a89e0
RD
6700 int row = m_currentCellCoords.GetRow();
6701 int col = m_currentCellCoords.GetCol();
6702
3da93aae 6703 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f
VZ
6704 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6705 editor->Show( FALSE );
6706 editor->DecRef();
2c9a89e0
RD
6707 attr->DecRef();
6708 m_gridWin->SetFocus();
f6bcfd97
BP
6709 wxRect rect( CellToRect( row, col ) );
6710 m_gridWin->Refresh( FALSE, &rect );
f85afd4e 6711 }
2d66e025 6712}
8f177c8e 6713
2d66e025 6714
2d66e025
MB
6715void wxGrid::SaveEditControlValue()
6716{
3da93aae
VZ
6717 if ( IsCellEditControlEnabled() )
6718 {
2c9a89e0
RD
6719 int row = m_currentCellCoords.GetRow();
6720 int col = m_currentCellCoords.GetCol();
8f177c8e 6721
3da93aae 6722 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6723 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 6724 bool changed = editor->EndEdit(row, col, this);
2d66e025 6725
0b190b0f 6726 editor->DecRef();
2c9a89e0 6727 attr->DecRef();
2d66e025 6728
3da93aae
VZ
6729 if (changed)
6730 {
b54ba671 6731 SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025
MB
6732 m_currentCellCoords.GetRow(),
6733 m_currentCellCoords.GetCol() );
6734 }
f85afd4e
MB
6735 }
6736}
6737
2d66e025
MB
6738
6739//
60ff3b99
VZ
6740// ------ Grid location functions
6741// Note that all of these functions work with the logical coordinates of
2d66e025
MB
6742// grid cells and labels so you will need to convert from device
6743// coordinates for mouse events etc.
6744//
6745
6746void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 6747{
58dd5b3b
MB
6748 int row = YToRow(y);
6749 int col = XToCol(x);
6750
6751 if ( row == -1 || col == -1 )
6752 {
6753 coords = wxGridNoCellCoords;
6754 }
6755 else
6756 {
6757 coords.Set( row, col );
6758 }
2d66e025 6759}
f85afd4e 6760
8f177c8e 6761
2d66e025
MB
6762int wxGrid::YToRow( int y )
6763{
6764 int i;
8f177c8e 6765
2d66e025 6766 for ( i = 0; i < m_numRows; i++ )
f85afd4e 6767 {
7c1cb261
VZ
6768 if ( y < GetRowBottom(i) )
6769 return i;
f85afd4e 6770 }
2d66e025 6771
4ee5fc9c 6772 return -1;
f85afd4e
MB
6773}
6774
2d66e025
MB
6775
6776int wxGrid::XToCol( int x )
f85afd4e 6777{
2d66e025 6778 int i;
8f177c8e 6779
2d66e025 6780 for ( i = 0; i < m_numCols; i++ )
f85afd4e 6781 {
7c1cb261
VZ
6782 if ( x < GetColRight(i) )
6783 return i;
f85afd4e
MB
6784 }
6785
4ee5fc9c 6786 return -1;
2d66e025 6787}
8f177c8e 6788
2d66e025
MB
6789
6790// return the row number that that the y coord is near the edge of, or
6791// -1 if not near an edge
6792//
6793int wxGrid::YToEdgeOfRow( int y )
6794{
6795 int i, d;
6796
6797 for ( i = 0; i < m_numRows; i++ )
6798 {
7c1cb261 6799 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 6800 {
7c1cb261
VZ
6801 d = abs( y - GetRowBottom(i) );
6802 if ( d < WXGRID_LABEL_EDGE_ZONE )
6803 return i;
f85afd4e 6804 }
f85afd4e 6805 }
2d66e025
MB
6806
6807 return -1;
6808}
6809
6810
6811// return the col number that that the x coord is near the edge of, or
6812// -1 if not near an edge
6813//
6814int wxGrid::XToEdgeOfCol( int x )
6815{
6816 int i, d;
6817
6818 for ( i = 0; i < m_numCols; i++ )
f85afd4e 6819 {
7c1cb261 6820 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 6821 {
7c1cb261
VZ
6822 d = abs( x - GetColRight(i) );
6823 if ( d < WXGRID_LABEL_EDGE_ZONE )
6824 return i;
2d66e025 6825 }
f85afd4e 6826 }
2d66e025
MB
6827
6828 return -1;
f85afd4e
MB
6829}
6830
2d66e025
MB
6831
6832wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 6833{
2d66e025 6834 wxRect rect( -1, -1, -1, -1 );
f85afd4e 6835
2d66e025
MB
6836 if ( row >= 0 && row < m_numRows &&
6837 col >= 0 && col < m_numCols )
f85afd4e 6838 {
7c1cb261
VZ
6839 rect.x = GetColLeft(col);
6840 rect.y = GetRowTop(row);
6841 rect.width = GetColWidth(col);
6842 rect.height = GetRowHeight(row);
f85afd4e
MB
6843 }
6844
f6bcfd97
BP
6845 // if grid lines are enabled, then the area of the cell is a bit smaller
6846 if (m_gridLinesEnabled) {
6847 rect.width -= 1;
6848 rect.height -= 1;
6849 }
2d66e025
MB
6850 return rect;
6851}
6852
6853
6854bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
6855{
6856 // get the cell rectangle in logical coords
6857 //
6858 wxRect r( CellToRect( row, col ) );
60ff3b99 6859
2d66e025
MB
6860 // convert to device coords
6861 //
6862 int left, top, right, bottom;
6863 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6864 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6865
2d66e025
MB
6866 // check against the client area of the grid window
6867 //
6868 int cw, ch;
6869 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6870
2d66e025 6871 if ( wholeCellVisible )
f85afd4e 6872 {
2d66e025 6873 // is the cell wholly visible ?
8f177c8e 6874 //
2d66e025
MB
6875 return ( left >= 0 && right <= cw &&
6876 top >= 0 && bottom <= ch );
6877 }
6878 else
6879 {
6880 // is the cell partly visible ?
6881 //
6882 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
6883 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
6884 }
6885}
6886
6887
6888// make the specified cell location visible by doing a minimal amount
6889// of scrolling
6890//
6891void wxGrid::MakeCellVisible( int row, int col )
6892{
6893 int i;
60ff3b99 6894 int xpos = -1, ypos = -1;
2d66e025
MB
6895
6896 if ( row >= 0 && row < m_numRows &&
6897 col >= 0 && col < m_numCols )
6898 {
6899 // get the cell rectangle in logical coords
6900 //
6901 wxRect r( CellToRect( row, col ) );
60ff3b99 6902
2d66e025
MB
6903 // convert to device coords
6904 //
6905 int left, top, right, bottom;
6906 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6907 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6908
2d66e025
MB
6909 int cw, ch;
6910 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6911
2d66e025 6912 if ( top < 0 )
3f296516 6913 {
2d66e025 6914 ypos = r.GetTop();
3f296516 6915 }
2d66e025
MB
6916 else if ( bottom > ch )
6917 {
6918 int h = r.GetHeight();
6919 ypos = r.GetTop();
6920 for ( i = row-1; i >= 0; i-- )
6921 {
7c1cb261
VZ
6922 int rowHeight = GetRowHeight(i);
6923 if ( h + rowHeight > ch )
6924 break;
2d66e025 6925
7c1cb261
VZ
6926 h += rowHeight;
6927 ypos -= rowHeight;
2d66e025 6928 }
f0102d2a
VZ
6929
6930 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6931 // have rounding errors (this is important, because if we do, we
6932 // might not scroll at all and some cells won't be redrawn)
6933 ypos += GRID_SCROLL_LINE / 2;
2d66e025
MB
6934 }
6935
6936 if ( left < 0 )
6937 {
6938 xpos = r.GetLeft();
6939 }
6940 else if ( right > cw )
6941 {
6942 int w = r.GetWidth();
6943 xpos = r.GetLeft();
6944 for ( i = col-1; i >= 0; i-- )
6945 {
7c1cb261
VZ
6946 int colWidth = GetColWidth(i);
6947 if ( w + colWidth > cw )
6948 break;
2d66e025 6949
7c1cb261
VZ
6950 w += colWidth;
6951 xpos -= colWidth;
2d66e025 6952 }
f0102d2a
VZ
6953
6954 // see comment for ypos above
6955 xpos += GRID_SCROLL_LINE / 2;
2d66e025
MB
6956 }
6957
6958 if ( xpos != -1 || ypos != -1 )
6959 {
f0102d2a
VZ
6960 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
6961 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
2d66e025
MB
6962 Scroll( xpos, ypos );
6963 AdjustScrollbars();
6964 }
6965 }
6966}
6967
6968
6969//
6970// ------ Grid cursor movement functions
6971//
6972
5c8fc7c1 6973bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025
MB
6974{
6975 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 6976 m_currentCellCoords.GetRow() >= 0 )
2d66e025 6977 {
f6bcfd97 6978 if ( expandSelection)
d95b0c2b
SN
6979 {
6980 if ( m_selectingKeyboard == wxGridNoCellCoords )
6981 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
6982 if ( m_selectingKeyboard.GetRow() > 0 )
6983 {
6984 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
6985 MakeCellVisible( m_selectingKeyboard.GetRow(),
6986 m_selectingKeyboard.GetCol() );
c9097836 6987 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 6988 }
d95b0c2b 6989 }
f6bcfd97 6990 else if ( m_currentCellCoords.GetRow() > 0 )
aa5e1f75
SN
6991 {
6992 ClearSelection();
6993 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
6994 m_currentCellCoords.GetCol() );
d95b0c2b
SN
6995 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
6996 m_currentCellCoords.GetCol() );
aa5e1f75 6997 }
f6bcfd97
BP
6998 else
6999 return FALSE;
8f177c8e 7000 return TRUE;
f85afd4e 7001 }
2d66e025
MB
7002
7003 return FALSE;
7004}
7005
7006
5c8fc7c1 7007bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025 7008{
2d66e025 7009 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7010 m_currentCellCoords.GetRow() < m_numRows )
f85afd4e 7011 {
5c8fc7c1 7012 if ( expandSelection )
d95b0c2b
SN
7013 {
7014 if ( m_selectingKeyboard == wxGridNoCellCoords )
7015 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7016 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
7017 {
7018 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
7019 MakeCellVisible( m_selectingKeyboard.GetRow(),
7020 m_selectingKeyboard.GetCol() );
c9097836 7021 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7022 }
d95b0c2b 7023 }
f6bcfd97 7024 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
aa5e1f75
SN
7025 {
7026 ClearSelection();
7027 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
7028 m_currentCellCoords.GetCol() );
d95b0c2b
SN
7029 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
7030 m_currentCellCoords.GetCol() );
aa5e1f75 7031 }
f6bcfd97
BP
7032 else
7033 return FALSE;
2d66e025 7034 return TRUE;
f85afd4e 7035 }
2d66e025
MB
7036
7037 return FALSE;
f85afd4e
MB
7038}
7039
2d66e025 7040
5c8fc7c1 7041bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 7042{
2d66e025 7043 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7044 m_currentCellCoords.GetCol() >= 0 )
2d66e025 7045 {
5c8fc7c1 7046 if ( expandSelection )
d95b0c2b
SN
7047 {
7048 if ( m_selectingKeyboard == wxGridNoCellCoords )
7049 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7050 if ( m_selectingKeyboard.GetCol() > 0 )
7051 {
7052 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
7053 MakeCellVisible( m_selectingKeyboard.GetRow(),
7054 m_selectingKeyboard.GetCol() );
c9097836 7055 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7056 }
d95b0c2b 7057 }
f6bcfd97 7058 else if ( m_currentCellCoords.GetCol() > 0 )
aa5e1f75
SN
7059 {
7060 ClearSelection();
7061 MakeCellVisible( m_currentCellCoords.GetRow(),
7062 m_currentCellCoords.GetCol() - 1 );
d95b0c2b
SN
7063 SetCurrentCell( m_currentCellCoords.GetRow(),
7064 m_currentCellCoords.GetCol() - 1 );
aa5e1f75 7065 }
f6bcfd97
BP
7066 else
7067 return FALSE;
2d66e025
MB
7068 return TRUE;
7069 }
7070
7071 return FALSE;
7072}
7073
7074
5c8fc7c1 7075bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025
MB
7076{
7077 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 7078 m_currentCellCoords.GetCol() < m_numCols )
f85afd4e 7079 {
5c8fc7c1 7080 if ( expandSelection )
d95b0c2b
SN
7081 {
7082 if ( m_selectingKeyboard == wxGridNoCellCoords )
7083 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
7084 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
7085 {
7086 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
7087 MakeCellVisible( m_selectingKeyboard.GetRow(),
7088 m_selectingKeyboard.GetCol() );
c9097836 7089 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 7090 }
d95b0c2b 7091 }
f6bcfd97 7092 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
aa5e1f75
SN
7093 {
7094 ClearSelection();
7095 MakeCellVisible( m_currentCellCoords.GetRow(),
7096 m_currentCellCoords.GetCol() + 1 );
7097 SetCurrentCell( m_currentCellCoords.GetRow(),
d95b0c2b 7098 m_currentCellCoords.GetCol() + 1 );
aa5e1f75 7099 }
f6bcfd97
BP
7100 else
7101 return FALSE;
2d66e025 7102 return TRUE;
f85afd4e 7103 }
8f177c8e 7104
2d66e025
MB
7105 return FALSE;
7106}
7107
7108
7109bool wxGrid::MovePageUp()
7110{
7111 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 7112
2d66e025
MB
7113 int row = m_currentCellCoords.GetRow();
7114 if ( row > 0 )
f85afd4e 7115 {
2d66e025
MB
7116 int cw, ch;
7117 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7118
7c1cb261 7119 int y = GetRowTop(row);
2d66e025
MB
7120 int newRow = YToRow( y - ch + 1 );
7121 if ( newRow == -1 )
7122 {
7123 newRow = 0;
7124 }
60ff3b99 7125 else if ( newRow == row )
2d66e025
MB
7126 {
7127 newRow = row - 1;
7128 }
8f177c8e 7129
2d66e025
MB
7130 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7131 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 7132
f85afd4e
MB
7133 return TRUE;
7134 }
2d66e025
MB
7135
7136 return FALSE;
7137}
7138
7139bool wxGrid::MovePageDown()
7140{
7141 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 7142
2d66e025
MB
7143 int row = m_currentCellCoords.GetRow();
7144 if ( row < m_numRows )
f85afd4e 7145 {
2d66e025
MB
7146 int cw, ch;
7147 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 7148
7c1cb261 7149 int y = GetRowTop(row);
2d66e025
MB
7150 int newRow = YToRow( y + ch );
7151 if ( newRow == -1 )
7152 {
7153 newRow = m_numRows - 1;
7154 }
60ff3b99 7155 else if ( newRow == row )
2d66e025
MB
7156 {
7157 newRow = row + 1;
7158 }
7159
7160 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7161 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 7162
2d66e025 7163 return TRUE;
f85afd4e 7164 }
2d66e025
MB
7165
7166 return FALSE;
f85afd4e 7167}
8f177c8e 7168
5c8fc7c1 7169bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
7170{
7171 if ( m_table &&
7172 m_currentCellCoords != wxGridNoCellCoords &&
7173 m_currentCellCoords.GetRow() > 0 )
7174 {
7175 int row = m_currentCellCoords.GetRow();
7176 int col = m_currentCellCoords.GetCol();
7177
7178 if ( m_table->IsEmptyCell(row, col) )
7179 {
7180 // starting in an empty cell: find the next block of
7181 // non-empty cells
7182 //
7183 while ( row > 0 )
7184 {
7185 row-- ;
7186 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7187 }
7188 }
7189 else if ( m_table->IsEmptyCell(row-1, col) )
7190 {
7191 // starting at the top of a block: find the next block
7192 //
7193 row--;
7194 while ( row > 0 )
7195 {
7196 row-- ;
7197 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7198 }
7199 }
7200 else
7201 {
7202 // starting within a block: find the top of the block
7203 //
7204 while ( row > 0 )
7205 {
7206 row-- ;
7207 if ( m_table->IsEmptyCell(row, col) )
7208 {
7209 row++ ;
7210 break;
7211 }
7212 }
7213 }
f85afd4e 7214
2d66e025 7215 MakeCellVisible( row, col );
5c8fc7c1 7216 if ( expandSelection )
d95b0c2b
SN
7217 {
7218 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7219 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7220 }
7221 else
aa5e1f75
SN
7222 {
7223 ClearSelection();
7224 SetCurrentCell( row, col );
7225 }
2d66e025
MB
7226 return TRUE;
7227 }
f85afd4e 7228
2d66e025
MB
7229 return FALSE;
7230}
f85afd4e 7231
5c8fc7c1 7232bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 7233{
2d66e025
MB
7234 if ( m_table &&
7235 m_currentCellCoords != wxGridNoCellCoords &&
7236 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 7237 {
2d66e025
MB
7238 int row = m_currentCellCoords.GetRow();
7239 int col = m_currentCellCoords.GetCol();
7240
7241 if ( m_table->IsEmptyCell(row, col) )
7242 {
7243 // starting in an empty cell: find the next block of
7244 // non-empty cells
7245 //
7246 while ( row < m_numRows-1 )
7247 {
7248 row++ ;
7249 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7250 }
7251 }
7252 else if ( m_table->IsEmptyCell(row+1, col) )
7253 {
7254 // starting at the bottom of a block: find the next block
7255 //
7256 row++;
7257 while ( row < m_numRows-1 )
7258 {
7259 row++ ;
7260 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7261 }
7262 }
7263 else
7264 {
7265 // starting within a block: find the bottom of the block
7266 //
7267 while ( row < m_numRows-1 )
7268 {
7269 row++ ;
7270 if ( m_table->IsEmptyCell(row, col) )
7271 {
7272 row-- ;
7273 break;
7274 }
7275 }
7276 }
7277
7278 MakeCellVisible( row, col );
5c8fc7c1 7279 if ( expandSelection )
d95b0c2b
SN
7280 {
7281 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7282 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7283 }
7284 else
aa5e1f75
SN
7285 {
7286 ClearSelection();
7287 SetCurrentCell( row, col );
7288 }
2d66e025
MB
7289
7290 return TRUE;
f85afd4e 7291 }
f85afd4e 7292
2d66e025
MB
7293 return FALSE;
7294}
f85afd4e 7295
5c8fc7c1 7296bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 7297{
2d66e025
MB
7298 if ( m_table &&
7299 m_currentCellCoords != wxGridNoCellCoords &&
7300 m_currentCellCoords.GetCol() > 0 )
f85afd4e 7301 {
2d66e025
MB
7302 int row = m_currentCellCoords.GetRow();
7303 int col = m_currentCellCoords.GetCol();
7304
7305 if ( m_table->IsEmptyCell(row, col) )
7306 {
7307 // starting in an empty cell: find the next block of
7308 // non-empty cells
7309 //
7310 while ( col > 0 )
7311 {
7312 col-- ;
7313 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7314 }
7315 }
7316 else if ( m_table->IsEmptyCell(row, col-1) )
7317 {
7318 // starting at the left of a block: find the next block
7319 //
7320 col--;
7321 while ( col > 0 )
7322 {
7323 col-- ;
7324 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7325 }
7326 }
7327 else
7328 {
7329 // starting within a block: find the left of the block
7330 //
7331 while ( col > 0 )
7332 {
7333 col-- ;
7334 if ( m_table->IsEmptyCell(row, col) )
7335 {
7336 col++ ;
7337 break;
7338 }
7339 }
7340 }
f85afd4e 7341
2d66e025 7342 MakeCellVisible( row, col );
5c8fc7c1 7343 if ( expandSelection )
d95b0c2b
SN
7344 {
7345 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7346 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7347 }
7348 else
aa5e1f75
SN
7349 {
7350 ClearSelection();
7351 SetCurrentCell( row, col );
7352 }
8f177c8e 7353
2d66e025 7354 return TRUE;
f85afd4e 7355 }
2d66e025
MB
7356
7357 return FALSE;
f85afd4e
MB
7358}
7359
5c8fc7c1 7360bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 7361{
2d66e025
MB
7362 if ( m_table &&
7363 m_currentCellCoords != wxGridNoCellCoords &&
7364 m_currentCellCoords.GetCol() < m_numCols-1 )
f85afd4e 7365 {
2d66e025
MB
7366 int row = m_currentCellCoords.GetRow();
7367 int col = m_currentCellCoords.GetCol();
8f177c8e 7368
2d66e025
MB
7369 if ( m_table->IsEmptyCell(row, col) )
7370 {
7371 // starting in an empty cell: find the next block of
7372 // non-empty cells
7373 //
7374 while ( col < m_numCols-1 )
7375 {
7376 col++ ;
7377 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7378 }
7379 }
7380 else if ( m_table->IsEmptyCell(row, col+1) )
7381 {
7382 // starting at the right of a block: find the next block
7383 //
7384 col++;
7385 while ( col < m_numCols-1 )
7386 {
7387 col++ ;
7388 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7389 }
7390 }
7391 else
7392 {
7393 // starting within a block: find the right of the block
7394 //
7395 while ( col < m_numCols-1 )
7396 {
7397 col++ ;
7398 if ( m_table->IsEmptyCell(row, col) )
7399 {
7400 col-- ;
7401 break;
7402 }
7403 }
7404 }
8f177c8e 7405
2d66e025 7406 MakeCellVisible( row, col );
5c8fc7c1 7407 if ( expandSelection )
d95b0c2b
SN
7408 {
7409 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 7410 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
7411 }
7412 else
aa5e1f75
SN
7413 {
7414 ClearSelection();
7415 SetCurrentCell( row, col );
7416 }
f85afd4e 7417
2d66e025 7418 return TRUE;
f85afd4e 7419 }
2d66e025
MB
7420
7421 return FALSE;
f85afd4e
MB
7422}
7423
7424
2d66e025 7425
f85afd4e 7426//
2d66e025 7427// ------ Label values and formatting
f85afd4e
MB
7428//
7429
7430void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
7431{
7432 *horiz = m_rowLabelHorizAlign;
7433 *vert = m_rowLabelVertAlign;
7434}
7435
7436void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
7437{
7438 *horiz = m_colLabelHorizAlign;
7439 *vert = m_colLabelVertAlign;
7440}
7441
7442wxString wxGrid::GetRowLabelValue( int row )
7443{
7444 if ( m_table )
7445 {
7446 return m_table->GetRowLabelValue( row );
7447 }
7448 else
7449 {
7450 wxString s;
7451 s << row;
7452 return s;
7453 }
7454}
7455
7456wxString wxGrid::GetColLabelValue( int col )
7457{
7458 if ( m_table )
7459 {
7460 return m_table->GetColLabelValue( col );
7461 }
7462 else
7463 {
7464 wxString s;
7465 s << col;
7466 return s;
7467 }
7468}
7469
2e8cd977 7470
f85afd4e
MB
7471void wxGrid::SetRowLabelSize( int width )
7472{
2e8cd977
MB
7473 width = wxMax( width, 0 );
7474 if ( width != m_rowLabelWidth )
7475 {
2e8cd977
MB
7476 if ( width == 0 )
7477 {
7478 m_rowLabelWin->Show( FALSE );
7807d81c 7479 m_cornerLabelWin->Show( FALSE );
2e8cd977 7480 }
7807d81c 7481 else if ( m_rowLabelWidth == 0 )
2e8cd977 7482 {
7807d81c
MB
7483 m_rowLabelWin->Show( TRUE );
7484 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 7485 }
b99be8fb 7486
2e8cd977 7487 m_rowLabelWidth = width;
7807d81c
MB
7488 CalcWindowSizes();
7489 Refresh( TRUE );
2e8cd977 7490 }
f85afd4e
MB
7491}
7492
2e8cd977 7493
f85afd4e
MB
7494void wxGrid::SetColLabelSize( int height )
7495{
7807d81c 7496 height = wxMax( height, 0 );
2e8cd977
MB
7497 if ( height != m_colLabelHeight )
7498 {
2e8cd977
MB
7499 if ( height == 0 )
7500 {
2e8cd977 7501 m_colLabelWin->Show( FALSE );
7807d81c 7502 m_cornerLabelWin->Show( FALSE );
2e8cd977 7503 }
7807d81c 7504 else if ( m_colLabelHeight == 0 )
2e8cd977 7505 {
7807d81c
MB
7506 m_colLabelWin->Show( TRUE );
7507 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 7508 }
7807d81c 7509
2e8cd977 7510 m_colLabelHeight = height;
7807d81c
MB
7511 CalcWindowSizes();
7512 Refresh( TRUE );
2e8cd977 7513 }
f85afd4e
MB
7514}
7515
2e8cd977 7516
f85afd4e
MB
7517void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
7518{
2d66e025
MB
7519 if ( m_labelBackgroundColour != colour )
7520 {
7521 m_labelBackgroundColour = colour;
7522 m_rowLabelWin->SetBackgroundColour( colour );
7523 m_colLabelWin->SetBackgroundColour( colour );
7524 m_cornerLabelWin->SetBackgroundColour( colour );
7525
7526 if ( !GetBatchCount() )
7527 {
7528 m_rowLabelWin->Refresh();
7529 m_colLabelWin->Refresh();
7530 m_cornerLabelWin->Refresh();
7531 }
7532 }
f85afd4e
MB
7533}
7534
7535void wxGrid::SetLabelTextColour( const wxColour& colour )
7536{
2d66e025
MB
7537 if ( m_labelTextColour != colour )
7538 {
7539 m_labelTextColour = colour;
7540 if ( !GetBatchCount() )
7541 {
7542 m_rowLabelWin->Refresh();
7543 m_colLabelWin->Refresh();
7544 }
7545 }
f85afd4e
MB
7546}
7547
7548void wxGrid::SetLabelFont( const wxFont& font )
7549{
7550 m_labelFont = font;
2d66e025
MB
7551 if ( !GetBatchCount() )
7552 {
7553 m_rowLabelWin->Refresh();
7554 m_colLabelWin->Refresh();
7555 }
f85afd4e
MB
7556}
7557
7558void wxGrid::SetRowLabelAlignment( int horiz, int vert )
7559{
4c7277db
MB
7560 // allow old (incorrect) defs to be used
7561 switch ( horiz )
7562 {
7563 case wxLEFT: horiz = wxALIGN_LEFT; break;
7564 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7565 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7566 }
84912ef8 7567
4c7277db
MB
7568 switch ( vert )
7569 {
7570 case wxTOP: vert = wxALIGN_TOP; break;
7571 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7572 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7573 }
84912ef8 7574
4c7277db 7575 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7576 {
7577 m_rowLabelHorizAlign = horiz;
7578 }
8f177c8e 7579
4c7277db 7580 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7581 {
7582 m_rowLabelVertAlign = vert;
7583 }
7584
2d66e025
MB
7585 if ( !GetBatchCount() )
7586 {
7587 m_rowLabelWin->Refresh();
60ff3b99 7588 }
f85afd4e
MB
7589}
7590
7591void wxGrid::SetColLabelAlignment( int horiz, int vert )
7592{
4c7277db
MB
7593 // allow old (incorrect) defs to be used
7594 switch ( horiz )
7595 {
7596 case wxLEFT: horiz = wxALIGN_LEFT; break;
7597 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7598 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7599 }
84912ef8 7600
4c7277db
MB
7601 switch ( vert )
7602 {
7603 case wxTOP: vert = wxALIGN_TOP; break;
7604 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7605 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7606 }
84912ef8 7607
4c7277db 7608 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7609 {
7610 m_colLabelHorizAlign = horiz;
7611 }
8f177c8e 7612
4c7277db 7613 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7614 {
7615 m_colLabelVertAlign = vert;
7616 }
7617
2d66e025
MB
7618 if ( !GetBatchCount() )
7619 {
2d66e025 7620 m_colLabelWin->Refresh();
60ff3b99 7621 }
f85afd4e
MB
7622}
7623
7624void wxGrid::SetRowLabelValue( int row, const wxString& s )
7625{
7626 if ( m_table )
7627 {
7628 m_table->SetRowLabelValue( row, s );
2d66e025
MB
7629 if ( !GetBatchCount() )
7630 {
70c7a608
SN
7631 wxRect rect = CellToRect( row, 0);
7632 if ( rect.height > 0 )
7633 {
cb309039 7634 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 7635 rect.x = 0;
70c7a608
SN
7636 rect.width = m_rowLabelWidth;
7637 m_rowLabelWin->Refresh( TRUE, &rect );
7638 }
2d66e025 7639 }
f85afd4e
MB
7640 }
7641}
7642
7643void wxGrid::SetColLabelValue( int col, const wxString& s )
7644{
7645 if ( m_table )
7646 {
7647 m_table->SetColLabelValue( col, s );
2d66e025
MB
7648 if ( !GetBatchCount() )
7649 {
70c7a608
SN
7650 wxRect rect = CellToRect( 0, col );
7651 if ( rect.width > 0 )
7652 {
cb309039 7653 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
b1944ebc 7654 rect.y = 0;
70c7a608
SN
7655 rect.height = m_colLabelHeight;
7656 m_colLabelWin->Refresh( TRUE, &rect );
7657 }
2d66e025 7658 }
f85afd4e
MB
7659 }
7660}
7661
7662void wxGrid::SetGridLineColour( const wxColour& colour )
7663{
2d66e025
MB
7664 if ( m_gridLineColour != colour )
7665 {
7666 m_gridLineColour = colour;
60ff3b99 7667
2d66e025
MB
7668 wxClientDC dc( m_gridWin );
7669 PrepareDC( dc );
c6a51dcd 7670 DrawAllGridLines( dc, wxRegion() );
2d66e025 7671 }
f85afd4e
MB
7672}
7673
f6bcfd97
BP
7674
7675void wxGrid::SetCellHighlightColour( const wxColour& colour )
7676{
7677 if ( m_cellHighlightColour != colour )
7678 {
7679 m_cellHighlightColour = colour;
7680
7681 wxClientDC dc( m_gridWin );
7682 PrepareDC( dc );
7683 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7684 DrawCellHighlight(dc, attr);
7685 attr->DecRef();
7686 }
7687}
7688
d2520c85
RD
7689void wxGrid::SetCellHighlightPenWidth(int width)
7690{
7691 if (m_cellHighlightPenWidth != width) {
7692 m_cellHighlightPenWidth = width;
7693
7694 // Just redrawing the cell highlight is not enough since that won't
7695 // make any visible change if the the thickness is getting smaller.
7696 int row = m_currentCellCoords.GetRow();
7697 int col = m_currentCellCoords.GetCol();
7698 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7699 return;
7700 wxRect rect = CellToRect(row, col);
7701 m_gridWin->Refresh(TRUE, &rect);
7702 }
7703}
7704
7705void wxGrid::SetCellHighlightROPenWidth(int width)
7706{
7707 if (m_cellHighlightROPenWidth != width) {
7708 m_cellHighlightROPenWidth = width;
7709
7710 // Just redrawing the cell highlight is not enough since that won't
7711 // make any visible change if the the thickness is getting smaller.
7712 int row = m_currentCellCoords.GetRow();
7713 int col = m_currentCellCoords.GetCol();
7714 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7715 return;
7716 wxRect rect = CellToRect(row, col);
7717 m_gridWin->Refresh(TRUE, &rect);
7718 }
7719}
7720
f85afd4e
MB
7721void wxGrid::EnableGridLines( bool enable )
7722{
7723 if ( enable != m_gridLinesEnabled )
7724 {
7725 m_gridLinesEnabled = enable;
2d66e025
MB
7726
7727 if ( !GetBatchCount() )
7728 {
7729 if ( enable )
7730 {
7731 wxClientDC dc( m_gridWin );
7732 PrepareDC( dc );
c6a51dcd 7733 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
7734 }
7735 else
7736 {
7737 m_gridWin->Refresh();
7738 }
7739 }
f85afd4e
MB
7740 }
7741}
7742
7743
7744int wxGrid::GetDefaultRowSize()
7745{
7746 return m_defaultRowHeight;
7747}
7748
7749int wxGrid::GetRowSize( int row )
7750{
b99be8fb
VZ
7751 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
7752
7c1cb261 7753 return GetRowHeight(row);
f85afd4e
MB
7754}
7755
7756int wxGrid::GetDefaultColSize()
7757{
7758 return m_defaultColWidth;
7759}
7760
7761int wxGrid::GetColSize( int col )
7762{
b99be8fb
VZ
7763 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
7764
7c1cb261 7765 return GetColWidth(col);
f85afd4e
MB
7766}
7767
2e9a6788
VZ
7768// ============================================================================
7769// access to the grid attributes: each of them has a default value in the grid
7770// itself and may be overidden on a per-cell basis
7771// ============================================================================
7772
7773// ----------------------------------------------------------------------------
7774// setting default attributes
7775// ----------------------------------------------------------------------------
7776
7777void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
7778{
2796cce3 7779 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
7780#ifdef __WXGTK__
7781 m_gridWin->SetBackgroundColour(col);
7782#endif
2e9a6788
VZ
7783}
7784
7785void wxGrid::SetDefaultCellTextColour( const wxColour& col )
7786{
2796cce3 7787 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
7788}
7789
7790void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
7791{
2796cce3 7792 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
7793}
7794
7795void wxGrid::SetDefaultCellFont( const wxFont& font )
7796{
2796cce3
RD
7797 m_defaultCellAttr->SetFont(font);
7798}
7799
0ba143c9
RD
7800void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
7801{
7802 m_defaultCellAttr->SetRenderer(renderer);
7803}
2e9a6788 7804
0ba143c9
RD
7805void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
7806{
7807 m_defaultCellAttr->SetEditor(editor);
7808}
9b4aede2 7809
2e9a6788
VZ
7810// ----------------------------------------------------------------------------
7811// access to the default attrbiutes
7812// ----------------------------------------------------------------------------
7813
f85afd4e
MB
7814wxColour wxGrid::GetDefaultCellBackgroundColour()
7815{
2796cce3 7816 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
7817}
7818
2e9a6788
VZ
7819wxColour wxGrid::GetDefaultCellTextColour()
7820{
2796cce3 7821 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
7822}
7823
7824wxFont wxGrid::GetDefaultCellFont()
7825{
2796cce3 7826 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
7827}
7828
7829void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
7830{
2796cce3 7831 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
7832}
7833
0ba143c9
RD
7834wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
7835{
0b190b0f 7836 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 7837}
ab79958a 7838
0ba143c9
RD
7839wxGridCellEditor *wxGrid::GetDefaultEditor() const
7840{
28a77bc4 7841 return m_defaultCellAttr->GetEditor(NULL,0,0);
0ba143c9 7842}
9b4aede2 7843
2e9a6788
VZ
7844// ----------------------------------------------------------------------------
7845// access to cell attributes
7846// ----------------------------------------------------------------------------
7847
b99be8fb 7848wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 7849{
0a976765 7850 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7851 wxColour colour = attr->GetBackgroundColour();
39bcce60 7852 attr->DecRef();
b99be8fb 7853 return colour;
f85afd4e
MB
7854}
7855
b99be8fb 7856wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 7857{
0a976765 7858 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7859 wxColour colour = attr->GetTextColour();
39bcce60 7860 attr->DecRef();
b99be8fb 7861 return colour;
f85afd4e
MB
7862}
7863
b99be8fb 7864wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 7865{
0a976765 7866 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7867 wxFont font = attr->GetFont();
39bcce60 7868 attr->DecRef();
b99be8fb 7869 return font;
f85afd4e
MB
7870}
7871
b99be8fb 7872void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 7873{
0a976765 7874 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7875 attr->GetAlignment(horiz, vert);
39bcce60 7876 attr->DecRef();
2e9a6788
VZ
7877}
7878
2796cce3
RD
7879wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
7880{
7881 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7882 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 7883 attr->DecRef();
0b190b0f 7884
2796cce3
RD
7885 return renderer;
7886}
7887
9b4aede2
RD
7888wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
7889{
7890 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7891 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 7892 attr->DecRef();
0b190b0f 7893
9b4aede2
RD
7894 return editor;
7895}
7896
283b7808
VZ
7897bool wxGrid::IsReadOnly(int row, int col) const
7898{
7899 wxGridCellAttr* attr = GetCellAttr(row, col);
7900 bool isReadOnly = attr->IsReadOnly();
7901 attr->DecRef();
7902 return isReadOnly;
7903}
7904
2e9a6788 7905// ----------------------------------------------------------------------------
758cbedf 7906// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
7907// ----------------------------------------------------------------------------
7908
7909bool wxGrid::CanHaveAttributes()
7910{
7911 if ( !m_table )
7912 {
7913 return FALSE;
7914 }
7915
f2d76237 7916 return m_table->CanHaveAttributes();
2e9a6788
VZ
7917}
7918
0a976765
VZ
7919void wxGrid::ClearAttrCache()
7920{
7921 if ( m_attrCache.row != -1 )
7922 {
39bcce60 7923 wxSafeDecRef(m_attrCache.attr);
19d7140e 7924 m_attrCache.attr = NULL;
0a976765
VZ
7925 m_attrCache.row = -1;
7926 }
7927}
7928
7929void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
7930{
7931 wxGrid *self = (wxGrid *)this; // const_cast
7932
7933 self->ClearAttrCache();
7934 self->m_attrCache.row = row;
7935 self->m_attrCache.col = col;
7936 self->m_attrCache.attr = attr;
39bcce60 7937 wxSafeIncRef(attr);
0a976765
VZ
7938}
7939
7940bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
7941{
7942 if ( row == m_attrCache.row && col == m_attrCache.col )
7943 {
7944 *attr = m_attrCache.attr;
39bcce60 7945 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
7946
7947#ifdef DEBUG_ATTR_CACHE
7948 gs_nAttrCacheHits++;
7949#endif
7950
7951 return TRUE;
7952 }
7953 else
7954 {
7955#ifdef DEBUG_ATTR_CACHE
7956 gs_nAttrCacheMisses++;
7957#endif
7958 return FALSE;
7959 }
7960}
7961
2e9a6788
VZ
7962wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
7963{
0a976765
VZ
7964 wxGridCellAttr *attr;
7965 if ( !LookupAttr(row, col, &attr) )
2e9a6788 7966 {
19d7140e 7967 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any) : (wxGridCellAttr *)NULL;
0a976765
VZ
7968 CacheAttr(row, col, attr);
7969 }
508011ce
VZ
7970 if (attr)
7971 {
2796cce3 7972 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
7973 }
7974 else
7975 {
2796cce3
RD
7976 attr = m_defaultCellAttr;
7977 attr->IncRef();
7978 }
2e9a6788 7979
0a976765
VZ
7980 return attr;
7981}
7982
7983wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
7984{
19d7140e 7985 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
0a976765 7986 wxASSERT_MSG( m_table,
f6bcfd97 7987 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
0a976765 7988
19d7140e 7989 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell );
0a976765
VZ
7990 if ( !attr )
7991 {
7992 attr = new wxGridCellAttr;
7993
7994 // artificially inc the ref count to match DecRef() in caller
7995 attr->IncRef();
0a976765
VZ
7996 m_table->SetAttr(attr, row, col);
7997 }
2796cce3 7998 attr->SetDefAttr(m_defaultCellAttr);
2e9a6788
VZ
7999 return attr;
8000}
8001
0b190b0f
VZ
8002// ----------------------------------------------------------------------------
8003// setting column attributes (wrappers around SetColAttr)
8004// ----------------------------------------------------------------------------
8005
8006void wxGrid::SetColFormatBool(int col)
8007{
8008 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
8009}
8010
8011void wxGrid::SetColFormatNumber(int col)
8012{
8013 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
8014}
8015
8016void wxGrid::SetColFormatFloat(int col, int width, int precision)
8017{
8018 wxString typeName = wxGRID_VALUE_FLOAT;
8019 if ( (width != -1) || (precision != -1) )
8020 {
8021 typeName << _T(':') << width << _T(',') << precision;
8022 }
8023
8024 SetColFormatCustom(col, typeName);
8025}
8026
8027void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
8028{
19d7140e
VZ
8029 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
8030
8031 attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
8032 if(!attr)
8033 attr = new wxGridCellAttr;
0b190b0f
VZ
8034 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
8035 attr->SetRenderer(renderer);
8036
8037 SetColAttr(col, attr);
19d7140e 8038
0b190b0f
VZ
8039}
8040
758cbedf
VZ
8041// ----------------------------------------------------------------------------
8042// setting cell attributes: this is forwarded to the table
8043// ----------------------------------------------------------------------------
8044
8045void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
8046{
8047 if ( CanHaveAttributes() )
8048 {
8049 m_table->SetRowAttr(attr, row);
19d7140e 8050 ClearAttrCache();
758cbedf
VZ
8051 }
8052 else
8053 {
39bcce60 8054 wxSafeDecRef(attr);
758cbedf
VZ
8055 }
8056}
8057
8058void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
8059{
8060 if ( CanHaveAttributes() )
8061 {
8062 m_table->SetColAttr(attr, col);
19d7140e 8063 ClearAttrCache();
758cbedf
VZ
8064 }
8065 else
8066 {
39bcce60 8067 wxSafeDecRef(attr);
758cbedf
VZ
8068 }
8069}
8070
2e9a6788
VZ
8071void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
8072{
8073 if ( CanHaveAttributes() )
8074 {
0a976765 8075 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8076 attr->SetBackgroundColour(colour);
8077 attr->DecRef();
8078 }
8079}
8080
8081void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
8082{
8083 if ( CanHaveAttributes() )
8084 {
0a976765 8085 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8086 attr->SetTextColour(colour);
8087 attr->DecRef();
8088 }
8089}
8090
8091void wxGrid::SetCellFont( int row, int col, const wxFont& font )
8092{
8093 if ( CanHaveAttributes() )
8094 {
0a976765 8095 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8096 attr->SetFont(font);
8097 attr->DecRef();
8098 }
8099}
8100
8101void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
8102{
8103 if ( CanHaveAttributes() )
8104 {
0a976765 8105 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
8106 attr->SetAlignment(horiz, vert);
8107 attr->DecRef();
ab79958a
VZ
8108 }
8109}
8110
8111void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
8112{
8113 if ( CanHaveAttributes() )
8114 {
0a976765 8115 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
8116 attr->SetRenderer(renderer);
8117 attr->DecRef();
9b4aede2
RD
8118 }
8119}
8120
8121void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
8122{
8123 if ( CanHaveAttributes() )
8124 {
8125 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8126 attr->SetEditor(editor);
8127 attr->DecRef();
283b7808
VZ
8128 }
8129}
8130
8131void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
8132{
8133 if ( CanHaveAttributes() )
8134 {
8135 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8136 attr->SetReadOnly(isReadOnly);
8137 attr->DecRef();
2e9a6788 8138 }
f85afd4e
MB
8139}
8140
f2d76237
RD
8141// ----------------------------------------------------------------------------
8142// Data type registration
8143// ----------------------------------------------------------------------------
8144
8145void wxGrid::RegisterDataType(const wxString& typeName,
8146 wxGridCellRenderer* renderer,
8147 wxGridCellEditor* editor)
8148{
8149 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
8150}
8151
8152
99306db2 8153wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
8154{
8155 wxString typeName = m_table->GetTypeName(row, col);
8156 return GetDefaultEditorForType(typeName);
8157}
8158
99306db2 8159wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
8160{
8161 wxString typeName = m_table->GetTypeName(row, col);
8162 return GetDefaultRendererForType(typeName);
8163}
8164
99306db2
VZ
8165wxGridCellEditor*
8166wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 8167{
c4608a8a 8168 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8169 if ( index == wxNOT_FOUND )
8170 {
8171 wxFAIL_MSG(wxT("Unknown data type name"));
8172
f2d76237
RD
8173 return NULL;
8174 }
0b190b0f 8175
f2d76237
RD
8176 return m_typeRegistry->GetEditor(index);
8177}
8178
99306db2
VZ
8179wxGridCellRenderer*
8180wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 8181{
c4608a8a 8182 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8183 if ( index == wxNOT_FOUND )
8184 {
c4608a8a 8185 wxFAIL_MSG(wxT("Unknown data type name"));
0b190b0f 8186
c4608a8a 8187 return NULL;
e72b4213 8188 }
0b190b0f 8189
c4608a8a 8190 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
8191}
8192
8193
2e9a6788
VZ
8194// ----------------------------------------------------------------------------
8195// row/col size
8196// ----------------------------------------------------------------------------
8197
6e8524b1
MB
8198void wxGrid::EnableDragRowSize( bool enable )
8199{
8200 m_canDragRowSize = enable;
8201}
8202
8203
8204void wxGrid::EnableDragColSize( bool enable )
8205{
8206 m_canDragColSize = enable;
8207}
8208
4cfa5de6
RD
8209void wxGrid::EnableDragGridSize( bool enable )
8210{
8211 m_canDragGridSize = enable;
8212}
8213
6e8524b1 8214
f85afd4e
MB
8215void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
8216{
8217 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
8218
8219 if ( resizeExistingRows )
8220 {
7c1cb261 8221 InitRowHeights();
faec5a43
SN
8222 if ( !GetBatchCount() )
8223 CalcDimensions();
f85afd4e
MB
8224 }
8225}
8226
8227void wxGrid::SetRowSize( int row, int height )
8228{
b99be8fb 8229 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 8230
7c1cb261
VZ
8231 if ( m_rowHeights.IsEmpty() )
8232 {
8233 // need to really create the array
8234 InitRowHeights();
8235 }
60ff3b99 8236
b99be8fb
VZ
8237 int h = wxMax( 0, height );
8238 int diff = h - m_rowHeights[row];
60ff3b99 8239
b99be8fb 8240 m_rowHeights[row] = h;
7c1cb261 8241 int i;
b99be8fb 8242 for ( i = row; i < m_numRows; i++ )
f85afd4e 8243 {
b99be8fb 8244 m_rowBottoms[i] += diff;
f85afd4e 8245 }
faec5a43
SN
8246 if ( !GetBatchCount() )
8247 CalcDimensions();
f85afd4e
MB
8248}
8249
8250void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
8251{
8252 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
8253
8254 if ( resizeExistingCols )
8255 {
7c1cb261 8256 InitColWidths();
faec5a43
SN
8257 if ( !GetBatchCount() )
8258 CalcDimensions();
f85afd4e
MB
8259 }
8260}
8261
8262void wxGrid::SetColSize( int col, int width )
8263{
b99be8fb 8264 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 8265
43947979
VZ
8266 // should we check that it's bigger than GetColMinimalWidth(col) here?
8267
7c1cb261
VZ
8268 if ( m_colWidths.IsEmpty() )
8269 {
8270 // need to really create the array
8271 InitColWidths();
8272 }
f85afd4e 8273
b99be8fb
VZ
8274 int w = wxMax( 0, width );
8275 int diff = w - m_colWidths[col];
8276 m_colWidths[col] = w;
60ff3b99 8277
7c1cb261 8278 int i;
b99be8fb 8279 for ( i = col; i < m_numCols; i++ )
f85afd4e 8280 {
b99be8fb 8281 m_colRights[i] += diff;
f85afd4e 8282 }
faec5a43
SN
8283 if ( !GetBatchCount() )
8284 CalcDimensions();
f85afd4e
MB
8285}
8286
2d66e025 8287
43947979
VZ
8288void wxGrid::SetColMinimalWidth( int col, int width )
8289{
af547d51
VZ
8290 m_colMinWidths.Put(col, width);
8291}
8292
8293void wxGrid::SetRowMinimalHeight( int row, int width )
8294{
8295 m_rowMinHeights.Put(row, width);
43947979
VZ
8296}
8297
8298int wxGrid::GetColMinimalWidth(int col) const
8299{
af547d51
VZ
8300 long value = m_colMinWidths.Get(col);
8301 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
8302}
8303
8304int wxGrid::GetRowMinimalHeight(int row) const
8305{
8306 long value = m_rowMinHeights.Get(row);
8307 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
43947979
VZ
8308}
8309
57c086ef
VZ
8310// ----------------------------------------------------------------------------
8311// auto sizing
8312// ----------------------------------------------------------------------------
8313
af547d51 8314void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
8315{
8316 wxClientDC dc(m_gridWin);
8317
a95e38c0
VZ
8318 // init both of them to avoid compiler warnings, even if weo nly need one
8319 int row = -1,
8320 col = -1;
af547d51
VZ
8321 if ( column )
8322 col = colOrRow;
8323 else
8324 row = colOrRow;
8325
8326 wxCoord extent, extentMax = 0;
8327 int max = column ? m_numRows : m_numCols;
39bcce60 8328 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 8329 {
af547d51
VZ
8330 if ( column )
8331 row = rowOrCol;
8332 else
8333 col = rowOrCol;
8334
65e4e78e 8335 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8336 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
8337 if ( renderer )
8338 {
af547d51
VZ
8339 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
8340 extent = column ? size.x : size.y;
8341 if ( extent > extentMax )
65e4e78e 8342 {
af547d51 8343 extentMax = extent;
65e4e78e 8344 }
0b190b0f
VZ
8345
8346 renderer->DecRef();
65e4e78e
VZ
8347 }
8348
8349 attr->DecRef();
8350 }
8351
af547d51
VZ
8352 // now also compare with the column label extent
8353 wxCoord w, h;
65e4e78e 8354 dc.SetFont( GetLabelFont() );
294d195c
MB
8355
8356 if ( column )
8357 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
8358 else
ee495e4c 8359 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
294d195c 8360
af547d51
VZ
8361 extent = column ? w : h;
8362 if ( extent > extentMax )
65e4e78e 8363 {
af547d51 8364 extentMax = extent;
65e4e78e
VZ
8365 }
8366
af547d51 8367 if ( !extentMax )
65e4e78e 8368 {
af547d51
VZ
8369 // empty column - give default extent (notice that if extentMax is less
8370 // than default extent but != 0, it's ok)
8371 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
8372 }
8373 else
8374 {
a95e38c0
VZ
8375 if ( column )
8376 {
8377 // leave some space around text
8378 extentMax += 10;
8379 }
f6bcfd97
BP
8380 else
8381 {
8382 extentMax += 6;
8383 }
65e4e78e
VZ
8384 }
8385
faec5a43 8386 if ( column ){
af547d51 8387 SetColSize(col, extentMax);
faec5a43
SN
8388 if ( !GetBatchCount() )
8389 {
8390 int cw, ch, dummy;
8391 m_gridWin->GetClientSize( &cw, &ch );
8392 wxRect rect ( CellToRect( 0, col ) );
8393 rect.y = 0;
8394 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
8395 rect.width = cw - rect.x;
8396 rect.height = m_colLabelHeight;
8397 m_colLabelWin->Refresh( TRUE, &rect );
8398 }
8399 }
8400 else{
39bcce60 8401 SetRowSize(row, extentMax);
faec5a43
SN
8402 if ( !GetBatchCount() )
8403 {
8404 int cw, ch, dummy;
8405 m_gridWin->GetClientSize( &cw, &ch );
8406 wxRect rect ( CellToRect( row, 0 ) );
8407 rect.x = 0;
8408 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
8409 rect.width = m_rowLabelWidth;
8410 rect.height = ch - rect.y;
8411 m_rowLabelWin->Refresh( TRUE, &rect );
8412 }
8413 }
65e4e78e
VZ
8414 if ( setAsMin )
8415 {
af547d51
VZ
8416 if ( column )
8417 SetColMinimalWidth(col, extentMax);
8418 else
39bcce60 8419 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
8420 }
8421}
8422
266e8367 8423int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 8424{
57c086ef
VZ
8425 int width = m_rowLabelWidth;
8426
faec5a43
SN
8427 if ( !calcOnly )
8428 BeginBatch();
65e4e78e
VZ
8429 for ( int col = 0; col < m_numCols; col++ )
8430 {
266e8367
VZ
8431 if ( !calcOnly )
8432 {
8433 AutoSizeColumn(col, setAsMin);
8434 }
57c086ef
VZ
8435
8436 width += GetColWidth(col);
65e4e78e 8437 }
faec5a43
SN
8438 if ( !calcOnly )
8439 EndBatch();
266e8367 8440 return width;
65e4e78e
VZ
8441}
8442
266e8367 8443int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
8444{
8445 int height = m_colLabelHeight;
8446
faec5a43
SN
8447 if ( !calcOnly )
8448 BeginBatch();
57c086ef
VZ
8449 for ( int row = 0; row < m_numRows; row++ )
8450 {
af547d51
VZ
8451 if ( !calcOnly )
8452 {
8453 AutoSizeRow(row, setAsMin);
8454 }
57c086ef
VZ
8455
8456 height += GetRowHeight(row);
8457 }
faec5a43
SN
8458 if ( !calcOnly )
8459 EndBatch();
266e8367 8460 return height;
57c086ef
VZ
8461}
8462
8463void wxGrid::AutoSize()
8464{
266e8367 8465 // set the size too
faec5a43 8466 SetClientSize(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
266e8367
VZ
8467}
8468
8469wxSize wxGrid::DoGetBestSize() const
8470{
8471 // don't set sizes, only calculate them
8472 wxGrid *self = (wxGrid *)this; // const_cast
8473
8474 return wxSize(self->SetOrCalcColumnSizes(TRUE),
8475 self->SetOrCalcRowSizes(TRUE));
8476}
8477
8478void wxGrid::Fit()
8479{
8480 AutoSize();
57c086ef
VZ
8481}
8482
6fc0f38f
SN
8483
8484wxPen& wxGrid::GetDividerPen() const
8485{
8486 return wxNullPen;
8487}
8488
57c086ef
VZ
8489// ----------------------------------------------------------------------------
8490// cell value accessor functions
8491// ----------------------------------------------------------------------------
f85afd4e
MB
8492
8493void wxGrid::SetCellValue( int row, int col, const wxString& s )
8494{
8495 if ( m_table )
8496 {
f6bcfd97 8497 m_table->SetValue( row, col, s );
2d66e025
MB
8498 if ( !GetBatchCount() )
8499 {
8500 wxClientDC dc( m_gridWin );
8501 PrepareDC( dc );
8502 DrawCell( dc, wxGridCellCoords(row, col) );
8503 }
60ff3b99 8504
f85afd4e 8505 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 8506 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
8507 IsCellEditControlShown())
8508 // Note: If we are using IsCellEditControlEnabled,
8509 // this interacts badly with calling SetCellValue from
8510 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 8511 {
4cfa5de6
RD
8512 HideCellEditControl();
8513 ShowCellEditControl(); // will reread data from table
f85afd4e 8514 }
f85afd4e
MB
8515 }
8516}
8517
8518
8519//
2d66e025 8520// ------ Block, row and col selection
f85afd4e
MB
8521//
8522
8523void wxGrid::SelectRow( int row, bool addToSelected )
8524{
b5808881 8525 if ( IsSelection() && !addToSelected )
e32352cf 8526 ClearSelection();
70c7a608 8527
f6bcfd97 8528 m_selection->SelectRow( row, FALSE, addToSelected );
f85afd4e
MB
8529}
8530
8531
8532void wxGrid::SelectCol( int col, bool addToSelected )
8533{
b5808881 8534 if ( IsSelection() && !addToSelected )
e32352cf 8535 ClearSelection();
f85afd4e 8536
f6bcfd97 8537 m_selection->SelectCol( col, FALSE, addToSelected );
f85afd4e
MB
8538}
8539
8540
84912ef8 8541void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
c9097836 8542 bool addToSelected )
f85afd4e 8543{
c9097836
MB
8544 if ( IsSelection() && !addToSelected )
8545 ClearSelection();
f85afd4e 8546
84912ef8
RD
8547 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
8548 FALSE, addToSelected );
f85afd4e
MB
8549}
8550
c9097836 8551
f85afd4e
MB
8552void wxGrid::SelectAll()
8553{
f74d0b57
SN
8554 if ( m_numRows > 0 && m_numCols > 0 )
8555 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
b5808881 8556}
f85afd4e 8557
f7b4b343
VZ
8558//
8559// ------ Cell, row and col deselection
8560//
8561
8562void wxGrid::DeselectRow( int row )
8563{
8564 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
8565 {
8566 if ( m_selection->IsInSelection(row, 0 ) )
8567 m_selection->ToggleCellSelection( row, 0);
ffdd3c98 8568 }
f7b4b343
VZ
8569 else
8570 {
8571 int nCols = GetNumberCols();
8572 for ( int i = 0; i < nCols ; i++ )
8573 {
8574 if ( m_selection->IsInSelection(row, i ) )
8575 m_selection->ToggleCellSelection( row, i);
8576 }
8577 }
8578}
8579
8580void wxGrid::DeselectCol( int col )
8581{
8582 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
8583 {
8584 if ( m_selection->IsInSelection(0, col ) )
8585 m_selection->ToggleCellSelection( 0, col);
8586 }
8587 else
8588 {
8589 int nRows = GetNumberRows();
8590 for ( int i = 0; i < nRows ; i++ )
8591 {
8592 if ( m_selection->IsInSelection(i, col ) )
8593 m_selection->ToggleCellSelection(i, col);
8594 }
8595 }
8596}
8597
8598void wxGrid::DeselectCell( int row, int col )
8599{
8600 if ( m_selection->IsInSelection(row, col) )
8601 m_selection->ToggleCellSelection(row, col);
8602}
8603
b5808881
SN
8604bool wxGrid::IsSelection()
8605{
8606 return ( m_selection->IsSelection() ||
8607 ( m_selectingTopLeft != wxGridNoCellCoords &&
8608 m_selectingBottomRight != wxGridNoCellCoords ) );
f85afd4e
MB
8609}
8610
b5808881
SN
8611bool wxGrid::IsInSelection( int row, int col )
8612{
8613 return ( m_selection->IsInSelection( row, col ) ||
8614 ( row >= m_selectingTopLeft.GetRow() &&
8615 col >= m_selectingTopLeft.GetCol() &&
8616 row <= m_selectingBottomRight.GetRow() &&
8617 col <= m_selectingBottomRight.GetCol() ) );
8618}
f85afd4e
MB
8619
8620void wxGrid::ClearSelection()
8621{
b5808881
SN
8622 m_selectingTopLeft = wxGridNoCellCoords;
8623 m_selectingBottomRight = wxGridNoCellCoords;
8624 m_selection->ClearSelection();
8f177c8e 8625}
f85afd4e
MB
8626
8627
da6af900 8628// This function returns the rectangle that encloses the given block
2d66e025
MB
8629// in device coords clipped to the client size of the grid window.
8630//
58dd5b3b
MB
8631wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
8632 const wxGridCellCoords &bottomRight )
f85afd4e 8633{
58dd5b3b 8634 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
8635 wxRect cellRect;
8636
58dd5b3b
MB
8637 cellRect = CellToRect( topLeft );
8638 if ( cellRect != wxGridNoCellRect )
f85afd4e 8639 {
58dd5b3b
MB
8640 rect = cellRect;
8641 }
8642 else
8643 {
8644 rect = wxRect( 0, 0, 0, 0 );
8645 }
60ff3b99 8646
58dd5b3b
MB
8647 cellRect = CellToRect( bottomRight );
8648 if ( cellRect != wxGridNoCellRect )
8649 {
8650 rect += cellRect;
2d66e025
MB
8651 }
8652 else
8653 {
8654 return wxGridNoCellRect;
f85afd4e
MB
8655 }
8656
58dd5b3b
MB
8657 // convert to scrolled coords
8658 //
8659 int left, top, right, bottom;
8660 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
8661 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
8662
8663 int cw, ch;
8664 m_gridWin->GetClientSize( &cw, &ch );
8665
f6bcfd97
BP
8666 if (right < 0 || bottom < 0 || left > cw || top > ch)
8667 return wxRect( 0, 0, 0, 0);
8668
58dd5b3b
MB
8669 rect.SetLeft( wxMax(0, left) );
8670 rect.SetTop( wxMax(0, top) );
8671 rect.SetRight( wxMin(cw, right) );
8672 rect.SetBottom( wxMin(ch, bottom) );
8673
f85afd4e
MB
8674 return rect;
8675}
8676
8677
58dd5b3b 8678
f85afd4e
MB
8679//
8680// ------ Grid event classes
8681//
8682
bf7945ce 8683IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
8684
8685wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 8686 int row, int col, int x, int y, bool sel,
f85afd4e
MB
8687 bool control, bool shift, bool alt, bool meta )
8688 : wxNotifyEvent( type, id )
8689{
8690 m_row = row;
8691 m_col = col;
8692 m_x = x;
8693 m_y = y;
5c8fc7c1 8694 m_selecting = sel;
f85afd4e
MB
8695 m_control = control;
8696 m_shift = shift;
8697 m_alt = alt;
8698 m_meta = meta;
8f177c8e 8699
f85afd4e
MB
8700 SetEventObject(obj);
8701}
8702
8703
bf7945ce 8704IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
8705
8706wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
8707 int rowOrCol, int x, int y,
8708 bool control, bool shift, bool alt, bool meta )
8709 : wxNotifyEvent( type, id )
8710{
8711 m_rowOrCol = rowOrCol;
8712 m_x = x;
8713 m_y = y;
8714 m_control = control;
8715 m_shift = shift;
8716 m_alt = alt;
8717 m_meta = meta;
8f177c8e 8718
f85afd4e
MB
8719 SetEventObject(obj);
8720}
8721
8722
bf7945ce 8723IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
8724
8725wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
8726 const wxGridCellCoords& topLeft,
8727 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
8728 bool sel, bool control,
8729 bool shift, bool alt, bool meta )
8f177c8e 8730 : wxNotifyEvent( type, id )
f85afd4e
MB
8731{
8732 m_topLeft = topLeft;
8733 m_bottomRight = bottomRight;
5c8fc7c1 8734 m_selecting = sel;
f85afd4e
MB
8735 m_control = control;
8736 m_shift = shift;
8737 m_alt = alt;
8738 m_meta = meta;
8739
8740 SetEventObject(obj);
8741}
8742
8743
bf7945ce
RD
8744IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
8745
8746wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
8747 wxObject* obj, int row,
8748 int col, wxControl* ctrl)
8749 : wxCommandEvent(type, id)
8750{
8751 SetEventObject(obj);
8752 m_row = row;
8753 m_col = col;
8754 m_ctrl = ctrl;
8755}
8756
8757
f85afd4e 8758#endif // ifndef wxUSE_NEW_GRID