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