]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
smoother time estimation updates in wxProgressDialog (patch 992813)
[wxWidgets.git] / src / generic / grid.cpp
Content-type: text/html ]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp


500 - Internal Server Error

Malformed UTF-8 character (fatal) at /usr/lib/x86_64-linux-gnu/perl5/5.40/HTML/Entities.pm line 485, <$fd> line 9263.
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)
65571936 9// Licence: wxWindows licence
f85afd4e
MB
10/////////////////////////////////////////////////////////////////////////////
11
758cbedf
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f85afd4e
MB
21 #pragma implementation "grid.h"
22#endif
23
f6bcfd97 24// For compilers that support precompilatixon, includes "wx/wx.h".
4d85bcd1
JS
25#include "wx/wxprec.h"
26
27#include "wx/defs.h"
f85afd4e
MB
28
29#ifdef __BORLANDC__
30 #pragma hdrstop
31#endif
32
27b92ca4
VZ
33#if wxUSE_GRID
34
f85afd4e
MB
35#ifndef WX_PRECOMP
36 #include "wx/utils.h"
37 #include "wx/dcclient.h"
38 #include "wx/settings.h"
39 #include "wx/log.h"
508011ce
VZ
40 #include "wx/textctrl.h"
41 #include "wx/checkbox.h"
4ee5fc9c 42 #include "wx/combobox.h"
816be743 43 #include "wx/valtext.h"
60d876f3 44 #include "wx/intl.h"
f85afd4e
MB
45#endif
46
cb5df486 47#include "wx/textfile.h"
816be743 48#include "wx/spinctrl.h"
c4608a8a 49#include "wx/tokenzr.h"
6d004f67 50
07296f0b 51#include "wx/grid.h"
b5808881 52#include "wx/generic/gridsel.h"
07296f0b 53
0b7e6e7d
SN
54#if defined(__WXMOTIF__)
55 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
c78b3acd 56#else
0b7e6e7d 57 #define WXUNUSED_MOTIF(identifier) identifier
c78b3acd
SN
58#endif
59
60#if defined(__WXGTK__)
61 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
62#else
63 #define WXUNUSED_GTK(identifier) identifier
64#endif
65
3f8e5072
JS
66// Required for wxIs... functions
67#include <ctype.h>
68
b99be8fb 69// ----------------------------------------------------------------------------
758cbedf 70// array classes
b99be8fb
VZ
71// ----------------------------------------------------------------------------
72
d5d29b8a 73WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs,
160ba750 74 class WXDLLIMPEXP_ADV);
758cbedf 75
b99be8fb
VZ
76struct wxGridCellWithAttr
77{
2e9a6788
VZ
78 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
79 : coords(row, col), attr(attr_)
b99be8fb
VZ
80 {
81 }
82
2e9a6788
VZ
83 ~wxGridCellWithAttr()
84 {
85 attr->DecRef();
86 }
87
b99be8fb 88 wxGridCellCoords coords;
2e9a6788 89 wxGridCellAttr *attr;
22f3361e
VZ
90
91// Cannot do this:
92// DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
93// without rewriting the macros, which require a public copy constructor.
b99be8fb
VZ
94};
95
160ba750
VS
96WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray,
97 class WXDLLIMPEXP_ADV);
b99be8fb
VZ
98
99#include "wx/arrimpl.cpp"
100
101WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
102WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
103
0f442030
RR
104// ----------------------------------------------------------------------------
105// events
106// ----------------------------------------------------------------------------
107
2e4df4bf
VZ
108DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK)
109DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK)
110DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK)
111DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK)
79dbea21 112DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG)
2e4df4bf
VZ
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
12f190b0 130class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow
b99be8fb
VZ
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
12f190b0 152class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow
b99be8fb
VZ
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
12f190b0 174class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow
b99be8fb
VZ
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
12f190b0 195class WXDLLIMPEXP_ADV 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 225 void OnEraseBackground( wxEraseEvent& );
80acaf25 226 void OnFocus( wxFocusEvent& );
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
12f190b0 270class WXDLLIMPEXP_ADV 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 285// this class stores attributes set for rows or columns
12f190b0 286class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
758cbedf
VZ
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
12f190b0 304class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
758cbedf
VZ
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
d5d29b8a 339WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray,
160ba750 340 class WXDLLIMPEXP_ADV);
f2d76237
RD
341
342
12f190b0 343class WXDLLIMPEXP_ADV wxGridTypeRegistry
b94ae1ea 344{
f2d76237 345public:
c78b3acd 346 wxGridTypeRegistry() {}
f2d76237 347 ~wxGridTypeRegistry();
b94ae1ea 348
f2d76237
RD
349 void RegisterDataType(const wxString& typeName,
350 wxGridCellRenderer* renderer,
351 wxGridCellEditor* editor);
c4608a8a
VZ
352
353 // find one of already registered data types
354 int FindRegisteredDataType(const wxString& typeName);
355
356 // try to FindRegisteredDataType(), if this fails and typeName is one of
357 // standard typenames, register it and return its index
f2d76237 358 int FindDataType(const wxString& typeName);
c4608a8a
VZ
359
360 // try to FindDataType(), if it fails see if it is not one of already
361 // registered data types with some params in which case clone the
362 // registered data type and set params for it
363 int FindOrCloneDataType(const wxString& typeName);
364
f2d76237
RD
365 wxGridCellRenderer* GetRenderer(int index);
366 wxGridCellEditor* GetEditor(int index);
367
368private:
369 wxGridDataTypeInfoArray m_typeinfo;
370};
371
b99be8fb
VZ
372// ----------------------------------------------------------------------------
373// conditional compilation
374// ----------------------------------------------------------------------------
375
9496deb5
MB
376#ifndef WXGRID_DRAW_LINES
377#define WXGRID_DRAW_LINES 1
796df70a
SN
378#endif
379
0a976765
VZ
380// ----------------------------------------------------------------------------
381// globals
382// ----------------------------------------------------------------------------
383
384//#define DEBUG_ATTR_CACHE
385#ifdef DEBUG_ATTR_CACHE
386 static size_t gs_nAttrCacheHits = 0;
387 static size_t gs_nAttrCacheMisses = 0;
388#endif // DEBUG_ATTR_CACHE
f85afd4e 389
43947979
VZ
390// ----------------------------------------------------------------------------
391// constants
392// ----------------------------------------------------------------------------
393
f85afd4e
MB
394wxGridCellCoords wxGridNoCellCoords( -1, -1 );
395wxRect wxGridNoCellRect( -1, -1, -1, -1 );
396
f0102d2a 397// scroll line size
faec5a43
SN
398// TODO: this doesn't work at all, grid cells have different sizes and approx
399// calculations don't work as because of the size mismatch scrollbars
400// sometimes fail to be shown when they should be or vice versa
b51c3f27
RD
401//
402// The scroll bars may be a little flakey once in a while, but that is
403// surely much less horrible than having scroll lines of only 1!!!
404// -- Robin
97a9929e
VZ
405//
406// Well, it's still seriously broken so it might be better but needs
407// fixing anyhow
408// -- Vadim
409static const size_t GRID_SCROLL_LINE_X = 15; // 1;
410static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
f85afd4e 411
43947979
VZ
412// the size of hash tables used a bit everywhere (the max number of elements
413// in these hash tables is the number of rows/columns)
414static const int GRID_HASH_SIZE = 100;
415
97a9929e
VZ
416// ----------------------------------------------------------------------------
417// private functions
418// ----------------------------------------------------------------------------
419
d0eb7e56 420static inline int GetScrollX(int x)
97a9929e
VZ
421{
422 return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X;
423}
424
d0eb7e56 425static inline int GetScrollY(int y)
97a9929e
VZ
426{
427 return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y;
428}
429
ab79958a
VZ
430// ============================================================================
431// implementation
432// ============================================================================
433
2796cce3
RD
434// ----------------------------------------------------------------------------
435// wxGridCellEditor
436// ----------------------------------------------------------------------------
437
438wxGridCellEditor::wxGridCellEditor()
439{
440 m_control = NULL;
1bd71df9 441 m_attr = NULL;
2796cce3
RD
442}
443
444
445wxGridCellEditor::~wxGridCellEditor()
446{
447 Destroy();
448}
449
508011ce
VZ
450void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
451 wxWindowID WXUNUSED(id),
452 wxEvtHandler* evtHandler)
453{
189d0213 454 if ( evtHandler )
508011ce
VZ
455 m_control->PushEventHandler(evtHandler);
456}
2796cce3 457
189d0213
VZ
458void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
459 wxGridCellAttr *attr)
460{
461 // erase the background because we might not fill the cell
462 wxClientDC dc(m_control->GetParent());
b819b854
JS
463 wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow);
464 if (gridWindow)
465 gridWindow->GetOwner()->PrepareDC(dc);
ef5df12b 466
189d0213
VZ
467 dc.SetPen(*wxTRANSPARENT_PEN);
468 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
469 dc.DrawRectangle(rectCell);
470
471 // redraw the control we just painted over
472 m_control->Refresh();
473}
474
2796cce3
RD
475void wxGridCellEditor::Destroy()
476{
508011ce
VZ
477 if (m_control)
478 {
ca65c044 479 m_control->PopEventHandler(true /* delete it*/);
b94ae1ea 480
2796cce3
RD
481 m_control->Destroy();
482 m_control = NULL;
483 }
484}
485
3da93aae 486void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
2796cce3
RD
487{
488 wxASSERT_MSG(m_control,
489 wxT("The wxGridCellEditor must be Created first!"));
490 m_control->Show(show);
3da93aae
VZ
491
492 if ( show )
493 {
494 // set the colours/fonts if we have any
495 if ( attr )
496 {
f2d76237
RD
497 m_colFgOld = m_control->GetForegroundColour();
498 m_control->SetForegroundColour(attr->GetTextColour());
3da93aae 499
f2d76237
RD
500 m_colBgOld = m_control->GetBackgroundColour();
501 m_control->SetBackgroundColour(attr->GetBackgroundColour());
3da93aae 502
f2d76237
RD
503 m_fontOld = m_control->GetFont();
504 m_control->SetFont(attr->GetFont());
3da93aae
VZ
505
506 // can't do anything more in the base class version, the other
507 // attributes may only be used by the derived classes
508 }
509 }
510 else
511 {
512 // restore the standard colours fonts
513 if ( m_colFgOld.Ok() )
514 {
515 m_control->SetForegroundColour(m_colFgOld);
516 m_colFgOld = wxNullColour;
517 }
518
519 if ( m_colBgOld.Ok() )
520 {
521 m_control->SetBackgroundColour(m_colBgOld);
522 m_colBgOld = wxNullColour;
523 }
524
525 if ( m_fontOld.Ok() )
526 {
527 m_control->SetFont(m_fontOld);
528 m_fontOld = wxNullFont;
529 }
530 }
2796cce3
RD
531}
532
533void wxGridCellEditor::SetSize(const wxRect& rect)
534{
535 wxASSERT_MSG(m_control,
536 wxT("The wxGridCellEditor must be Created first!"));
28a77bc4 537 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
2796cce3
RD
538}
539
540void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
541{
542 event.Skip();
543}
544
f6bcfd97
BP
545bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
546{
547 // accept the simple key presses, not anything with Ctrl/Alt/Meta
a4f7bf58 548 return !(event.ControlDown() || event.AltDown());
f6bcfd97 549}
2796cce3 550
2c9a89e0
RD
551void wxGridCellEditor::StartingKey(wxKeyEvent& event)
552{
e195a54c
VZ
553 event.Skip();
554}
2c9a89e0 555
e195a54c
VZ
556void wxGridCellEditor::StartingClick()
557{
b54ba671 558}
2c9a89e0 559
3a8c693a
VZ
560#if wxUSE_TEXTCTRL
561
b54ba671
VZ
562// ----------------------------------------------------------------------------
563// wxGridCellTextEditor
564// ----------------------------------------------------------------------------
2c9a89e0 565
2796cce3
RD
566wxGridCellTextEditor::wxGridCellTextEditor()
567{
c4608a8a 568 m_maxChars = 0;
2796cce3
RD
569}
570
571void wxGridCellTextEditor::Create(wxWindow* parent,
572 wxWindowID id,
2796cce3
RD
573 wxEvtHandler* evtHandler)
574{
508011ce 575 m_control = new wxTextCtrl(parent, id, wxEmptyString,
2c9a89e0 576 wxDefaultPosition, wxDefaultSize
2796cce3 577#if defined(__WXMSW__)
aaae069f 578 , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL
2796cce3 579#endif
508011ce 580 );
2796cce3 581
46a5010a
RD
582 // set max length allowed in the textctrl, if the parameter was set
583 if (m_maxChars != 0)
584 {
585 ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars);
586 }
c4608a8a 587
508011ce 588 wxGridCellEditor::Create(parent, id, evtHandler);
2796cce3
RD
589}
590
189d0213
VZ
591void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
592 wxGridCellAttr * WXUNUSED(attr))
593{
594 // as we fill the entire client area, don't do anything here to minimize
595 // flicker
596}
2796cce3 597
99306db2
VZ
598void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
599{
600 wxRect rect(rectOrig);
601
602 // Make the edit control large enough to allow for internal
603 // margins
604 //
605 // TODO: remove this if the text ctrl sizing is improved esp. for
606 // unix
607 //
608#if defined(__WXGTK__)
b0e282b3
RR
609 if (rect.x != 0)
610 {
611 rect.x += 1;
612 rect.y += 1;
613 rect.width -= 1;
614 rect.height -= 1;
615 }
99306db2 616#else // !GTK
cb105ad4 617 int extra_x = ( rect.x > 2 )? 2 : 1;
84912ef8
RD
618
619// MB: treat MSW separately here otherwise the caret doesn't show
620// when the editor is in the first row.
a0948e27
MB
621#if defined(__WXMSW__)
622 int extra_y = 2;
623#else
cb105ad4 624 int extra_y = ( rect.y > 2 )? 2 : 1;
a0948e27
MB
625#endif // MSW
626
99306db2 627#if defined(__WXMOTIF__)
cb105ad4
SN
628 extra_x *= 2;
629 extra_y *= 2;
99306db2 630#endif
cb105ad4
SN
631 rect.SetLeft( wxMax(0, rect.x - extra_x) );
632 rect.SetTop( wxMax(0, rect.y - extra_y) );
633 rect.SetRight( rect.GetRight() + 2*extra_x );
634 rect.SetBottom( rect.GetBottom() + 2*extra_y );
99306db2
VZ
635#endif // GTK/!GTK
636
637 wxGridCellEditor::SetSize(rect);
638}
639
3da93aae 640void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
2796cce3
RD
641{
642 wxASSERT_MSG(m_control,
643 wxT("The wxGridCellEditor must be Created first!"));
644
645 m_startValue = grid->GetTable()->GetValue(row, col);
816be743
VZ
646
647 DoBeginEdit(m_startValue);
648}
649
650void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
651{
652 Text()->SetValue(startValue);
b54ba671 653 Text()->SetInsertionPointEnd();
505f70de 654 Text()->SetSelection(-1,-1);
b54ba671 655 Text()->SetFocus();
2796cce3
RD
656}
657
3324d5f5 658bool wxGridCellTextEditor::EndEdit(int row, int col,
3da93aae 659 wxGrid* grid)
2796cce3
RD
660{
661 wxASSERT_MSG(m_control,
662 wxT("The wxGridCellEditor must be Created first!"));
663
ca65c044 664 bool changed = false;
b54ba671 665 wxString value = Text()->GetValue();
2796cce3 666 if (value != m_startValue)
ca65c044 667 changed = true;
2796cce3
RD
668
669 if (changed)
670 grid->GetTable()->SetValue(row, col, value);
2c9a89e0 671
3da93aae 672 m_startValue = wxEmptyString;
7b519e5e
JS
673 // No point in setting the text of the hidden control
674 //Text()->SetValue(m_startValue);
2796cce3
RD
675
676 return changed;
677}
678
679
680void wxGridCellTextEditor::Reset()
681{
682 wxASSERT_MSG(m_control,
683 wxT("The wxGridCellEditor must be Created first!"));
684
816be743
VZ
685 DoReset(m_startValue);
686}
687
688void wxGridCellTextEditor::DoReset(const wxString& startValue)
689{
690 Text()->SetValue(startValue);
b54ba671 691 Text()->SetInsertionPointEnd();
2796cce3
RD
692}
693
f6bcfd97
BP
694bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
695{
696 if ( wxGridCellEditor::IsAcceptedKey(event) )
697 {
698 int keycode = event.GetKeyCode();
699 switch ( keycode )
700 {
701 case WXK_NUMPAD0:
702 case WXK_NUMPAD1:
703 case WXK_NUMPAD2:
704 case WXK_NUMPAD3:
705 case WXK_NUMPAD4:
706 case WXK_NUMPAD5:
707 case WXK_NUMPAD6:
708 case WXK_NUMPAD7:
709 case WXK_NUMPAD8:
710 case WXK_NUMPAD9:
711 case WXK_MULTIPLY:
712 case WXK_NUMPAD_MULTIPLY:
713 case WXK_ADD:
714 case WXK_NUMPAD_ADD:
715 case WXK_SUBTRACT:
716 case WXK_NUMPAD_SUBTRACT:
717 case WXK_DECIMAL:
718 case WXK_NUMPAD_DECIMAL:
719 case WXK_DIVIDE:
720 case WXK_NUMPAD_DIVIDE:
ca65c044 721 return true;
f6bcfd97
BP
722
723 default:
724 // accept 8 bit chars too if isprint() agrees
1c193821 725 if ( (keycode < 255) && (wxIsprint(keycode)) )
ca65c044 726 return true;
f6bcfd97
BP
727 }
728 }
729
ca65c044 730 return false;
f6bcfd97
BP
731}
732
2c9a89e0
RD
733void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
734{
94af7d45 735 if ( !Text()->EmulateKeyPress(event) )
f6bcfd97
BP
736 {
737 event.Skip();
738 }
b54ba671 739}
2c9a89e0 740
c78b3acd 741void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
0b7e6e7d 742 WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
2796cce3
RD
743{
744#if defined(__WXMOTIF__) || defined(__WXGTK__)
745 // wxMotif needs a little extra help...
6fc0f38f 746 size_t pos = (size_t)( Text()->GetInsertionPoint() );
b54ba671 747 wxString s( Text()->GetValue() );
8dd8f875 748 s = s.Left(pos) + wxT("\n") + s.Mid(pos);
b54ba671
VZ
749 Text()->SetValue(s);
750 Text()->SetInsertionPoint( pos );
2796cce3
RD
751#else
752 // the other ports can handle a Return key press
753 //
754 event.Skip();
755#endif
756}
757
c4608a8a
VZ
758void wxGridCellTextEditor::SetParameters(const wxString& params)
759{
760 if ( !params )
761 {
762 // reset to default
763 m_maxChars = 0;
764 }
765 else
766 {
767 long tmp;
768 if ( !params.ToLong(&tmp) )
769 {
f6bcfd97 770 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str());
c4608a8a
VZ
771 }
772 else
773 {
774 m_maxChars = (size_t)tmp;
775 }
776 }
777}
778
73145b0e
JS
779// return the value in the text control
780wxString wxGridCellTextEditor::GetValue() const
781{
782 return Text()->GetValue();
783}
784
816be743
VZ
785// ----------------------------------------------------------------------------
786// wxGridCellNumberEditor
787// ----------------------------------------------------------------------------
788
789wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
790{
791 m_min = min;
792 m_max = max;
793}
794
795void wxGridCellNumberEditor::Create(wxWindow* parent,
796 wxWindowID id,
797 wxEvtHandler* evtHandler)
798{
799 if ( HasRange() )
800 {
801 // create a spin ctrl
ca65c044 802 m_control = new wxSpinCtrl(parent, wxID_ANY, wxEmptyString,
816be743
VZ
803 wxDefaultPosition, wxDefaultSize,
804 wxSP_ARROW_KEYS,
805 m_min, m_max);
806
807 wxGridCellEditor::Create(parent, id, evtHandler);
808 }
809 else
810 {
811 // just a text control
812 wxGridCellTextEditor::Create(parent, id, evtHandler);
813
814#if wxUSE_VALIDATORS
85bc0351 815 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
816#endif // wxUSE_VALIDATORS
817 }
818}
819
820void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
821{
822 // first get the value
823 wxGridTableBase *table = grid->GetTable();
824 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
825 {
826 m_valueOld = table->GetValueAsLong(row, col);
827 }
828 else
829 {
8a60ff0a 830 m_valueOld = 0;
a5777624 831 wxString sValue = table->GetValue(row, col);
8a60ff0a 832 if (! sValue.ToLong(&m_valueOld) && ! sValue.IsEmpty())
a5777624
RD
833 {
834 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
835 return;
836 }
816be743
VZ
837 }
838
839 if ( HasRange() )
840 {
4a64bee4 841 Spin()->SetValue((int)m_valueOld);
f6bcfd97 842 Spin()->SetFocus();
816be743
VZ
843 }
844 else
845 {
846 DoBeginEdit(GetString());
847 }
848}
849
3324d5f5 850bool wxGridCellNumberEditor::EndEdit(int row, int col,
816be743
VZ
851 wxGrid* grid)
852{
853 bool changed;
8a60ff0a
RD
854 long value = 0;
855 wxString text;
816be743
VZ
856
857 if ( HasRange() )
858 {
859 value = Spin()->GetValue();
860 changed = value != m_valueOld;
8a60ff0a
RD
861 if (changed)
862 text = wxString::Format(wxT("%ld"), value);
816be743
VZ
863 }
864 else
865 {
8a60ff0a
RD
866 text = Text()->GetValue();
867 changed = (text.IsEmpty() || text.ToLong(&value)) && (value != m_valueOld);
816be743
VZ
868 }
869
870 if ( changed )
871 {
a5777624
RD
872 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
873 grid->GetTable()->SetValueAsLong(row, col, value);
874 else
8a60ff0a 875 grid->GetTable()->SetValue(row, col, text);
816be743
VZ
876 }
877
878 return changed;
879}
880
881void wxGridCellNumberEditor::Reset()
882{
883 if ( HasRange() )
884 {
4a64bee4 885 Spin()->SetValue((int)m_valueOld);
816be743
VZ
886 }
887 else
888 {
889 DoReset(GetString());
890 }
891}
892
f6bcfd97
BP
893bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event)
894{
895 if ( wxGridCellEditor::IsAcceptedKey(event) )
896 {
897 int keycode = event.GetKeyCode();
898 switch ( keycode )
899 {
900 case WXK_NUMPAD0:
901 case WXK_NUMPAD1:
902 case WXK_NUMPAD2:
903 case WXK_NUMPAD3:
904 case WXK_NUMPAD4:
905 case WXK_NUMPAD5:
906 case WXK_NUMPAD6:
907 case WXK_NUMPAD7:
908 case WXK_NUMPAD8:
909 case WXK_NUMPAD9:
910 case WXK_ADD:
911 case WXK_NUMPAD_ADD:
912 case WXK_SUBTRACT:
913 case WXK_NUMPAD_SUBTRACT:
914 case WXK_UP:
915 case WXK_DOWN:
ca65c044 916 return true;
f6bcfd97
BP
917
918 default:
4676948b 919 if ( (keycode < 128) && wxIsdigit(keycode) )
ca65c044 920 return true;
f6bcfd97
BP
921 }
922 }
923
ca65c044 924 return false;
f6bcfd97
BP
925}
926
816be743
VZ
927void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
928{
929 if ( !HasRange() )
930 {
12a3f227 931 int keycode = event.GetKeyCode();
4676948b 932 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-'
85d8c319
JS
933 || keycode == WXK_NUMPAD0
934 || keycode == WXK_NUMPAD1
935 || keycode == WXK_NUMPAD2
936 || keycode == WXK_NUMPAD3
937 || keycode == WXK_NUMPAD4
938 || keycode == WXK_NUMPAD5
939 || keycode == WXK_NUMPAD6
940 || keycode == WXK_NUMPAD7
941 || keycode == WXK_NUMPAD8
942 || keycode == WXK_NUMPAD9
943 || keycode == WXK_ADD
944 || keycode == WXK_NUMPAD_ADD
945 || keycode == WXK_SUBTRACT
946 || keycode == WXK_NUMPAD_SUBTRACT)
816be743
VZ
947 {
948 wxGridCellTextEditor::StartingKey(event);
949
950 // skip Skip() below
951 return;
952 }
953 }
954
955 event.Skip();
956}
9c4ba614 957
c4608a8a
VZ
958void wxGridCellNumberEditor::SetParameters(const wxString& params)
959{
960 if ( !params )
961 {
962 // reset to default
963 m_min =
964 m_max = -1;
965 }
966 else
967 {
968 long tmp;
969 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
970 {
971 m_min = (int)tmp;
972
973 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
974 {
975 m_max = (int)tmp;
976
977 // skip the error message below
978 return;
979 }
980 }
981
f6bcfd97 982 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str());
c4608a8a
VZ
983 }
984}
985
73145b0e
JS
986// return the value in the spin control if it is there (the text control otherwise)
987wxString wxGridCellNumberEditor::GetValue() const
988{
989 wxString s;
990
991 if( HasRange() )
992 {
14ac4f3c 993 long value = Spin()->GetValue();
73145b0e
JS
994 s.Printf(wxT("%ld"), value);
995 }
996 else
997 {
998 s = Text()->GetValue();
999 }
1000 return s;
1001}
1002
816be743
VZ
1003// ----------------------------------------------------------------------------
1004// wxGridCellFloatEditor
1005// ----------------------------------------------------------------------------
1006
f6bcfd97
BP
1007wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
1008{
1009 m_width = width;
1010 m_precision = precision;
1011}
1012
816be743
VZ
1013void wxGridCellFloatEditor::Create(wxWindow* parent,
1014 wxWindowID id,
1015 wxEvtHandler* evtHandler)
1016{
1017 wxGridCellTextEditor::Create(parent, id, evtHandler);
1018
1019#if wxUSE_VALIDATORS
85bc0351 1020 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
1021#endif // wxUSE_VALIDATORS
1022}
1023
1024void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
1025{
1026 // first get the value
1027 wxGridTableBase *table = grid->GetTable();
1028 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1029 {
1030 m_valueOld = table->GetValueAsDouble(row, col);
1031 }
1032 else
1033 {
8a60ff0a 1034 m_valueOld = 0.0;
a5777624 1035 wxString sValue = table->GetValue(row, col);
8a60ff0a 1036 if (! sValue.ToDouble(&m_valueOld) && ! sValue.IsEmpty())
a5777624
RD
1037 {
1038 wxFAIL_MSG( _T("this cell doesn't have float value") );
1039 return;
1040 }
816be743
VZ
1041 }
1042
1043 DoBeginEdit(GetString());
1044}
1045
3324d5f5 1046bool wxGridCellFloatEditor::EndEdit(int row, int col,
816be743
VZ
1047 wxGrid* grid)
1048{
8a60ff0a
RD
1049 double value = 0.0;
1050 wxString text(Text()->GetValue());
1051
1052 if ( (text.IsEmpty() || text.ToDouble(&value)) && (value != m_valueOld) )
816be743 1053 {
a5777624
RD
1054 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
1055 grid->GetTable()->SetValueAsDouble(row, col, value);
1056 else
8a60ff0a 1057 grid->GetTable()->SetValue(row, col, text);
816be743 1058
ca65c044 1059 return true;
816be743 1060 }
ca65c044 1061 return false;
816be743
VZ
1062}
1063
1064void wxGridCellFloatEditor::Reset()
1065{
1066 DoReset(GetString());
1067}
1068
1069void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
1070{
12a3f227 1071 int keycode = event.GetKeyCode();
3fe73755
SN
1072 char tmpbuf[2];
1073 tmpbuf[0] = (char) keycode;
1074 tmpbuf[1] = '\0';
ca65c044 1075 bool is_decimal_point = ( wxString(tmpbuf, *wxConvCurrent) ==
3fe73755 1076 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) );
60d876f3 1077 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-'
3fe73755 1078 || is_decimal_point
85d8c319
JS
1079 || keycode == WXK_NUMPAD0
1080 || keycode == WXK_NUMPAD1
1081 || keycode == WXK_NUMPAD2
1082 || keycode == WXK_NUMPAD3
1083 || keycode == WXK_NUMPAD4
1084 || keycode == WXK_NUMPAD5
1085 || keycode == WXK_NUMPAD6
1086 || keycode == WXK_NUMPAD7
1087 || keycode == WXK_NUMPAD8
1088 || keycode == WXK_NUMPAD9
1089 || keycode == WXK_ADD
1090 || keycode == WXK_NUMPAD_ADD
1091 || keycode == WXK_SUBTRACT
1092 || keycode == WXK_NUMPAD_SUBTRACT)
816be743
VZ
1093 {
1094 wxGridCellTextEditor::StartingKey(event);
1095
1096 // skip Skip() below
1097 return;
1098 }
1099
1100 event.Skip();
1101}
1102
f6bcfd97
BP
1103void wxGridCellFloatEditor::SetParameters(const wxString& params)
1104{
1105 if ( !params )
1106 {
1107 // reset to default
1108 m_width =
1109 m_precision = -1;
1110 }
1111 else
1112 {
1113 long tmp;
1114 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
1115 {
1116 m_width = (int)tmp;
1117
1118 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
1119 {
1120 m_precision = (int)tmp;
1121
1122 // skip the error message below
1123 return;
1124 }
1125 }
1126
1127 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str());
1128 }
1129}
1130
1131wxString wxGridCellFloatEditor::GetString() const
1132{
1133 wxString fmt;
1134 if ( m_width == -1 )
1135 {
1136 // default width/precision
ec53826c 1137 fmt = _T("%f");
f6bcfd97
BP
1138 }
1139 else if ( m_precision == -1 )
1140 {
1141 // default precision
ec53826c 1142 fmt.Printf(_T("%%%d.f"), m_width);
f6bcfd97
BP
1143 }
1144 else
1145 {
ec53826c 1146 fmt.Printf(_T("%%%d.%df"), m_width, m_precision);
f6bcfd97
BP
1147 }
1148
1149 return wxString::Format(fmt, m_valueOld);
1150}
1151
1152bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
1153{
1154 if ( wxGridCellEditor::IsAcceptedKey(event) )
1155 {
1156 int keycode = event.GetKeyCode();
1157 switch ( keycode )
1158 {
1159 case WXK_NUMPAD0:
1160 case WXK_NUMPAD1:
1161 case WXK_NUMPAD2:
1162 case WXK_NUMPAD3:
1163 case WXK_NUMPAD4:
1164 case WXK_NUMPAD5:
1165 case WXK_NUMPAD6:
1166 case WXK_NUMPAD7:
1167 case WXK_NUMPAD8:
1168 case WXK_NUMPAD9:
1169 case WXK_ADD:
1170 case WXK_NUMPAD_ADD:
1171 case WXK_SUBTRACT:
1172 case WXK_NUMPAD_SUBTRACT:
1173 case WXK_DECIMAL:
1174 case WXK_NUMPAD_DECIMAL:
ca65c044 1175 return true;
f6bcfd97 1176
ca65c044 1177 default:
3fe73755
SN
1178 {
1179 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1180 char tmpbuf[2];
1181 tmpbuf[0] = (char) keycode;
1182 tmpbuf[1] = '\0';
1183 bool is_decimal_point =
ca65c044 1184 ( wxString(tmpbuf, *wxConvCurrent) ==
3fe73755
SN
1185 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
1186 wxLOCALE_CAT_NUMBER) );
f6bcfd97 1187 if ( (keycode < 128) &&
60d876f3 1188 (wxIsdigit(keycode) || tolower(keycode) == 'e' ||
3fe73755 1189 is_decimal_point || keycode == '+' || keycode == '-') )
ca65c044 1190 return true;
3fe73755 1191 }
f6bcfd97
BP
1192 }
1193 }
1194
ca65c044 1195 return false;
f6bcfd97
BP
1196}
1197
3a8c693a
VZ
1198#endif // wxUSE_TEXTCTRL
1199
1200#if wxUSE_CHECKBOX
1201
508011ce
VZ
1202// ----------------------------------------------------------------------------
1203// wxGridCellBoolEditor
1204// ----------------------------------------------------------------------------
1205
1206void wxGridCellBoolEditor::Create(wxWindow* parent,
1207 wxWindowID id,
1208 wxEvtHandler* evtHandler)
1209{
1210 m_control = new wxCheckBox(parent, id, wxEmptyString,
1211 wxDefaultPosition, wxDefaultSize,
1212 wxNO_BORDER);
1213
1214 wxGridCellEditor::Create(parent, id, evtHandler);
1215}
1216
1217void wxGridCellBoolEditor::SetSize(const wxRect& r)
1218{
ca65c044 1219 bool resize = false;
b94ae1ea
VZ
1220 wxSize size = m_control->GetSize();
1221 wxCoord minSize = wxMin(r.width, r.height);
1222
1223 // check if the checkbox is not too big/small for this cell
1224 wxSize sizeBest = m_control->GetBestSize();
1225 if ( !(size == sizeBest) )
1226 {
1227 // reset to default size if it had been made smaller
1228 size = sizeBest;
1229
ca65c044 1230 resize = true;
b94ae1ea
VZ
1231 }
1232
1233 if ( size.x >= minSize || size.y >= minSize )
1234 {
1235 // leave 1 pixel margin
1236 size.x = size.y = minSize - 2;
1237
ca65c044 1238 resize = true;
b94ae1ea
VZ
1239 }
1240
1241 if ( resize )
1242 {
1243 m_control->SetSize(size);
1244 }
1245
508011ce 1246 // position it in the centre of the rectangle (TODO: support alignment?)
508011ce 1247
b94ae1ea 1248#if defined(__WXGTK__) || defined (__WXMOTIF__)
508011ce
VZ
1249 // the checkbox without label still has some space to the right in wxGTK,
1250 // so shift it to the right
b94ae1ea
VZ
1251 size.x -= 8;
1252#elif defined(__WXMSW__)
a95e38c0
VZ
1253 // here too, but in other way
1254 size.x += 1;
b94ae1ea
VZ
1255 size.y -= 2;
1256#endif
508011ce 1257
1bd71df9
JS
1258 int hAlign = wxALIGN_CENTRE;
1259 int vAlign = wxALIGN_CENTRE;
1260 if (GetCellAttr())
1261 GetCellAttr()->GetAlignment(& hAlign, & vAlign);
52d6f640 1262
1bd71df9
JS
1263 int x = 0, y = 0;
1264 if (hAlign == wxALIGN_LEFT)
1265 {
1266 x = r.x + 2;
1267#ifdef __WXMSW__
1268 x += 2;
52d6f640 1269#endif
1bd71df9
JS
1270 y = r.y + r.height/2 - size.y/2;
1271 }
1272 else if (hAlign == wxALIGN_RIGHT)
1273 {
1274 x = r.x + r.width - size.x - 2;
1275 y = r.y + r.height/2 - size.y/2;
1276 }
1277 else if (hAlign == wxALIGN_CENTRE)
1278 {
1279 x = r.x + r.width/2 - size.x/2;
1280 y = r.y + r.height/2 - size.y/2;
1281 }
52d6f640 1282
1bd71df9 1283 m_control->Move(x, y);
508011ce
VZ
1284}
1285
1286void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
1287{
99306db2
VZ
1288 m_control->Show(show);
1289
189d0213 1290 if ( show )
508011ce 1291 {
189d0213
VZ
1292 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
1293 CBox()->SetBackgroundColour(colBg);
508011ce 1294 }
508011ce
VZ
1295}
1296
1297void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
1298{
1299 wxASSERT_MSG(m_control,
1300 wxT("The wxGridCellEditor must be Created first!"));
1301
28a77bc4 1302 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
1303 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
1304 else
695a3263
MB
1305 {
1306 wxString cellval( grid->GetTable()->GetValue(row, col) );
8dd8f875 1307 m_startValue = !( !cellval || (cellval == wxT("0")) );
695a3263 1308 }
508011ce
VZ
1309 CBox()->SetValue(m_startValue);
1310 CBox()->SetFocus();
1311}
1312
1313bool wxGridCellBoolEditor::EndEdit(int row, int col,
508011ce
VZ
1314 wxGrid* grid)
1315{
1316 wxASSERT_MSG(m_control,
1317 wxT("The wxGridCellEditor must be Created first!"));
1318
ca65c044 1319 bool changed = false;
508011ce
VZ
1320 bool value = CBox()->GetValue();
1321 if ( value != m_startValue )
ca65c044 1322 changed = true;
508011ce
VZ
1323
1324 if ( changed )
1325 {
28a77bc4 1326 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
1327 grid->GetTable()->SetValueAsBool(row, col, value);
1328 else
1329 grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
508011ce
VZ
1330 }
1331
1332 return changed;
1333}
1334
1335void wxGridCellBoolEditor::Reset()
1336{
1337 wxASSERT_MSG(m_control,
1338 wxT("The wxGridCellEditor must be Created first!"));
1339
1340 CBox()->SetValue(m_startValue);
1341}
1342
e195a54c 1343void wxGridCellBoolEditor::StartingClick()
508011ce 1344{
e195a54c 1345 CBox()->SetValue(!CBox()->GetValue());
508011ce
VZ
1346}
1347
f6bcfd97
BP
1348bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event)
1349{
1350 if ( wxGridCellEditor::IsAcceptedKey(event) )
1351 {
1352 int keycode = event.GetKeyCode();
1353 switch ( keycode )
1354 {
1355 case WXK_MULTIPLY:
1356 case WXK_NUMPAD_MULTIPLY:
1357 case WXK_ADD:
1358 case WXK_NUMPAD_ADD:
1359 case WXK_SUBTRACT:
1360 case WXK_NUMPAD_SUBTRACT:
1361 case WXK_SPACE:
1362 case '+':
1363 case '-':
ca65c044 1364 return true;
f6bcfd97
BP
1365 }
1366 }
1367
ca65c044 1368 return false;
f6bcfd97 1369}
04418332 1370
73145b0e
JS
1371// return the value as "1" for true and the empty string for false
1372wxString wxGridCellBoolEditor::GetValue() const
1373{
1374 bool bSet = CBox()->GetValue();
04418332 1375 return bSet ? _T("1") : wxEmptyString;
73145b0e 1376}
f6bcfd97 1377
3a8c693a
VZ
1378#endif // wxUSE_CHECKBOX
1379
1380#if wxUSE_COMBOBOX
1381
4ee5fc9c
VZ
1382// ----------------------------------------------------------------------------
1383// wxGridCellChoiceEditor
1384// ----------------------------------------------------------------------------
1385
7db33cc3
MB
1386wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices,
1387 bool allowOthers)
1388 : m_choices(choices),
1389 m_allowOthers(allowOthers) { }
1390
4ee5fc9c 1391wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
f6bcfd97 1392 const wxString choices[],
4ee5fc9c
VZ
1393 bool allowOthers)
1394 : m_allowOthers(allowOthers)
1395{
c4608a8a 1396 if ( count )
4ee5fc9c 1397 {
c4608a8a
VZ
1398 m_choices.Alloc(count);
1399 for ( size_t n = 0; n < count; n++ )
1400 {
1401 m_choices.Add(choices[n]);
1402 }
4ee5fc9c
VZ
1403 }
1404}
1405
c4608a8a
VZ
1406wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
1407{
1408 wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
1409 editor->m_allowOthers = m_allowOthers;
1410 editor->m_choices = m_choices;
1411
1412 return editor;
1413}
1414
4ee5fc9c
VZ
1415void wxGridCellChoiceEditor::Create(wxWindow* parent,
1416 wxWindowID id,
1417 wxEvtHandler* evtHandler)
1418{
4ee5fc9c
VZ
1419 m_control = new wxComboBox(parent, id, wxEmptyString,
1420 wxDefaultPosition, wxDefaultSize,
584ad2a3 1421 m_choices,
4ee5fc9c
VZ
1422 m_allowOthers ? 0 : wxCB_READONLY);
1423
4ee5fc9c
VZ
1424 wxGridCellEditor::Create(parent, id, evtHandler);
1425}
1426
a5777624
RD
1427void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
1428 wxGridCellAttr * attr)
4ee5fc9c
VZ
1429{
1430 // as we fill the entire client area, don't do anything here to minimize
1431 // flicker
a5777624
RD
1432
1433 // TODO: It doesn't actually fill the client area since the height of a
1434 // combo always defaults to the standard... Until someone has time to
1435 // figure out the right rectangle to paint, just do it the normal way...
1436 wxGridCellEditor::PaintBackground(rectCell, attr);
4ee5fc9c
VZ
1437}
1438
1439void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
1440{
1441 wxASSERT_MSG(m_control,
1442 wxT("The wxGridCellEditor must be Created first!"));
1443
1444 m_startValue = grid->GetTable()->GetValue(row, col);
1445
2b5f62a0
VZ
1446 if (m_allowOthers)
1447 Combo()->SetValue(m_startValue);
1448 else
28a77bc4 1449 {
2b5f62a0
VZ
1450 // find the right position, or default to the first if not found
1451 int pos = Combo()->FindString(m_startValue);
1452 if (pos == -1)
1453 pos = 0;
1454 Combo()->SetSelection(pos);
28a77bc4 1455 }
4ee5fc9c
VZ
1456 Combo()->SetInsertionPointEnd();
1457 Combo()->SetFocus();
1458}
1459
28a77bc4 1460bool wxGridCellChoiceEditor::EndEdit(int row, int col,
4ee5fc9c
VZ
1461 wxGrid* grid)
1462{
1463 wxString value = Combo()->GetValue();
faffacec
VZ
1464 if ( value == m_startValue )
1465 return false;
4ee5fc9c 1466
faffacec 1467 grid->GetTable()->SetValue(row, col, value);
4ee5fc9c 1468
faffacec 1469 return true;
4ee5fc9c
VZ
1470}
1471
1472void wxGridCellChoiceEditor::Reset()
1473{
1474 Combo()->SetValue(m_startValue);
1475 Combo()->SetInsertionPointEnd();
1476}
1477
c4608a8a
VZ
1478void wxGridCellChoiceEditor::SetParameters(const wxString& params)
1479{
1480 if ( !params )
1481 {
1482 // what can we do?
1483 return;
1484 }
1485
1486 m_choices.Empty();
1487
1488 wxStringTokenizer tk(params, _T(','));
1489 while ( tk.HasMoreTokens() )
1490 {
1491 m_choices.Add(tk.GetNextToken());
1492 }
1493}
1494
73145b0e
JS
1495// return the value in the text control
1496wxString wxGridCellChoiceEditor::GetValue() const
1497{
1498 return Combo()->GetValue();
1499}
04418332 1500
3a8c693a
VZ
1501#endif // wxUSE_COMBOBOX
1502
508011ce
VZ
1503// ----------------------------------------------------------------------------
1504// wxGridCellEditorEvtHandler
1505// ----------------------------------------------------------------------------
2796cce3
RD
1506
1507void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
1508{
12a3f227 1509 switch ( event.GetKeyCode() )
2796cce3
RD
1510 {
1511 case WXK_ESCAPE:
1512 m_editor->Reset();
b54ba671 1513 m_grid->DisableCellEditControl();
2796cce3
RD
1514 break;
1515
2c9a89e0 1516 case WXK_TAB:
b51c3f27 1517 m_grid->GetEventHandler()->ProcessEvent( event );
9b4aede2
RD
1518 break;
1519
2796cce3 1520 case WXK_RETURN:
faec5a43
SN
1521 case WXK_NUMPAD_ENTER:
1522 if (!m_grid->GetEventHandler()->ProcessEvent(event))
2796cce3
RD
1523 m_editor->HandleReturn(event);
1524 break;
1525
2796cce3
RD
1526
1527 default:
1528 event.Skip();
1529 }
1530}
1531
fb0de762
RD
1532void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
1533{
12a3f227 1534 switch ( event.GetKeyCode() )
fb0de762
RD
1535 {
1536 case WXK_ESCAPE:
1537 case WXK_TAB:
1538 case WXK_RETURN:
a4f7bf58 1539 case WXK_NUMPAD_ENTER:
fb0de762
RD
1540 break;
1541
1542 default:
1543 event.Skip();
1544 }
1545}
1546
c4608a8a
VZ
1547// ----------------------------------------------------------------------------
1548// wxGridCellWorker is an (almost) empty common base class for
1549// wxGridCellRenderer and wxGridCellEditor managing ref counting
1550// ----------------------------------------------------------------------------
1551
1552void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1553{
1554 // nothing to do
1555}
1556
1557wxGridCellWorker::~wxGridCellWorker()
1558{
1559}
1560
508011ce
VZ
1561// ============================================================================
1562// renderer classes
1563// ============================================================================
1564
ab79958a
VZ
1565// ----------------------------------------------------------------------------
1566// wxGridCellRenderer
1567// ----------------------------------------------------------------------------
1568
1569void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 1570 wxGridCellAttr& attr,
ab79958a
VZ
1571 wxDC& dc,
1572 const wxRect& rect,
c78b3acd 1573 int WXUNUSED(row), int WXUNUSED(col),
ab79958a
VZ
1574 bool isSelected)
1575{
1576 dc.SetBackgroundMode( wxSOLID );
1577
04418332 1578 // grey out fields if the grid is disabled
73145b0e 1579 if( grid.IsEnabled() )
ab79958a 1580 {
73145b0e
JS
1581 if ( isSelected )
1582 {
1583 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
1584 }
1585 else
1586 {
1587 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
1588 }
52d6f640 1589 }
ab79958a
VZ
1590 else
1591 {
73145b0e 1592 dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID));
ab79958a
VZ
1593 }
1594
1595 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
1596 dc.DrawRectangle(rect);
1597}
1598
508011ce
VZ
1599// ----------------------------------------------------------------------------
1600// wxGridCellStringRenderer
1601// ----------------------------------------------------------------------------
1602
816be743
VZ
1603void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
1604 wxGridCellAttr& attr,
1605 wxDC& dc,
1606 bool isSelected)
ab79958a 1607{
ab79958a
VZ
1608 dc.SetBackgroundMode( wxTRANSPARENT );
1609
283b7808
VZ
1610 // TODO some special colours for attr.IsReadOnly() case?
1611
04418332 1612 // different coloured text when the grid is disabled
73145b0e 1613 if( grid.IsEnabled() )
ab79958a 1614 {
73145b0e
JS
1615 if ( isSelected )
1616 {
1617 dc.SetTextBackground( grid.GetSelectionBackground() );
1618 dc.SetTextForeground( grid.GetSelectionForeground() );
1619 }
1620 else
1621 {
1622 dc.SetTextBackground( attr.GetBackgroundColour() );
1623 dc.SetTextForeground( attr.GetTextColour() );
1624 }
ab79958a
VZ
1625 }
1626 else
1627 {
73145b0e
JS
1628 dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE));
1629 dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT));
ab79958a 1630 }
816be743 1631
2796cce3 1632 dc.SetFont( attr.GetFont() );
816be743
VZ
1633}
1634
65e4e78e
VZ
1635wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
1636 wxDC& dc,
1637 const wxString& text)
1638{
f6bcfd97 1639 wxCoord x = 0, y = 0, max_x = 0;
65e4e78e 1640 dc.SetFont(attr.GetFont());
f6bcfd97
BP
1641 wxStringTokenizer tk(text, _T('\n'));
1642 while ( tk.HasMoreTokens() )
1643 {
1644 dc.GetTextExtent(tk.GetNextToken(), &x, &y);
1645 max_x = wxMax(max_x, x);
1646 }
1647
1648 y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
65e4e78e 1649
f6bcfd97 1650 return wxSize(max_x, y);
65e4e78e
VZ
1651}
1652
1653wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1654 wxGridCellAttr& attr,
1655 wxDC& dc,
1656 int row, int col)
1657{
1658 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1659}
1660
816be743
VZ
1661void wxGridCellStringRenderer::Draw(wxGrid& grid,
1662 wxGridCellAttr& attr,
1663 wxDC& dc,
1664 const wxRect& rectCell,
1665 int row, int col,
1666 bool isSelected)
1667{
27f35b66 1668 wxRect rect = rectCell;
dc1f566f
SN
1669 rect.Inflate(-1);
1670
1671 // erase only this cells background, overflow cells should have been erased
1672 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1673
1674 int hAlign, vAlign;
1675 attr.GetAlignment(&hAlign, &vAlign);
27f35b66 1676
39b80349
SN
1677 int overflowCols = 0;
1678
27f35b66
SN
1679 if (attr.GetOverflow())
1680 {
1681 int cols = grid.GetNumberCols();
1682 int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
1683 int cell_rows, cell_cols;
1684 attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0
1685 if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
1686 {
1687 int i, c_cols, c_rows;
1688 for (i = col+cell_cols; i < cols; i++)
1689 {
ca65c044 1690 bool is_empty = true;
39b80349 1691 for (int j=row; j<row+cell_rows; j++)
27f35b66 1692 {
39b80349 1693 // check w/ anchor cell for multicell block
ef4d6ce8 1694 grid.GetCellSize(j, i, &c_rows, &c_cols);
39b80349 1695 if (c_rows > 0) c_rows = 0;
ef4d6ce8 1696 if (!grid.GetTable()->IsEmptyCell(j+c_rows, i))
39b80349 1697 {
ca65c044 1698 is_empty = false;
ef4d6ce8 1699 break;
39b80349 1700 }
27f35b66 1701 }
39b80349
SN
1702 if (is_empty)
1703 rect.width += grid.GetColSize(i);
dc1f566f
SN
1704 else
1705 {
1706 i--;
1707 break;
1708 }
39b80349 1709 if (rect.width >= best_width) break;
2b5f62a0 1710 }
39b80349 1711 overflowCols = i - col - cell_cols + 1;
2b5f62a0 1712 if (overflowCols >= cols) overflowCols = cols - 1;
39b80349 1713 }
27f35b66 1714
dc1f566f
SN
1715 if (overflowCols > 0) // redraw overflow cells w/ proper hilight
1716 {
39b80349 1717 hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
2b5f62a0 1718 wxRect clip = rect;
39b80349 1719 clip.x += rectCell.width;
dc1f566f
SN
1720 // draw each overflow cell individually
1721 int col_end = col+cell_cols+overflowCols;
1722 if (col_end >= grid.GetNumberCols())
2b5f62a0 1723 col_end = grid.GetNumberCols() - 1;
dc1f566f
SN
1724 for (int i = col+cell_cols; i <= col_end; i++)
1725 {
dc1f566f
SN
1726 clip.width = grid.GetColSize(i) - 1;
1727 dc.DestroyClippingRegion();
1728 dc.SetClippingRegion(clip);
39b80349
SN
1729
1730 SetTextColoursAndFont(grid, attr, dc,
2b5f62a0 1731 grid.IsInSelection(row,i));
39b80349 1732
dc1f566f 1733 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
2b5f62a0 1734 rect, hAlign, vAlign);
39b80349 1735 clip.x += grid.GetColSize(i) - 1;
dc1f566f 1736 }
ab79958a 1737
39b80349 1738 rect = rectCell;
2b5f62a0 1739 rect.Inflate(-1);
dc1f566f 1740 rect.width++;
39b80349 1741 dc.DestroyClippingRegion();
dc1f566f 1742 }
39b80349 1743 }
ab79958a 1744
dc1f566f
SN
1745 // now we only have to draw the text
1746 SetTextColoursAndFont(grid, attr, dc, isSelected);
39b80349 1747
ab79958a
VZ
1748 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1749 rect, hAlign, vAlign);
1750}
1751
65e4e78e
VZ
1752// ----------------------------------------------------------------------------
1753// wxGridCellNumberRenderer
1754// ----------------------------------------------------------------------------
1755
1756wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
1757{
1758 wxGridTableBase *table = grid.GetTable();
1759 wxString text;
1760 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1761 {
1762 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1763 }
9c4ba614
VZ
1764 else
1765 {
1766 text = table->GetValue(row, col);
1767 }
65e4e78e
VZ
1768
1769 return text;
1770}
1771
816be743
VZ
1772void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1773 wxGridCellAttr& attr,
1774 wxDC& dc,
1775 const wxRect& rectCell,
1776 int row, int col,
1777 bool isSelected)
1778{
1779 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1780
1781 SetTextColoursAndFont(grid, attr, dc, isSelected);
1782
1783 // draw the text right aligned by default
1784 int hAlign, vAlign;
1785 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 1786 hAlign = wxALIGN_RIGHT;
816be743
VZ
1787
1788 wxRect rect = rectCell;
1789 rect.Inflate(-1);
1790
65e4e78e
VZ
1791 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1792}
816be743 1793
65e4e78e
VZ
1794wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1795 wxGridCellAttr& attr,
1796 wxDC& dc,
1797 int row, int col)
1798{
1799 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1800}
1801
1802// ----------------------------------------------------------------------------
1803// wxGridCellFloatRenderer
1804// ----------------------------------------------------------------------------
1805
1806wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1807{
1808 SetWidth(width);
1809 SetPrecision(precision);
1810}
1811
e72b4213
VZ
1812wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1813{
1814 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1815 renderer->m_width = m_width;
1816 renderer->m_precision = m_precision;
1817 renderer->m_format = m_format;
1818
1819 return renderer;
1820}
1821
65e4e78e
VZ
1822wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
1823{
1824 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
1825
1826 bool hasDouble;
1827 double val;
65e4e78e
VZ
1828 wxString text;
1829 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1830 {
0b190b0f 1831 val = table->GetValueAsDouble(row, col);
ca65c044 1832 hasDouble = true;
65e4e78e 1833 }
9c4ba614
VZ
1834 else
1835 {
1836 text = table->GetValue(row, col);
0b190b0f 1837 hasDouble = text.ToDouble(&val);
9c4ba614 1838 }
65e4e78e 1839
0b190b0f
VZ
1840 if ( hasDouble )
1841 {
1842 if ( !m_format )
1843 {
1844 if ( m_width == -1 )
1845 {
19d7140e
VZ
1846 if ( m_precision == -1 )
1847 {
2b5f62a0
VZ
1848 // default width/precision
1849 m_format = _T("%f");
1850 }
19d7140e
VZ
1851 else
1852 {
1853 m_format.Printf(_T("%%.%df"), m_precision);
1854 }
0b190b0f
VZ
1855 }
1856 else if ( m_precision == -1 )
1857 {
1858 // default precision
1859 m_format.Printf(_T("%%%d.f"), m_width);
1860 }
1861 else
1862 {
1863 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
1864 }
1865 }
1866
1867 text.Printf(m_format, val);
19d7140e 1868
0b190b0f
VZ
1869 }
1870 //else: text already contains the string
1871
65e4e78e
VZ
1872 return text;
1873}
1874
816be743
VZ
1875void wxGridCellFloatRenderer::Draw(wxGrid& grid,
1876 wxGridCellAttr& attr,
1877 wxDC& dc,
1878 const wxRect& rectCell,
1879 int row, int col,
1880 bool isSelected)
1881{
1882 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1883
1884 SetTextColoursAndFont(grid, attr, dc, isSelected);
1885
1886 // draw the text right aligned by default
1887 int hAlign, vAlign;
1888 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 1889 hAlign = wxALIGN_RIGHT;
816be743
VZ
1890
1891 wxRect rect = rectCell;
1892 rect.Inflate(-1);
1893
65e4e78e
VZ
1894 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1895}
816be743 1896
65e4e78e
VZ
1897wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
1898 wxGridCellAttr& attr,
1899 wxDC& dc,
1900 int row, int col)
1901{
1902 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1903}
1904
0b190b0f
VZ
1905void wxGridCellFloatRenderer::SetParameters(const wxString& params)
1906{
0b190b0f
VZ
1907 if ( !params )
1908 {
1909 // reset to defaults
1910 SetWidth(-1);
1911 SetPrecision(-1);
1912 }
1913 else
1914 {
1915 wxString tmp = params.BeforeFirst(_T(','));
1916 if ( !!tmp )
1917 {
1918 long width;
19d7140e 1919 if ( tmp.ToLong(&width) )
0b190b0f 1920 {
19d7140e 1921 SetWidth((int)width);
0b190b0f
VZ
1922 }
1923 else
1924 {
19d7140e
VZ
1925 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
1926 }
0b190b0f 1927
19d7140e 1928 }
0b190b0f
VZ
1929 tmp = params.AfterFirst(_T(','));
1930 if ( !!tmp )
1931 {
1932 long precision;
19d7140e 1933 if ( tmp.ToLong(&precision) )
0b190b0f 1934 {
19d7140e 1935 SetPrecision((int)precision);
0b190b0f
VZ
1936 }
1937 else
1938 {
19d7140e 1939 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
0b190b0f
VZ
1940 }
1941
0b190b0f
VZ
1942 }
1943 }
1944}
1945
19d7140e 1946
508011ce
VZ
1947// ----------------------------------------------------------------------------
1948// wxGridCellBoolRenderer
1949// ----------------------------------------------------------------------------
1950
65e4e78e 1951wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 1952
b94ae1ea
VZ
1953// FIXME these checkbox size calculations are really ugly...
1954
65e4e78e 1955// between checkmark and box
a95e38c0 1956static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 1957
65e4e78e
VZ
1958wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
1959 wxGridCellAttr& WXUNUSED(attr),
1960 wxDC& WXUNUSED(dc),
1961 int WXUNUSED(row),
1962 int WXUNUSED(col))
1963{
1964 // compute it only once (no locks for MT safeness in GUI thread...)
1965 if ( !ms_sizeCheckMark.x )
297da4ba 1966 {
65e4e78e 1967 // get checkbox size
ca65c044 1968 wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString);
297da4ba 1969 wxSize size = checkbox->GetBestSize();
999836aa 1970 wxCoord checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
297da4ba 1971
65e4e78e 1972 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
69d8f612 1973#if defined(__WXGTK__) || defined(__WXMOTIF__)
65e4e78e 1974 checkSize -= size.y / 2;
297da4ba
VZ
1975#endif
1976
1977 delete checkbox;
65e4e78e
VZ
1978
1979 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
1980 }
1981
65e4e78e
VZ
1982 return ms_sizeCheckMark;
1983}
1984
1985void wxGridCellBoolRenderer::Draw(wxGrid& grid,
1986 wxGridCellAttr& attr,
1987 wxDC& dc,
1988 const wxRect& rect,
1989 int row, int col,
1990 bool isSelected)
1991{
1992 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1993
297da4ba 1994 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 1995 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
1996
1997 // don't draw outside the cell
1998 wxCoord minSize = wxMin(rect.width, rect.height);
1999 if ( size.x >= minSize || size.y >= minSize )
2000 {
2001 // and even leave (at least) 1 pixel margin
2002 size.x = size.y = minSize - 2;
2003 }
2004
2005 // draw a border around checkmark
1bd71df9
JS
2006 int vAlign, hAlign;
2007 attr.GetAlignment(& hAlign, &vAlign);
52d6f640 2008
a95e38c0 2009 wxRect rectBorder;
1bd71df9
JS
2010 if (hAlign == wxALIGN_CENTRE)
2011 {
2012 rectBorder.x = rect.x + rect.width/2 - size.x/2;
2013 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2014 rectBorder.width = size.x;
2015 rectBorder.height = size.y;
2016 }
2017 else if (hAlign == wxALIGN_LEFT)
2018 {
2019 rectBorder.x = rect.x + 2;
2020 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2021 rectBorder.width = size.x;
52d6f640 2022 rectBorder.height = size.y;
1bd71df9
JS
2023 }
2024 else if (hAlign == wxALIGN_RIGHT)
2025 {
2026 rectBorder.x = rect.x + rect.width - size.x - 2;
2027 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2028 rectBorder.width = size.x;
52d6f640 2029 rectBorder.height = size.y;
1bd71df9 2030 }
b94ae1ea 2031
f2d76237 2032 bool value;
b94ae1ea 2033 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
f2d76237
RD
2034 value = grid.GetTable()->GetValueAsBool(row, col);
2035 else
695a3263
MB
2036 {
2037 wxString cellval( grid.GetTable()->GetValue(row, col) );
8dd8f875 2038 value = !( !cellval || (cellval == wxT("0")) );
695a3263 2039 }
f2d76237
RD
2040
2041 if ( value )
508011ce 2042 {
a95e38c0
VZ
2043 wxRect rectMark = rectBorder;
2044#ifdef __WXMSW__
2045 // MSW DrawCheckMark() is weird (and should probably be changed...)
2046 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
2047 rectMark.x++;
2048 rectMark.y++;
2049#else // !MSW
2050 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
2051#endif // MSW/!MSW
2052
508011ce
VZ
2053 dc.SetTextForeground(attr.GetTextColour());
2054 dc.DrawCheckMark(rectMark);
2055 }
a95e38c0
VZ
2056
2057 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2058 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
2059 dc.DrawRectangle(rectBorder);
508011ce
VZ
2060}
2061
2796cce3
RD
2062// ----------------------------------------------------------------------------
2063// wxGridCellAttr
2064// ----------------------------------------------------------------------------
2065
1df4050d
VZ
2066void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
2067{
2068 m_nRef = 1;
2069
2070 m_isReadOnly = Unset;
2071
2072 m_renderer = NULL;
2073 m_editor = NULL;
2074
2075 m_attrkind = wxGridCellAttr::Cell;
2076
27f35b66 2077 m_sizeRows = m_sizeCols = 1;
b63fce94 2078 m_overflow = UnsetOverflow;
27f35b66 2079
1df4050d
VZ
2080 SetDefAttr(attrDefault);
2081}
2082
39bcce60 2083wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246 2084{
1df4050d
VZ
2085 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
2086
a68c1246
VZ
2087 if ( HasTextColour() )
2088 attr->SetTextColour(GetTextColour());
2089 if ( HasBackgroundColour() )
2090 attr->SetBackgroundColour(GetBackgroundColour());
2091 if ( HasFont() )
2092 attr->SetFont(GetFont());
2093 if ( HasAlignment() )
2094 attr->SetAlignment(m_hAlign, m_vAlign);
2095
27f35b66
SN
2096 attr->SetSize( m_sizeRows, m_sizeCols );
2097
a68c1246
VZ
2098 if ( m_renderer )
2099 {
2100 attr->SetRenderer(m_renderer);
39bcce60 2101 m_renderer->IncRef();
a68c1246
VZ
2102 }
2103 if ( m_editor )
2104 {
2105 attr->SetEditor(m_editor);
39bcce60 2106 m_editor->IncRef();
a68c1246
VZ
2107 }
2108
2109 if ( IsReadOnly() )
2110 attr->SetReadOnly();
2111
19d7140e
VZ
2112 attr->SetKind( m_attrkind );
2113
a68c1246
VZ
2114 return attr;
2115}
2116
19d7140e
VZ
2117void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
2118{
2119 if ( !HasTextColour() && mergefrom->HasTextColour() )
2120 SetTextColour(mergefrom->GetTextColour());
2121 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
2122 SetBackgroundColour(mergefrom->GetBackgroundColour());
2123 if ( !HasFont() && mergefrom->HasFont() )
2124 SetFont(mergefrom->GetFont());
7e48d7d9 2125 if ( !HasAlignment() && mergefrom->HasAlignment() ){
19d7140e
VZ
2126 int hAlign, vAlign;
2127 mergefrom->GetAlignment( &hAlign, &vAlign);
2128 SetAlignment(hAlign, vAlign);
2129 }
2130
27f35b66
SN
2131 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
2132
19d7140e
VZ
2133 // Directly access member functions as GetRender/Editor don't just return
2134 // m_renderer/m_editor
2135 //
2136 // Maybe add support for merge of Render and Editor?
2137 if (!HasRenderer() && mergefrom->HasRenderer() )
bf7945ce 2138 {
19d7140e
VZ
2139 m_renderer = mergefrom->m_renderer;
2140 m_renderer->IncRef();
2141 }
2142 if ( !HasEditor() && mergefrom->HasEditor() )
2143 {
2144 m_editor = mergefrom->m_editor;
2145 m_editor->IncRef();
2146 }
2147 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
2148 SetReadOnly(mergefrom->IsReadOnly());
2149
ef5df12b 2150 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
ff699386 2151 SetOverflow(mergefrom->GetOverflow());
ef5df12b 2152
19d7140e
VZ
2153 SetDefAttr(mergefrom->m_defGridAttr);
2154}
2155
27f35b66
SN
2156void wxGridCellAttr::SetSize(int num_rows, int num_cols)
2157{
2158 // The size of a cell is normally 1,1
2159
2160 // If this cell is larger (2,2) then this is the top left cell
2161 // the other cells that will be covered (lower right cells) must be
2162 // set to negative or zero values such that
2163 // row + num_rows of the covered cell points to the larger cell (this cell)
2164 // same goes for the col + num_cols.
2165
2166 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2167
2168 wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) ||
2169 !((num_rows<=0)&&(num_cols>0)) ||
2170 !((num_rows==0)&&(num_cols==0))),
2171 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2172
2173 m_sizeRows = num_rows;
2174 m_sizeCols = num_cols;
2175}
2176
2796cce3
RD
2177const wxColour& wxGridCellAttr::GetTextColour() const
2178{
2179 if (HasTextColour())
508011ce 2180 {
2796cce3 2181 return m_colText;
508011ce 2182 }
0926b2fc 2183 else if (m_defGridAttr && m_defGridAttr != this)
508011ce 2184 {
2796cce3 2185 return m_defGridAttr->GetTextColour();
508011ce
VZ
2186 }
2187 else
2188 {
2796cce3
RD
2189 wxFAIL_MSG(wxT("Missing default cell attribute"));
2190 return wxNullColour;
2191 }
2192}
2193
2194
2195const wxColour& wxGridCellAttr::GetBackgroundColour() const
2196{
2197 if (HasBackgroundColour())
2198 return m_colBack;
0926b2fc 2199 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2200 return m_defGridAttr->GetBackgroundColour();
508011ce
VZ
2201 else
2202 {
2796cce3
RD
2203 wxFAIL_MSG(wxT("Missing default cell attribute"));
2204 return wxNullColour;
2205 }
2206}
2207
2208
2209const wxFont& wxGridCellAttr::GetFont() const
2210{
2211 if (HasFont())
2212 return m_font;
0926b2fc 2213 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2214 return m_defGridAttr->GetFont();
508011ce
VZ
2215 else
2216 {
2796cce3
RD
2217 wxFAIL_MSG(wxT("Missing default cell attribute"));
2218 return wxNullFont;
2219 }
2220}
2221
2222
2223void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
2224{
508011ce
VZ
2225 if (HasAlignment())
2226 {
2796cce3
RD
2227 if ( hAlign ) *hAlign = m_hAlign;
2228 if ( vAlign ) *vAlign = m_vAlign;
2229 }
0926b2fc 2230 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2231 m_defGridAttr->GetAlignment(hAlign, vAlign);
508011ce
VZ
2232 else
2233 {
2796cce3
RD
2234 wxFAIL_MSG(wxT("Missing default cell attribute"));
2235 }
2236}
2237
27f35b66
SN
2238void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
2239{
2240 if ( num_rows ) *num_rows = m_sizeRows;
2241 if ( num_cols ) *num_cols = m_sizeCols;
2242}
2796cce3 2243
f2d76237 2244// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
2245// which attribute to use. If a non-default attr object has one then it is
2246// used, otherwise the default editor or renderer is fetched from the grid and
2247// used. It should be the default for the data type of the cell. If it is
2248// NULL (because the table has a type that the grid does not have in its
2249// registry,) then the grid's default editor or renderer is used.
2250
2251wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
2252{
3cf883a2 2253 wxGridCellRenderer *renderer;
28a77bc4 2254
3cf883a2 2255 if ( m_renderer && this != m_defGridAttr )
0b190b0f 2256 {
3cf883a2
VZ
2257 // use the cells renderer if it has one
2258 renderer = m_renderer;
2259 renderer->IncRef();
0b190b0f 2260 }
3cf883a2 2261 else // no non default cell renderer
0b190b0f 2262 {
3cf883a2
VZ
2263 // get default renderer for the data type
2264 if ( grid )
2265 {
2266 // GetDefaultRendererForCell() will do IncRef() for us
2267 renderer = grid->GetDefaultRendererForCell(row, col);
2268 }
2269 else
2270 {
2271 renderer = NULL;
2272 }
0b190b0f 2273
3cf883a2
VZ
2274 if ( !renderer )
2275 {
0926b2fc 2276 if (m_defGridAttr && this != m_defGridAttr )
3cf883a2
VZ
2277 {
2278 // if we still don't have one then use the grid default
2279 // (no need for IncRef() here neither)
2280 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
2281 }
2282 else // default grid attr
2283 {
2284 // use m_renderer which we had decided not to use initially
2285 renderer = m_renderer;
2286 if ( renderer )
2287 renderer->IncRef();
2288 }
2289 }
0b190b0f 2290 }
28a77bc4 2291
3cf883a2
VZ
2292 // we're supposed to always find something
2293 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
28a77bc4
RD
2294
2295 return renderer;
2796cce3
RD
2296}
2297
3cf883a2 2298// same as above, except for s/renderer/editor/g
28a77bc4 2299wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
07296f0b 2300{
3cf883a2 2301 wxGridCellEditor *editor;
0b190b0f 2302
3cf883a2 2303 if ( m_editor && this != m_defGridAttr )
0b190b0f 2304 {
3cf883a2
VZ
2305 // use the cells editor if it has one
2306 editor = m_editor;
2307 editor->IncRef();
0b190b0f 2308 }
3cf883a2 2309 else // no non default cell editor
0b190b0f 2310 {
3cf883a2
VZ
2311 // get default editor for the data type
2312 if ( grid )
2313 {
2314 // GetDefaultEditorForCell() will do IncRef() for us
2315 editor = grid->GetDefaultEditorForCell(row, col);
2316 }
2317 else
2318 {
2319 editor = NULL;
2320 }
2321
2322 if ( !editor )
2323 {
0926b2fc 2324 if ( m_defGridAttr && this != m_defGridAttr )
3cf883a2
VZ
2325 {
2326 // if we still don't have one then use the grid default
2327 // (no need for IncRef() here neither)
2328 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
2329 }
2330 else // default grid attr
2331 {
2332 // use m_editor which we had decided not to use initially
2333 editor = m_editor;
2334 if ( editor )
2335 editor->IncRef();
2336 }
2337 }
0b190b0f 2338 }
28a77bc4 2339
3cf883a2
VZ
2340 // we're supposed to always find something
2341 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
2342
28a77bc4 2343 return editor;
07296f0b
RD
2344}
2345
b99be8fb 2346// ----------------------------------------------------------------------------
758cbedf 2347// wxGridCellAttrData
b99be8fb
VZ
2348// ----------------------------------------------------------------------------
2349
758cbedf 2350void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb
VZ
2351{
2352 int n = FindIndex(row, col);
2353 if ( n == wxNOT_FOUND )
2354 {
2355 // add the attribute
2356 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
2357 }
2358 else
2359 {
6f36917b
VZ
2360 // free the old attribute
2361 m_attrs[(size_t)n].attr->DecRef();
2362
b99be8fb
VZ
2363 if ( attr )
2364 {
2365 // change the attribute
2e9a6788 2366 m_attrs[(size_t)n].attr = attr;
b99be8fb
VZ
2367 }
2368 else
2369 {
2370 // remove this attribute
2371 m_attrs.RemoveAt((size_t)n);
2372 }
2373 }
b99be8fb
VZ
2374}
2375
758cbedf 2376wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb
VZ
2377{
2378 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2379
2380 int n = FindIndex(row, col);
2381 if ( n != wxNOT_FOUND )
2382 {
2e9a6788
VZ
2383 attr = m_attrs[(size_t)n].attr;
2384 attr->IncRef();
b99be8fb
VZ
2385 }
2386
2387 return attr;
2388}
2389
4d60017a
SN
2390void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
2391{
2392 size_t count = m_attrs.GetCount();
2393 for ( size_t n = 0; n < count; n++ )
2394 {
2395 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2396 wxCoord row = coords.GetRow();
2397 if ((size_t)row >= pos)
2398 {
2399 if (numRows > 0)
2400 {
2401 // If rows inserted, include row counter where necessary
2402 coords.SetRow(row + numRows);
2403 }
2404 else if (numRows < 0)
2405 {
2406 // If rows deleted ...
2407 if ((size_t)row >= pos - numRows)
2408 {
2409 // ...either decrement row counter (if row still exists)...
2410 coords.SetRow(row + numRows);
2411 }
2412 else
2413 {
2414 // ...or remove the attribute
2415 m_attrs.RemoveAt((size_t)n);
2416 n--; count--;
2417 }
2418 }
4d60017a
SN
2419 }
2420 }
2421}
2422
2423void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
2424{
2425 size_t count = m_attrs.GetCount();
2426 for ( size_t n = 0; n < count; n++ )
2427 {
2428 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2429 wxCoord col = coords.GetCol();
2430 if ( (size_t)col >= pos )
2431 {
2432 if ( numCols > 0 )
2433 {
2434 // If rows inserted, include row counter where necessary
2435 coords.SetCol(col + numCols);
2436 }
2437 else if (numCols < 0)
2438 {
2439 // If rows deleted ...
2440 if ((size_t)col >= pos - numCols)
2441 {
2442 // ...either decrement row counter (if row still exists)...
2443 coords.SetCol(col + numCols);
2444 }
2445 else
2446 {
2447 // ...or remove the attribute
2448 m_attrs.RemoveAt((size_t)n);
2449 n--; count--;
2450 }
2451 }
4d60017a
SN
2452 }
2453 }
2454}
2455
758cbedf 2456int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
2457{
2458 size_t count = m_attrs.GetCount();
2459 for ( size_t n = 0; n < count; n++ )
2460 {
2461 const wxGridCellCoords& coords = m_attrs[n].coords;
2462 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
2463 {
2464 return n;
2465 }
2466 }
2467
2468 return wxNOT_FOUND;
2469}
2470
758cbedf
VZ
2471// ----------------------------------------------------------------------------
2472// wxGridRowOrColAttrData
2473// ----------------------------------------------------------------------------
2474
2475wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2476{
2477 size_t count = m_attrs.Count();
2478 for ( size_t n = 0; n < count; n++ )
2479 {
2480 m_attrs[n]->DecRef();
2481 }
2482}
2483
2484wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
2485{
2486 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2487
2488 int n = m_rowsOrCols.Index(rowOrCol);
2489 if ( n != wxNOT_FOUND )
2490 {
2491 attr = m_attrs[(size_t)n];
2492 attr->IncRef();
2493 }
2494
2495 return attr;
2496}
2497
2498void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
2499{
a95e38c0
VZ
2500 int i = m_rowsOrCols.Index(rowOrCol);
2501 if ( i == wxNOT_FOUND )
758cbedf
VZ
2502 {
2503 // add the attribute
2504 m_rowsOrCols.Add(rowOrCol);
2505 m_attrs.Add(attr);
2506 }
2507 else
2508 {
a95e38c0 2509 size_t n = (size_t)i;
758cbedf
VZ
2510 if ( attr )
2511 {
2512 // change the attribute
a95e38c0
VZ
2513 m_attrs[n]->DecRef();
2514 m_attrs[n] = attr;
758cbedf
VZ
2515 }
2516 else
2517 {
2518 // remove this attribute
a95e38c0
VZ
2519 m_attrs[n]->DecRef();
2520 m_rowsOrCols.RemoveAt(n);
2521 m_attrs.RemoveAt(n);
758cbedf
VZ
2522 }
2523 }
2524}
2525
4d60017a
SN
2526void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
2527{
2528 size_t count = m_attrs.GetCount();
2529 for ( size_t n = 0; n < count; n++ )
2530 {
2531 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
2532 if ( (size_t)rowOrCol >= pos )
2533 {
2534 if ( numRowsOrCols > 0 )
2535 {
2536 // If rows inserted, include row counter where necessary
2537 rowOrCol += numRowsOrCols;
2538 }
2539 else if ( numRowsOrCols < 0)
2540 {
2541 // If rows deleted, either decrement row counter (if row still exists)
2542 if ((size_t)rowOrCol >= pos - numRowsOrCols)
2543 rowOrCol += numRowsOrCols;
2544 else
2545 {
2546 m_rowsOrCols.RemoveAt((size_t)n);
2547 m_attrs.RemoveAt((size_t)n);
2548 n--; count--;
2549 }
2550 }
4d60017a
SN
2551 }
2552 }
2553}
2554
b99be8fb
VZ
2555// ----------------------------------------------------------------------------
2556// wxGridCellAttrProvider
2557// ----------------------------------------------------------------------------
2558
2559wxGridCellAttrProvider::wxGridCellAttrProvider()
2560{
2561 m_data = (wxGridCellAttrProviderData *)NULL;
2562}
2563
2564wxGridCellAttrProvider::~wxGridCellAttrProvider()
2565{
2566 delete m_data;
2567}
2568
2569void wxGridCellAttrProvider::InitData()
2570{
2571 m_data = new wxGridCellAttrProviderData;
2572}
2573
19d7140e
VZ
2574wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
2575 wxGridCellAttr::wxAttrKind kind ) const
b99be8fb 2576{
758cbedf
VZ
2577 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2578 if ( m_data )
2579 {
19d7140e 2580 switch(kind)
758cbedf 2581 {
19d7140e
VZ
2582 case (wxGridCellAttr::Any):
2583 //Get cached merge attributes.
2584 // Currenlty not used as no cache implemented as not mutiable
2585 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2586 if(!attr)
2587 {
2588 //Basicaly implement old version.
2589 //Also check merge cache, so we don't have to re-merge every time..
999836aa
VZ
2590 wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col);
2591 wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row);
2592 wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col);
19d7140e 2593
bf7945ce 2594 if((attrcell != attrrow) && (attrrow !=attrcol) && (attrcell != attrcol)){
19d7140e
VZ
2595 // Two or move are non NULL
2596 attr = new wxGridCellAttr;
2597 attr->SetKind(wxGridCellAttr::Merged);
2598
bf7945ce 2599 //Order important..
19d7140e
VZ
2600 if(attrcell){
2601 attr->MergeWith(attrcell);
2602 attrcell->DecRef();
2603 }
2604 if(attrcol){
2605 attr->MergeWith(attrcol);
2606 attrcol->DecRef();
2607 }
2608 if(attrrow){
2609 attr->MergeWith(attrrow);
2610 attrrow->DecRef();
2611 }
2612 //store merge attr if cache implemented
2613 //attr->IncRef();
2614 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2615 }
2616 else
758cbedf 2617 {
19d7140e
VZ
2618 // one or none is non null return it or null.
2619 if(attrrow) attr = attrrow;
2620 if(attrcol) attr = attrcol;
bf7945ce 2621 if(attrcell) attr = attrcell;
19d7140e
VZ
2622 }
2623 }
2624 break;
2625 case (wxGridCellAttr::Cell):
2626 attr = m_data->m_cellAttrs.GetAttr(row, col);
2627 break;
2628 case (wxGridCellAttr::Col):
2629 attr = m_data->m_colAttrs.GetAttr(col);
2630 break;
2631 case (wxGridCellAttr::Row):
758cbedf 2632 attr = m_data->m_rowAttrs.GetAttr(row);
19d7140e
VZ
2633 break;
2634 default:
2635 // unused as yet...
2636 // (wxGridCellAttr::Default):
2637 // (wxGridCellAttr::Merged):
2638 break;
758cbedf
VZ
2639 }
2640 }
758cbedf 2641 return attr;
b99be8fb
VZ
2642}
2643
2e9a6788 2644void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
2645 int row, int col)
2646{
2647 if ( !m_data )
2648 InitData();
2649
758cbedf
VZ
2650 m_data->m_cellAttrs.SetAttr(attr, row, col);
2651}
2652
2653void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
2654{
2655 if ( !m_data )
2656 InitData();
2657
2658 m_data->m_rowAttrs.SetAttr(attr, row);
2659}
2660
2661void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
2662{
2663 if ( !m_data )
2664 InitData();
2665
2666 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
2667}
2668
4d60017a
SN
2669void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
2670{
2671 if ( m_data )
2672 {
2673 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2674
d1c0b4f9 2675 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
2676 }
2677}
2678
2679void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
2680{
2681 if ( m_data )
2682 {
2683 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2684
d1c0b4f9 2685 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
2686 }
2687}
2688
f2d76237
RD
2689// ----------------------------------------------------------------------------
2690// wxGridTypeRegistry
2691// ----------------------------------------------------------------------------
2692
2693wxGridTypeRegistry::~wxGridTypeRegistry()
2694{
b94ae1ea
VZ
2695 size_t count = m_typeinfo.Count();
2696 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
2697 delete m_typeinfo[i];
2698}
2699
2700
2701void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2702 wxGridCellRenderer* renderer,
2703 wxGridCellEditor* editor)
2704{
f2d76237
RD
2705 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2706
2707 // is it already registered?
c4608a8a 2708 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
2709 if ( loc != wxNOT_FOUND )
2710 {
f2d76237
RD
2711 delete m_typeinfo[loc];
2712 m_typeinfo[loc] = info;
2713 }
39bcce60
VZ
2714 else
2715 {
f2d76237
RD
2716 m_typeinfo.Add(info);
2717 }
2718}
2719
c4608a8a
VZ
2720int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2721{
2722 size_t count = m_typeinfo.GetCount();
2723 for ( size_t i = 0; i < count; i++ )
2724 {
2725 if ( typeName == m_typeinfo[i]->m_typeName )
2726 {
2727 return i;
2728 }
2729 }
2730
2731 return wxNOT_FOUND;
2732}
2733
f2d76237
RD
2734int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2735{
c4608a8a
VZ
2736 int index = FindRegisteredDataType(typeName);
2737 if ( index == wxNOT_FOUND )
2738 {
2739 // check whether this is one of the standard ones, in which case
2740 // register it "on the fly"
3a8c693a 2741#if wxUSE_TEXTCTRL
c4608a8a
VZ
2742 if ( typeName == wxGRID_VALUE_STRING )
2743 {
2744 RegisterDataType(wxGRID_VALUE_STRING,
2745 new wxGridCellStringRenderer,
2746 new wxGridCellTextEditor);
3a8c693a
VZ
2747 } else
2748#endif // wxUSE_TEXTCTRL
2749#if wxUSE_CHECKBOX
2750 if ( typeName == wxGRID_VALUE_BOOL )
c4608a8a
VZ
2751 {
2752 RegisterDataType(wxGRID_VALUE_BOOL,
2753 new wxGridCellBoolRenderer,
2754 new wxGridCellBoolEditor);
3a8c693a
VZ
2755 } else
2756#endif // wxUSE_CHECKBOX
2757#if wxUSE_TEXTCTRL
2758 if ( typeName == wxGRID_VALUE_NUMBER )
c4608a8a
VZ
2759 {
2760 RegisterDataType(wxGRID_VALUE_NUMBER,
2761 new wxGridCellNumberRenderer,
2762 new wxGridCellNumberEditor);
2763 }
2764 else if ( typeName == wxGRID_VALUE_FLOAT )
2765 {
2766 RegisterDataType(wxGRID_VALUE_FLOAT,
2767 new wxGridCellFloatRenderer,
2768 new wxGridCellFloatEditor);
3a8c693a
VZ
2769 } else
2770#endif // wxUSE_TEXTCTRL
2771#if wxUSE_COMBOBOX
2772 if ( typeName == wxGRID_VALUE_CHOICE )
c4608a8a
VZ
2773 {
2774 RegisterDataType(wxGRID_VALUE_CHOICE,
2775 new wxGridCellStringRenderer,
2776 new wxGridCellChoiceEditor);
3a8c693a
VZ
2777 } else
2778#endif // wxUSE_COMBOBOX
c4608a8a
VZ
2779 {
2780 return wxNOT_FOUND;
2781 }
f2d76237 2782
c4608a8a
VZ
2783 // we get here only if just added the entry for this type, so return
2784 // the last index
2785 index = m_typeinfo.GetCount() - 1;
2786 }
2787
2788 return index;
2789}
2790
2791int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
2792{
2793 int index = FindDataType(typeName);
2794 if ( index == wxNOT_FOUND )
2795 {
2796 // the first part of the typename is the "real" type, anything after ':'
2797 // are the parameters for the renderer
2798 index = FindDataType(typeName.BeforeFirst(_T(':')));
2799 if ( index == wxNOT_FOUND )
2800 {
2801 return wxNOT_FOUND;
f2d76237 2802 }
c4608a8a
VZ
2803
2804 wxGridCellRenderer *renderer = GetRenderer(index);
2805 wxGridCellRenderer *rendererOld = renderer;
2806 renderer = renderer->Clone();
2807 rendererOld->DecRef();
2808
2809 wxGridCellEditor *editor = GetEditor(index);
2810 wxGridCellEditor *editorOld = editor;
2811 editor = editor->Clone();
2812 editorOld->DecRef();
2813
2814 // do it even if there are no parameters to reset them to defaults
2815 wxString params = typeName.AfterFirst(_T(':'));
2816 renderer->SetParameters(params);
2817 editor->SetParameters(params);
2818
2819 // register the new typename
c4608a8a
VZ
2820 RegisterDataType(typeName, renderer, editor);
2821
2822 // we just registered it, it's the last one
2823 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
2824 }
2825
c4608a8a 2826 return index;
f2d76237
RD
2827}
2828
2829wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
2830{
2831 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
faec5a43
SN
2832 if (renderer)
2833 renderer->IncRef();
f2d76237
RD
2834 return renderer;
2835}
2836
0b190b0f 2837wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
2838{
2839 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
faec5a43
SN
2840 if (editor)
2841 editor->IncRef();
f2d76237
RD
2842 return editor;
2843}
2844
758cbedf
VZ
2845// ----------------------------------------------------------------------------
2846// wxGridTableBase
2847// ----------------------------------------------------------------------------
2848
f85afd4e
MB
2849IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
2850
2851
2852wxGridTableBase::wxGridTableBase()
f85afd4e
MB
2853{
2854 m_view = (wxGrid *) NULL;
b99be8fb 2855 m_attrProvider = (wxGridCellAttrProvider *) NULL;
f85afd4e
MB
2856}
2857
2858wxGridTableBase::~wxGridTableBase()
2859{
b99be8fb
VZ
2860 delete m_attrProvider;
2861}
2862
2863void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
2864{
2865 delete m_attrProvider;
2866 m_attrProvider = attrProvider;
f85afd4e
MB
2867}
2868
f2d76237
RD
2869bool wxGridTableBase::CanHaveAttributes()
2870{
2871 if ( ! GetAttrProvider() )
2872 {
2873 // use the default attr provider by default
2874 SetAttrProvider(new wxGridCellAttrProvider);
2875 }
ca65c044 2876 return true;
f2d76237
RD
2877}
2878
19d7140e 2879wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
b99be8fb
VZ
2880{
2881 if ( m_attrProvider )
19d7140e 2882 return m_attrProvider->GetAttr(row, col, kind);
b99be8fb
VZ
2883 else
2884 return (wxGridCellAttr *)NULL;
2885}
2886
758cbedf 2887void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
2888{
2889 if ( m_attrProvider )
2890 {
19d7140e 2891 attr->SetKind(wxGridCellAttr::Cell);
b99be8fb
VZ
2892 m_attrProvider->SetAttr(attr, row, col);
2893 }
2894 else
2895 {
2896 // as we take ownership of the pointer and don't store it, we must
2897 // free it now
39bcce60 2898 wxSafeDecRef(attr);
b99be8fb
VZ
2899 }
2900}
2901
758cbedf
VZ
2902void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
2903{
2904 if ( m_attrProvider )
2905 {
19d7140e 2906 attr->SetKind(wxGridCellAttr::Row);
758cbedf
VZ
2907 m_attrProvider->SetRowAttr(attr, row);
2908 }
2909 else
2910 {
2911 // as we take ownership of the pointer and don't store it, we must
2912 // free it now
39bcce60 2913 wxSafeDecRef(attr);
758cbedf
VZ
2914 }
2915}
2916
2917void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
2918{
2919 if ( m_attrProvider )
2920 {
19d7140e 2921 attr->SetKind(wxGridCellAttr::Col);
758cbedf
VZ
2922 m_attrProvider->SetColAttr(attr, col);
2923 }
2924 else
2925 {
2926 // as we take ownership of the pointer and don't store it, we must
2927 // free it now
39bcce60 2928 wxSafeDecRef(attr);
758cbedf
VZ
2929 }
2930}
2931
aa5e1f75
SN
2932bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
2933 size_t WXUNUSED(numRows) )
f85afd4e 2934{
f6bcfd97 2935 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
8f177c8e 2936
ca65c044 2937 return false;
f85afd4e
MB
2938}
2939
aa5e1f75 2940bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 2941{
f6bcfd97 2942 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
8f177c8e 2943
ca65c044 2944 return false;
f85afd4e
MB
2945}
2946
aa5e1f75
SN
2947bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
2948 size_t WXUNUSED(numRows) )
f85afd4e 2949{
f6bcfd97 2950 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
8f177c8e 2951
ca65c044 2952 return false;
f85afd4e
MB
2953}
2954
aa5e1f75
SN
2955bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
2956 size_t WXUNUSED(numCols) )
f85afd4e 2957{
f6bcfd97 2958 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
8f177c8e 2959
ca65c044 2960 return false;
f85afd4e
MB
2961}
2962
aa5e1f75 2963bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 2964{
f6bcfd97 2965 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
8f177c8e 2966
ca65c044 2967 return false;
f85afd4e
MB
2968}
2969
aa5e1f75
SN
2970bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
2971 size_t WXUNUSED(numCols) )
f85afd4e 2972{
f6bcfd97 2973 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
8f177c8e 2974
ca65c044 2975 return false;
f85afd4e
MB
2976}
2977
2978
2979wxString wxGridTableBase::GetRowLabelValue( int row )
2980{
2981 wxString s;
f2d76237
RD
2982 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
2983 // how much it makes sense to us geeks.
f85afd4e
MB
2984 return s;
2985}
2986
2987wxString wxGridTableBase::GetColLabelValue( int col )
2988{
2989 // default col labels are:
2990 // cols 0 to 25 : A-Z
2991 // cols 26 to 675 : AA-ZZ
2992 // etc.
2993
2994 wxString s;
2995 unsigned int i, n;
2996 for ( n = 1; ; n++ )
2997 {
fef5c556 2998 s += (wxChar) (_T('A') + (wxChar)( col%26 ));
f85afd4e
MB
2999 col = col/26 - 1;
3000 if ( col < 0 ) break;
3001 }
3002
3003 // reverse the string...
3004 wxString s2;
3005 for ( i = 0; i < n; i++ )
3006 {
3007 s2 += s[n-i-1];
3008 }
3009
3010 return s2;
3011}
3012
3013
f2d76237
RD
3014wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
3015{
816be743 3016 return wxGRID_VALUE_STRING;
f2d76237
RD
3017}
3018
3019bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
3020 const wxString& typeName )
3021{
816be743 3022 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
3023}
3024
3025bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
3026{
3027 return CanGetValueAs(row, col, typeName);
3028}
3029
3030long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
3031{
3032 return 0;
3033}
3034
3035double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
3036{
3037 return 0.0;
3038}
3039
3040bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
3041{
ca65c044 3042 return false;
f2d76237
RD
3043}
3044
3045void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
3046 long WXUNUSED(value) )
3047{
3048}
3049
3050void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
3051 double WXUNUSED(value) )
3052{
3053}
3054
3055void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
3056 bool WXUNUSED(value) )
3057{
3058}
3059
3060
3061void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3062 const wxString& WXUNUSED(typeName) )
3063{
3064 return NULL;
3065}
3066
3067void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3068 const wxString& WXUNUSED(typeName),
3069 void* WXUNUSED(value) )
3070{
3071}
3072
f85afd4e
MB
3073//////////////////////////////////////////////////////////////////////
3074//
3075// Message class for the grid table to send requests and notifications
3076// to the grid view
3077//
3078
3079wxGridTableMessage::wxGridTableMessage()
3080{
3081 m_table = (wxGridTableBase *) NULL;
3082 m_id = -1;
3083 m_comInt1 = -1;
3084 m_comInt2 = -1;
3085}
3086
3087wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
3088 int commandInt1, int commandInt2 )
3089{
3090 m_table = table;
3091 m_id = id;
3092 m_comInt1 = commandInt1;
3093 m_comInt2 = commandInt2;
3094}
3095
3096
3097
3098//////////////////////////////////////////////////////////////////////
3099//
3100// A basic grid table for string data. An object of this class will
3101// created by wxGrid if you don't specify an alternative table class.
3102//
3103
223d09f6 3104WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
3105
3106IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
3107
3108wxGridStringTable::wxGridStringTable()
3109 : wxGridTableBase()
3110{
3111}
3112
3113wxGridStringTable::wxGridStringTable( int numRows, int numCols )
3114 : wxGridTableBase()
3115{
f85afd4e
MB
3116 m_data.Alloc( numRows );
3117
3118 wxArrayString sa;
3119 sa.Alloc( numCols );
27f35b66 3120 sa.Add( wxEmptyString, numCols );
8f177c8e 3121
27f35b66 3122 m_data.Add( sa, numRows );
f85afd4e
MB
3123}
3124
3125wxGridStringTable::~wxGridStringTable()
3126{
3127}
3128
e32352cf 3129int wxGridStringTable::GetNumberRows()
f85afd4e
MB
3130{
3131 return m_data.GetCount();
3132}
3133
e32352cf 3134int wxGridStringTable::GetNumberCols()
f85afd4e
MB
3135{
3136 if ( m_data.GetCount() > 0 )
3137 return m_data[0].GetCount();
3138 else
3139 return 0;
3140}
3141
3142wxString wxGridStringTable::GetValue( int row, int col )
3143{
3e13956a
RD
3144 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3145 wxEmptyString,
3146 _T("invalid row or column index in wxGridStringTable") );
af547d51 3147
f85afd4e
MB
3148 return m_data[row][col];
3149}
3150
f2d76237 3151void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 3152{
3e13956a
RD
3153 wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()),
3154 _T("invalid row or column index in wxGridStringTable") );
af547d51 3155
f2d76237 3156 m_data[row][col] = value;
f85afd4e
MB
3157}
3158
3159bool wxGridStringTable::IsEmptyCell( int row, int col )
3160{
3e13956a
RD
3161 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3162 true,
af547d51
VZ
3163 _T("invalid row or column index in wxGridStringTable") );
3164
f85afd4e
MB
3165 return (m_data[row][col] == wxEmptyString);
3166}
3167
f85afd4e
MB
3168void wxGridStringTable::Clear()
3169{
3170 int row, col;
3171 int numRows, numCols;
8f177c8e 3172
f85afd4e
MB
3173 numRows = m_data.GetCount();
3174 if ( numRows > 0 )
3175 {
3176 numCols = m_data[0].GetCount();
3177
3178 for ( row = 0; row < numRows; row++ )
3179 {
3180 for ( col = 0; col < numCols; col++ )
3181 {
3182 m_data[row][col] = wxEmptyString;
3183 }
3184 }
3185 }
3186}
3187
3188
3189bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
3190{
f85afd4e 3191 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3192 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3193 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3194
f85afd4e
MB
3195 if ( pos >= curNumRows )
3196 {
3197 return AppendRows( numRows );
3198 }
8f177c8e 3199
f85afd4e
MB
3200 wxArrayString sa;
3201 sa.Alloc( curNumCols );
27f35b66
SN
3202 sa.Add( wxEmptyString, curNumCols );
3203 m_data.Insert( sa, pos, numRows );
f85afd4e
MB
3204 if ( GetView() )
3205 {
3206 wxGridTableMessage msg( this,
3207 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
3208 pos,
3209 numRows );
8f177c8e 3210
f85afd4e
MB
3211 GetView()->ProcessTableMessage( msg );
3212 }
3213
ca65c044 3214 return true;
f85afd4e
MB
3215}
3216
3217bool wxGridStringTable::AppendRows( size_t numRows )
3218{
f85afd4e 3219 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3220 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3221 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3222
f85afd4e
MB
3223 wxArrayString sa;
3224 if ( curNumCols > 0 )
3225 {
3226 sa.Alloc( curNumCols );
27f35b66 3227 sa.Add( wxEmptyString, curNumCols );
f85afd4e 3228 }
8f177c8e 3229
27f35b66 3230 m_data.Add( sa, numRows );
f85afd4e
MB
3231
3232 if ( GetView() )
3233 {
3234 wxGridTableMessage msg( this,
3235 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
3236 numRows );
8f177c8e 3237
f85afd4e
MB
3238 GetView()->ProcessTableMessage( msg );
3239 }
3240
ca65c044 3241 return true;
f85afd4e
MB
3242}
3243
3244bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
3245{
f85afd4e 3246 size_t curNumRows = m_data.GetCount();
8f177c8e 3247
f85afd4e
MB
3248 if ( pos >= curNumRows )
3249 {
e91d2033
VZ
3250 wxFAIL_MSG( wxString::Format
3251 (
3252 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3253 (unsigned long)pos,
3254 (unsigned long)numRows,
3255 (unsigned long)curNumRows
3256 ) );
3257
ca65c044 3258 return false;
f85afd4e
MB
3259 }
3260
3261 if ( numRows > curNumRows - pos )
3262 {
3263 numRows = curNumRows - pos;
3264 }
8f177c8e 3265
f85afd4e
MB
3266 if ( numRows >= curNumRows )
3267 {
d57ad377 3268 m_data.Clear();
f85afd4e
MB
3269 }
3270 else
3271 {
27f35b66 3272 m_data.RemoveAt( pos, numRows );
f85afd4e 3273 }
f85afd4e
MB
3274 if ( GetView() )
3275 {
3276 wxGridTableMessage msg( this,
3277 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
3278 pos,
3279 numRows );
8f177c8e 3280
f85afd4e
MB
3281 GetView()->ProcessTableMessage( msg );
3282 }
3283
ca65c044 3284 return true;
f85afd4e
MB
3285}
3286
3287bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
3288{
3289 size_t row, col;
3290
3291 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3292 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3293 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3294
f85afd4e
MB
3295 if ( pos >= curNumCols )
3296 {
3297 return AppendCols( numCols );
3298 }
3299
3300 for ( row = 0; row < curNumRows; row++ )
3301 {
3302 for ( col = pos; col < pos + numCols; col++ )
3303 {
3304 m_data[row].Insert( wxEmptyString, col );
3305 }
3306 }
f85afd4e
MB
3307 if ( GetView() )
3308 {
3309 wxGridTableMessage msg( this,
3310 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
3311 pos,
3312 numCols );
8f177c8e 3313
f85afd4e
MB
3314 GetView()->ProcessTableMessage( msg );
3315 }
3316
ca65c044 3317 return true;
f85afd4e
MB
3318}
3319
3320bool wxGridStringTable::AppendCols( size_t numCols )
3321{
27f35b66 3322 size_t row;
f85afd4e
MB
3323
3324 size_t curNumRows = m_data.GetCount();
f6bcfd97 3325#if 0
f85afd4e
MB
3326 if ( !curNumRows )
3327 {
3328 // TODO: something better than this ?
3329 //
f6bcfd97 3330 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
ca65c044 3331 return false;
f85afd4e 3332 }
f6bcfd97 3333#endif
8f177c8e 3334
f85afd4e
MB
3335 for ( row = 0; row < curNumRows; row++ )
3336 {
27f35b66 3337 m_data[row].Add( wxEmptyString, numCols );
f85afd4e
MB
3338 }
3339
3340 if ( GetView() )
3341 {
3342 wxGridTableMessage msg( this,
3343 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
3344 numCols );
8f177c8e 3345
f85afd4e
MB
3346 GetView()->ProcessTableMessage( msg );
3347 }
3348
ca65c044 3349 return true;
f85afd4e
MB
3350}
3351
3352bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
3353{
27f35b66 3354 size_t row;
f85afd4e
MB
3355
3356 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3357 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3358 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3359
f85afd4e
MB
3360 if ( pos >= curNumCols )
3361 {
e91d2033
VZ
3362 wxFAIL_MSG( wxString::Format
3363 (
3364 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3365 (unsigned long)pos,
3366 (unsigned long)numCols,
3367 (unsigned long)curNumCols
3368 ) );
ca65c044 3369 return false;
f85afd4e
MB
3370 }
3371
3372 if ( numCols > curNumCols - pos )
3373 {
8f177c8e 3374 numCols = curNumCols - pos;
f85afd4e
MB
3375 }
3376
3377 for ( row = 0; row < curNumRows; row++ )
3378 {
3379 if ( numCols >= curNumCols )
3380 {
dcdce64e 3381 m_data[row].Clear();
f85afd4e
MB
3382 }
3383 else
3384 {
27f35b66 3385 m_data[row].RemoveAt( pos, numCols );
f85afd4e
MB
3386 }
3387 }
f85afd4e
MB
3388 if ( GetView() )
3389 {
3390 wxGridTableMessage msg( this,
3391 wxGRIDTABLE_NOTIFY_COLS_DELETED,
3392 pos,
3393 numCols );
8f177c8e 3394
f85afd4e
MB
3395 GetView()->ProcessTableMessage( msg );
3396 }
3397
ca65c044 3398 return true;
f85afd4e
MB
3399}
3400
3401wxString wxGridStringTable::GetRowLabelValue( int row )
3402{
3403 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3404 {
3405 // using default label
3406 //
3407 return wxGridTableBase::GetRowLabelValue( row );
3408 }
3409 else
3410 {
3411 return m_rowLabels[ row ];
3412 }
3413}
3414
3415wxString wxGridStringTable::GetColLabelValue( int col )
3416{
3417 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3418 {
3419 // using default label
3420 //
3421 return wxGridTableBase::GetColLabelValue( col );
3422 }
3423 else
3424 {
3425 return m_colLabels[ col ];
3426 }
3427}
3428
3429void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
3430{
3431 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3432 {
3433 int n = m_rowLabels.GetCount();
3434 int i;
3435 for ( i = n; i <= row; i++ )
3436 {
3437 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
3438 }
3439 }
3440
3441 m_rowLabels[row] = value;
3442}
3443
3444void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
3445{
3446 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3447 {
3448 int n = m_colLabels.GetCount();
3449 int i;
3450 for ( i = n; i <= col; i++ )
3451 {
3452 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
3453 }
3454 }
3455
3456 m_colLabels[col] = value;
3457}
3458
3459
3460
f85afd4e 3461//////////////////////////////////////////////////////////////////////
2d66e025
MB
3462//////////////////////////////////////////////////////////////////////
3463
3464IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3465
3466BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
3467 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
b51c3f27 3468 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel)
2d66e025
MB
3469 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
3470 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
f6bcfd97 3471 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp )
2d66e025
MB
3472END_EVENT_TABLE()
3473
60ff3b99
VZ
3474wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
3475 wxWindowID id,
2d66e025 3476 const wxPoint &pos, const wxSize &size )
73bb6776 3477 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3478{
3479 m_owner = parent;
3480}
3481
aa5e1f75 3482void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3483{
3484 wxPaintDC dc(this);
3485
3486 // NO - don't do this because it will set both the x and y origin
3487 // coords to match the parent scrolled window and we just want to
3488 // set the y coord - MB
3489 //
3490 // m_owner->PrepareDC( dc );
60ff3b99 3491
790ad94f 3492 int x, y;
2d66e025
MB
3493 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3494 dc.SetDeviceOrigin( 0, -y );
60ff3b99 3495
d10f4bf9
VZ
3496 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
3497 m_owner->DrawRowLabels( dc , rows );
2d66e025
MB
3498}
3499
3500
3501void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3502{
3503 m_owner->ProcessRowLabelMouseEvent( event );
3504}
3505
3506
b51c3f27
RD
3507void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3508{
3509 m_owner->GetEventHandler()->ProcessEvent(event);
3510}
3511
3512
2d66e025
MB
3513// This seems to be required for wxMotif otherwise the mouse
3514// cursor must be in the cell edit control to get key events
3515//
3516void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3517{
ffdd3c98 3518 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3519}
3520
f6bcfd97
BP
3521void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3522{
ffdd3c98 3523 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3524}
3525
2d66e025
MB
3526
3527
3528//////////////////////////////////////////////////////////////////////
3529
3530IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3531
3532BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3533 EVT_PAINT( wxGridColLabelWindow::OnPaint )
b51c3f27 3534 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel)
2d66e025
MB
3535 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3536 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
f6bcfd97 3537 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
2d66e025
MB
3538END_EVENT_TABLE()
3539
60ff3b99
VZ
3540wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3541 wxWindowID id,
2d66e025 3542 const wxPoint &pos, const wxSize &size )
73bb6776 3543 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3544{
3545 m_owner = parent;
3546}
3547
aa5e1f75 3548void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3549{
3550 wxPaintDC dc(this);
3551
3552 // NO - don't do this because it will set both the x and y origin
3553 // coords to match the parent scrolled window and we just want to
3554 // set the x coord - MB
3555 //
3556 // m_owner->PrepareDC( dc );
60ff3b99 3557
790ad94f 3558 int x, y;
2d66e025
MB
3559 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3560 dc.SetDeviceOrigin( -x, 0 );
3561
d10f4bf9
VZ
3562 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
3563 m_owner->DrawColLabels( dc , cols );
2d66e025
MB
3564}
3565
3566
3567void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3568{
3569 m_owner->ProcessColLabelMouseEvent( event );
3570}
3571
b51c3f27
RD
3572void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3573{
3574 m_owner->GetEventHandler()->ProcessEvent(event);
3575}
3576
2d66e025
MB
3577
3578// This seems to be required for wxMotif otherwise the mouse
3579// cursor must be in the cell edit control to get key events
3580//
3581void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3582{
ffdd3c98 3583 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3584}
3585
f6bcfd97
BP
3586void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3587{
ffdd3c98 3588 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3589}
3590
2d66e025
MB
3591
3592
3593//////////////////////////////////////////////////////////////////////
3594
3595IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3596
3597BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
b51c3f27 3598 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel)
2d66e025 3599 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
d2fdd8d2 3600 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
2d66e025 3601 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
f6bcfd97 3602 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
2d66e025
MB
3603END_EVENT_TABLE()
3604
60ff3b99
VZ
3605wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3606 wxWindowID id,
2d66e025 3607 const wxPoint &pos, const wxSize &size )
73bb6776 3608 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3609{
3610 m_owner = parent;
3611}
3612
d2fdd8d2
RR
3613void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3614{
3615 wxPaintDC dc(this);
b99be8fb 3616
d2fdd8d2
RR
3617 int client_height = 0;
3618 int client_width = 0;
3619 GetClientSize( &client_width, &client_height );
b99be8fb 3620
73145b0e 3621 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
d2fdd8d2
RR
3622 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
3623 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
d2fdd8d2
RR
3624 dc.DrawLine( 0, 0, client_width, 0 );
3625 dc.DrawLine( 0, 0, 0, client_height );
73145b0e
JS
3626
3627 dc.SetPen( *wxWHITE_PEN );
3628 dc.DrawLine( 1, 1, client_width-1, 1 );
3629 dc.DrawLine( 1, 1, 1, client_height-1 );
d2fdd8d2
RR
3630}
3631
2d66e025
MB
3632
3633void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3634{
3635 m_owner->ProcessCornerLabelMouseEvent( event );
3636}
3637
3638
b51c3f27
RD
3639void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3640{
3641 m_owner->GetEventHandler()->ProcessEvent(event);
3642}
3643
2d66e025
MB
3644// This seems to be required for wxMotif otherwise the mouse
3645// cursor must be in the cell edit control to get key events
3646//
3647void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3648{
ffdd3c98 3649 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3650}
3651
f6bcfd97
BP
3652void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3653{
ffdd3c98 3654 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3655}
3656
2d66e025
MB
3657
3658
f85afd4e
MB
3659//////////////////////////////////////////////////////////////////////
3660
59ddac01 3661IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
2d66e025 3662
59ddac01 3663BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
2d66e025 3664 EVT_PAINT( wxGridWindow::OnPaint )
b51c3f27 3665 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel)
2d66e025
MB
3666 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3667 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 3668 EVT_KEY_UP( wxGridWindow::OnKeyUp )
80acaf25
JS
3669 EVT_SET_FOCUS( wxGridWindow::OnFocus )
3670 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
2796cce3 3671 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
3672END_EVENT_TABLE()
3673
60ff3b99
VZ
3674wxGridWindow::wxGridWindow( wxGrid *parent,
3675 wxGridRowLabelWindow *rowLblWin,
2d66e025 3676 wxGridColLabelWindow *colLblWin,
04418332
VZ
3677 wxWindowID id,
3678 const wxPoint &pos,
3679 const wxSize &size )
73bb6776 3680 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN|wxFULL_REPAINT_ON_RESIZE,
04418332 3681 wxT("grid window") )
73145b0e 3682
2d66e025
MB
3683{
3684 m_owner = parent;
3685 m_rowLabelWin = rowLblWin;
3686 m_colLabelWin = colLblWin;
2d66e025
MB
3687}
3688
3689
3690wxGridWindow::~wxGridWindow()
3691{
3692}
3693
3694
3695void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3696{
3697 wxPaintDC dc( this );
3698 m_owner->PrepareDC( dc );
796df70a 3699 wxRegion reg = GetUpdateRegion();
d10f4bf9
VZ
3700 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3701 m_owner->DrawGridCellArea( dc , DirtyCells);
9496deb5 3702#if WXGRID_DRAW_LINES
796df70a
SN
3703 m_owner->DrawAllGridLines( dc, reg );
3704#endif
a5777624 3705 m_owner->DrawGridSpace( dc );
d10f4bf9 3706 m_owner->DrawHighlight( dc , DirtyCells );
2d66e025
MB
3707}
3708
3709
3710void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3711{
59ddac01 3712 wxWindow::ScrollWindow( dx, dy, rect );
2d66e025
MB
3713 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3714 m_colLabelWin->ScrollWindow( dx, 0, rect );
3715}
3716
3717
3718void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3719{
3720 m_owner->ProcessGridCellMouseEvent( event );
3721}
3722
b51c3f27
RD
3723void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3724{
3725 m_owner->GetEventHandler()->ProcessEvent(event);
3726}
2d66e025 3727
f6bcfd97 3728// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
3729// cursor must be in the cell edit control to get key events
3730//
3731void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3732{
ffdd3c98 3733 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025 3734}
f85afd4e 3735
f6bcfd97
BP
3736void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3737{
ffdd3c98 3738 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97 3739}
7c8a8ad5 3740
aa5e1f75 3741void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 3742{
8dd4f536 3743}
025562fe 3744
80acaf25
JS
3745void wxGridWindow::OnFocus(wxFocusEvent& event)
3746{
3747 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3748 event.Skip();
3749}
2d66e025
MB
3750
3751//////////////////////////////////////////////////////////////////////
3752
33188aa4
SN
3753// Internal Helper function for computing row or column from some
3754// (unscrolled) coordinate value, using either
70e8d961 3755// m_defaultRowHeight/m_defaultColWidth or binary search on array
33188aa4
SN
3756// of m_rowBottoms/m_ColRights to speed up the search!
3757
3758// Internal helper macros for simpler use of that function
3759
3760static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 3761 const wxArrayInt& BorderArray, int nMax,
a967f048 3762 bool clipToMinMax);
33188aa4
SN
3763
3764#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
b8d24d4e 3765 m_minAcceptableColWidth, \
ca65c044 3766 m_colRights, m_numCols, true)
33188aa4 3767#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
b8d24d4e 3768 m_minAcceptableRowHeight, \
ca65c044 3769 m_rowBottoms, m_numRows, true)
33188aa4 3770/////////////////////////////////////////////////////////////////////
07296f0b 3771
b0a877ec 3772#if wxUSE_EXTENDED_RTTI
73c36334
JS
3773WX_DEFINE_FLAGS( wxGridStyle )
3774
3ff066a4 3775wxBEGIN_FLAGS( wxGridStyle )
73c36334
JS
3776 // new style border flags, we put them first to
3777 // use them for streaming out
3ff066a4
SC
3778 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
3779 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
3780 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
3781 wxFLAGS_MEMBER(wxBORDER_RAISED)
3782 wxFLAGS_MEMBER(wxBORDER_STATIC)
3783 wxFLAGS_MEMBER(wxBORDER_NONE)
ca65c044 3784
73c36334 3785 // old style border flags
3ff066a4
SC
3786 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
3787 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
3788 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
3789 wxFLAGS_MEMBER(wxRAISED_BORDER)
3790 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 3791 wxFLAGS_MEMBER(wxBORDER)
73c36334
JS
3792
3793 // standard window styles
3ff066a4
SC
3794 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
3795 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
3796 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
3797 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 3798 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
3799 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
3800 wxFLAGS_MEMBER(wxVSCROLL)
3801 wxFLAGS_MEMBER(wxHSCROLL)
73c36334 3802
3ff066a4 3803wxEND_FLAGS( wxGridStyle )
73c36334 3804
b0a877ec
SC
3805IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
3806
3ff066a4
SC
3807wxBEGIN_PROPERTIES_TABLE(wxGrid)
3808 wxHIDE_PROPERTY( Children )
af498247 3809 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 3810wxEND_PROPERTIES_TABLE()
b0a877ec 3811
3ff066a4
SC
3812wxBEGIN_HANDLERS_TABLE(wxGrid)
3813wxEND_HANDLERS_TABLE()
b0a877ec 3814
ca65c044 3815wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
b0a877ec
SC
3816
3817/*
3818