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