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