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