]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
__WINCE__ -> __WXWINCE__
[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 9294.
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
2d0c2e79
RD
2594 if((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol)){
2595 // Two or more are non NULL
19d7140e
VZ
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
2d0c2e79 2617 {
19d7140e
VZ
2618 // one or none is non null return it or null.
2619 if(attrrow) attr = attrrow;
2d0c2e79
RD
2620 if(attrcol)
2621 {
2622 if(attr)
2623 attr->DecRef();
2624 attr = attrcol;
2625 }
2626 if(attrcell)
2627 {
2628 if(attr)
2629 attr->DecRef();
2630 attr = attrcell;
2631 }
19d7140e
VZ
2632 }
2633 }
2634 break;
2635 case (wxGridCellAttr::Cell):
2636 attr = m_data->m_cellAttrs.GetAttr(row, col);
2637 break;
2638 case (wxGridCellAttr::Col):
2d0c2e79 2639 attr = m_data->m_colAttrs.GetAttr(col);
19d7140e
VZ
2640 break;
2641 case (wxGridCellAttr::Row):
2d0c2e79 2642 attr = m_data->m_rowAttrs.GetAttr(row);
19d7140e
VZ
2643 break;
2644 default:
2645 // unused as yet...
2646 // (wxGridCellAttr::Default):
2647 // (wxGridCellAttr::Merged):
2648 break;
758cbedf
VZ
2649 }
2650 }
758cbedf 2651 return attr;
b99be8fb
VZ
2652}
2653
2e9a6788 2654void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
2655 int row, int col)
2656{
2657 if ( !m_data )
2658 InitData();
2659
758cbedf
VZ
2660 m_data->m_cellAttrs.SetAttr(attr, row, col);
2661}
2662
2663void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
2664{
2665 if ( !m_data )
2666 InitData();
2667
2668 m_data->m_rowAttrs.SetAttr(attr, row);
2669}
2670
2671void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
2672{
2673 if ( !m_data )
2674 InitData();
2675
2676 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
2677}
2678
4d60017a
SN
2679void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
2680{
2681 if ( m_data )
2682 {
2683 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2684
d1c0b4f9 2685 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
2686 }
2687}
2688
2689void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
2690{
2691 if ( m_data )
2692 {
2693 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2694
d1c0b4f9 2695 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
2696 }
2697}
2698
f2d76237
RD
2699// ----------------------------------------------------------------------------
2700// wxGridTypeRegistry
2701// ----------------------------------------------------------------------------
2702
2703wxGridTypeRegistry::~wxGridTypeRegistry()
2704{
b94ae1ea
VZ
2705 size_t count = m_typeinfo.Count();
2706 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
2707 delete m_typeinfo[i];
2708}
2709
2710
2711void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2712 wxGridCellRenderer* renderer,
2713 wxGridCellEditor* editor)
2714{
f2d76237
RD
2715 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2716
2717 // is it already registered?
c4608a8a 2718 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
2719 if ( loc != wxNOT_FOUND )
2720 {
f2d76237
RD
2721 delete m_typeinfo[loc];
2722 m_typeinfo[loc] = info;
2723 }
39bcce60
VZ
2724 else
2725 {
f2d76237
RD
2726 m_typeinfo.Add(info);
2727 }
2728}
2729
c4608a8a
VZ
2730int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2731{
2732 size_t count = m_typeinfo.GetCount();
2733 for ( size_t i = 0; i < count; i++ )
2734 {
2735 if ( typeName == m_typeinfo[i]->m_typeName )
2736 {
2737 return i;
2738 }
2739 }
2740
2741 return wxNOT_FOUND;
2742}
2743
f2d76237
RD
2744int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2745{
c4608a8a
VZ
2746 int index = FindRegisteredDataType(typeName);
2747 if ( index == wxNOT_FOUND )
2748 {
2749 // check whether this is one of the standard ones, in which case
2750 // register it "on the fly"
3a8c693a 2751#if wxUSE_TEXTCTRL
c4608a8a
VZ
2752 if ( typeName == wxGRID_VALUE_STRING )
2753 {
2754 RegisterDataType(wxGRID_VALUE_STRING,
2755 new wxGridCellStringRenderer,
2756 new wxGridCellTextEditor);
3a8c693a
VZ
2757 } else
2758#endif // wxUSE_TEXTCTRL
2759#if wxUSE_CHECKBOX
2760 if ( typeName == wxGRID_VALUE_BOOL )
c4608a8a
VZ
2761 {
2762 RegisterDataType(wxGRID_VALUE_BOOL,
2763 new wxGridCellBoolRenderer,
2764 new wxGridCellBoolEditor);
3a8c693a
VZ
2765 } else
2766#endif // wxUSE_CHECKBOX
2767#if wxUSE_TEXTCTRL
2768 if ( typeName == wxGRID_VALUE_NUMBER )
c4608a8a
VZ
2769 {
2770 RegisterDataType(wxGRID_VALUE_NUMBER,
2771 new wxGridCellNumberRenderer,
2772 new wxGridCellNumberEditor);
2773 }
2774 else if ( typeName == wxGRID_VALUE_FLOAT )
2775 {
2776 RegisterDataType(wxGRID_VALUE_FLOAT,
2777 new wxGridCellFloatRenderer,
2778 new wxGridCellFloatEditor);
3a8c693a
VZ
2779 } else
2780#endif // wxUSE_TEXTCTRL
2781#if wxUSE_COMBOBOX
2782 if ( typeName == wxGRID_VALUE_CHOICE )
c4608a8a
VZ
2783 {
2784 RegisterDataType(wxGRID_VALUE_CHOICE,
2785 new wxGridCellStringRenderer,
2786 new wxGridCellChoiceEditor);
3a8c693a
VZ
2787 } else
2788#endif // wxUSE_COMBOBOX
c4608a8a
VZ
2789 {
2790 return wxNOT_FOUND;
2791 }
f2d76237 2792
c4608a8a
VZ
2793 // we get here only if just added the entry for this type, so return
2794 // the last index
2795 index = m_typeinfo.GetCount() - 1;
2796 }
2797
2798 return index;
2799}
2800
2801int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
2802{
2803 int index = FindDataType(typeName);
2804 if ( index == wxNOT_FOUND )
2805 {
2806 // the first part of the typename is the "real" type, anything after ':'
2807 // are the parameters for the renderer
2808 index = FindDataType(typeName.BeforeFirst(_T(':')));
2809 if ( index == wxNOT_FOUND )
2810 {
2811 return wxNOT_FOUND;
f2d76237 2812 }
c4608a8a
VZ
2813
2814 wxGridCellRenderer *renderer = GetRenderer(index);
2815 wxGridCellRenderer *rendererOld = renderer;
2816 renderer = renderer->Clone();
2817 rendererOld->DecRef();
2818
2819 wxGridCellEditor *editor = GetEditor(index);
2820 wxGridCellEditor *editorOld = editor;
2821 editor = editor->Clone();
2822 editorOld->DecRef();
2823
2824 // do it even if there are no parameters to reset them to defaults
2825 wxString params = typeName.AfterFirst(_T(':'));
2826 renderer->SetParameters(params);
2827 editor->SetParameters(params);
2828
2829 // register the new typename
c4608a8a
VZ
2830 RegisterDataType(typeName, renderer, editor);
2831
2832 // we just registered it, it's the last one
2833 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
2834 }
2835
c4608a8a 2836 return index;
f2d76237
RD
2837}
2838
2839wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
2840{
2841 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
faec5a43
SN
2842 if (renderer)
2843 renderer->IncRef();
f2d76237
RD
2844 return renderer;
2845}
2846
0b190b0f 2847wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
2848{
2849 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
faec5a43
SN
2850 if (editor)
2851 editor->IncRef();
f2d76237
RD
2852 return editor;
2853}
2854
758cbedf
VZ
2855// ----------------------------------------------------------------------------
2856// wxGridTableBase
2857// ----------------------------------------------------------------------------
2858
f85afd4e
MB
2859IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
2860
2861
2862wxGridTableBase::wxGridTableBase()
f85afd4e
MB
2863{
2864 m_view = (wxGrid *) NULL;
b99be8fb 2865 m_attrProvider = (wxGridCellAttrProvider *) NULL;
f85afd4e
MB
2866}
2867
2868wxGridTableBase::~wxGridTableBase()
2869{
b99be8fb
VZ
2870 delete m_attrProvider;
2871}
2872
2873void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
2874{
2875 delete m_attrProvider;
2876 m_attrProvider = attrProvider;
f85afd4e
MB
2877}
2878
f2d76237
RD
2879bool wxGridTableBase::CanHaveAttributes()
2880{
2881 if ( ! GetAttrProvider() )
2882 {
2883 // use the default attr provider by default
2884 SetAttrProvider(new wxGridCellAttrProvider);
2885 }
ca65c044 2886 return true;
f2d76237
RD
2887}
2888
19d7140e 2889wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
b99be8fb
VZ
2890{
2891 if ( m_attrProvider )
19d7140e 2892 return m_attrProvider->GetAttr(row, col, kind);
b99be8fb
VZ
2893 else
2894 return (wxGridCellAttr *)NULL;
2895}
2896
758cbedf 2897void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
2898{
2899 if ( m_attrProvider )
2900 {
19d7140e 2901 attr->SetKind(wxGridCellAttr::Cell);
b99be8fb
VZ
2902 m_attrProvider->SetAttr(attr, row, col);
2903 }
2904 else
2905 {
2906 // as we take ownership of the pointer and don't store it, we must
2907 // free it now
39bcce60 2908 wxSafeDecRef(attr);
b99be8fb
VZ
2909 }
2910}
2911
758cbedf
VZ
2912void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
2913{
2914 if ( m_attrProvider )
2915 {
19d7140e 2916 attr->SetKind(wxGridCellAttr::Row);
758cbedf
VZ
2917 m_attrProvider->SetRowAttr(attr, row);
2918 }
2919 else
2920 {
2921 // as we take ownership of the pointer and don't store it, we must
2922 // free it now
39bcce60 2923 wxSafeDecRef(attr);
758cbedf
VZ
2924 }
2925}
2926
2927void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
2928{
2929 if ( m_attrProvider )
2930 {
19d7140e 2931 attr->SetKind(wxGridCellAttr::Col);
758cbedf
VZ
2932 m_attrProvider->SetColAttr(attr, col);
2933 }
2934 else
2935 {
2936 // as we take ownership of the pointer and don't store it, we must
2937 // free it now
39bcce60 2938 wxSafeDecRef(attr);
758cbedf
VZ
2939 }
2940}
2941
aa5e1f75
SN
2942bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
2943 size_t WXUNUSED(numRows) )
f85afd4e 2944{
f6bcfd97 2945 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
8f177c8e 2946
ca65c044 2947 return false;
f85afd4e
MB
2948}
2949
aa5e1f75 2950bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 2951{
f6bcfd97 2952 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
8f177c8e 2953
ca65c044 2954 return false;
f85afd4e
MB
2955}
2956
aa5e1f75
SN
2957bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
2958 size_t WXUNUSED(numRows) )
f85afd4e 2959{
f6bcfd97 2960 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
8f177c8e 2961
ca65c044 2962 return false;
f85afd4e
MB
2963}
2964
aa5e1f75
SN
2965bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
2966 size_t WXUNUSED(numCols) )
f85afd4e 2967{
f6bcfd97 2968 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
8f177c8e 2969
ca65c044 2970 return false;
f85afd4e
MB
2971}
2972
aa5e1f75 2973bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 2974{
f6bcfd97 2975 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
8f177c8e 2976
ca65c044 2977 return false;
f85afd4e
MB
2978}
2979
aa5e1f75
SN
2980bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
2981 size_t WXUNUSED(numCols) )
f85afd4e 2982{
f6bcfd97 2983 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
8f177c8e 2984
ca65c044 2985 return false;
f85afd4e
MB
2986}
2987
2988
2989wxString wxGridTableBase::GetRowLabelValue( int row )
2990{
2991 wxString s;
f2d76237
RD
2992 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
2993 // how much it makes sense to us geeks.
f85afd4e
MB
2994 return s;
2995}
2996
2997wxString wxGridTableBase::GetColLabelValue( int col )
2998{
2999 // default col labels are:
3000 // cols 0 to 25 : A-Z
3001 // cols 26 to 675 : AA-ZZ
3002 // etc.
3003
3004 wxString s;
3005 unsigned int i, n;
3006 for ( n = 1; ; n++ )
3007 {
fef5c556 3008 s += (wxChar) (_T('A') + (wxChar)( col%26 ));
f85afd4e
MB
3009 col = col/26 - 1;
3010 if ( col < 0 ) break;
3011 }
3012
3013 // reverse the string...
3014 wxString s2;
3015 for ( i = 0; i < n; i++ )
3016 {
3017 s2 += s[n-i-1];
3018 }
3019
3020 return s2;
3021}
3022
3023
f2d76237
RD
3024wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
3025{
816be743 3026 return wxGRID_VALUE_STRING;
f2d76237
RD
3027}
3028
3029bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
3030 const wxString& typeName )
3031{
816be743 3032 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
3033}
3034
3035bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
3036{
3037 return CanGetValueAs(row, col, typeName);
3038}
3039
3040long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
3041{
3042 return 0;
3043}
3044
3045double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
3046{
3047 return 0.0;
3048}
3049
3050bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
3051{
ca65c044 3052 return false;
f2d76237
RD
3053}
3054
3055void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
3056 long WXUNUSED(value) )
3057{
3058}
3059
3060void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
3061 double WXUNUSED(value) )
3062{
3063}
3064
3065void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
3066 bool WXUNUSED(value) )
3067{
3068}
3069
3070
3071void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3072 const wxString& WXUNUSED(typeName) )
3073{
3074 return NULL;
3075}
3076
3077void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3078 const wxString& WXUNUSED(typeName),
3079 void* WXUNUSED(value) )
3080{
3081}
3082
f85afd4e
MB
3083//////////////////////////////////////////////////////////////////////
3084//
3085// Message class for the grid table to send requests and notifications
3086// to the grid view
3087//
3088
3089wxGridTableMessage::wxGridTableMessage()
3090{
3091 m_table = (wxGridTableBase *) NULL;
3092 m_id = -1;
3093 m_comInt1 = -1;
3094 m_comInt2 = -1;
3095}
3096
3097wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
3098 int commandInt1, int commandInt2 )
3099{
3100 m_table = table;
3101 m_id = id;
3102 m_comInt1 = commandInt1;
3103 m_comInt2 = commandInt2;
3104}
3105
3106
3107
3108//////////////////////////////////////////////////////////////////////
3109//
3110// A basic grid table for string data. An object of this class will
3111// created by wxGrid if you don't specify an alternative table class.
3112//
3113
223d09f6 3114WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
3115
3116IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
3117
3118wxGridStringTable::wxGridStringTable()
3119 : wxGridTableBase()
3120{
3121}
3122
3123wxGridStringTable::wxGridStringTable( int numRows, int numCols )
3124 : wxGridTableBase()
3125{
f85afd4e
MB
3126 m_data.Alloc( numRows );
3127
3128 wxArrayString sa;
3129 sa.Alloc( numCols );
27f35b66 3130 sa.Add( wxEmptyString, numCols );
8f177c8e 3131
27f35b66 3132 m_data.Add( sa, numRows );
f85afd4e
MB
3133}
3134
3135wxGridStringTable::~wxGridStringTable()
3136{
3137}
3138
e32352cf 3139int wxGridStringTable::GetNumberRows()
f85afd4e
MB
3140{
3141 return m_data.GetCount();
3142}
3143
e32352cf 3144int wxGridStringTable::GetNumberCols()
f85afd4e
MB
3145{
3146 if ( m_data.GetCount() > 0 )
3147 return m_data[0].GetCount();
3148 else
3149 return 0;
3150}
3151
3152wxString wxGridStringTable::GetValue( int row, int col )
3153{
3e13956a
RD
3154 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3155 wxEmptyString,
3156 _T("invalid row or column index in wxGridStringTable") );
af547d51 3157
f85afd4e
MB
3158 return m_data[row][col];
3159}
3160
f2d76237 3161void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 3162{
3e13956a
RD
3163 wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()),
3164 _T("invalid row or column index in wxGridStringTable") );
af547d51 3165
f2d76237 3166 m_data[row][col] = value;
f85afd4e
MB
3167}
3168
3169bool wxGridStringTable::IsEmptyCell( int row, int col )
3170{
3e13956a
RD
3171 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3172 true,
af547d51
VZ
3173 _T("invalid row or column index in wxGridStringTable") );
3174
f85afd4e
MB
3175 return (m_data[row][col] == wxEmptyString);
3176}
3177
f85afd4e
MB
3178void wxGridStringTable::Clear()
3179{
3180 int row, col;
3181 int numRows, numCols;
8f177c8e 3182
f85afd4e
MB
3183 numRows = m_data.GetCount();
3184 if ( numRows > 0 )
3185 {
3186 numCols = m_data[0].GetCount();
3187
3188 for ( row = 0; row < numRows; row++ )
3189 {
3190 for ( col = 0; col < numCols; col++ )
3191 {
3192 m_data[row][col] = wxEmptyString;
3193 }
3194 }
3195 }
3196}
3197
3198
3199bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
3200{
f85afd4e 3201 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3202 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3203 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3204
f85afd4e
MB
3205 if ( pos >= curNumRows )
3206 {
3207 return AppendRows( numRows );
3208 }
8f177c8e 3209
f85afd4e
MB
3210 wxArrayString sa;
3211 sa.Alloc( curNumCols );
27f35b66
SN
3212 sa.Add( wxEmptyString, curNumCols );
3213 m_data.Insert( sa, pos, numRows );
f85afd4e
MB
3214 if ( GetView() )
3215 {
3216 wxGridTableMessage msg( this,
3217 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
3218 pos,
3219 numRows );
8f177c8e 3220
f85afd4e
MB
3221 GetView()->ProcessTableMessage( msg );
3222 }
3223
ca65c044 3224 return true;
f85afd4e
MB
3225}
3226
3227bool wxGridStringTable::AppendRows( size_t numRows )
3228{
f85afd4e 3229 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3230 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3231 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3232
f85afd4e
MB
3233 wxArrayString sa;
3234 if ( curNumCols > 0 )
3235 {
3236 sa.Alloc( curNumCols );
27f35b66 3237 sa.Add( wxEmptyString, curNumCols );
f85afd4e 3238 }
8f177c8e 3239
27f35b66 3240 m_data.Add( sa, numRows );
f85afd4e
MB
3241
3242 if ( GetView() )
3243 {
3244 wxGridTableMessage msg( this,
3245 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
3246 numRows );
8f177c8e 3247
f85afd4e
MB
3248 GetView()->ProcessTableMessage( msg );
3249 }
3250
ca65c044 3251 return true;
f85afd4e
MB
3252}
3253
3254bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
3255{
f85afd4e 3256 size_t curNumRows = m_data.GetCount();
8f177c8e 3257
f85afd4e
MB
3258 if ( pos >= curNumRows )
3259 {
e91d2033
VZ
3260 wxFAIL_MSG( wxString::Format
3261 (
3262 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3263 (unsigned long)pos,
3264 (unsigned long)numRows,
3265 (unsigned long)curNumRows
3266 ) );
3267
ca65c044 3268 return false;
f85afd4e
MB
3269 }
3270
3271 if ( numRows > curNumRows - pos )
3272 {
3273 numRows = curNumRows - pos;
3274 }
8f177c8e 3275
f85afd4e
MB
3276 if ( numRows >= curNumRows )
3277 {
d57ad377 3278 m_data.Clear();
f85afd4e
MB
3279 }
3280 else
3281 {
27f35b66 3282 m_data.RemoveAt( pos, numRows );
f85afd4e 3283 }
f85afd4e
MB
3284 if ( GetView() )
3285 {
3286 wxGridTableMessage msg( this,
3287 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
3288 pos,
3289 numRows );
8f177c8e 3290
f85afd4e
MB
3291 GetView()->ProcessTableMessage( msg );
3292 }
3293
ca65c044 3294 return true;
f85afd4e
MB
3295}
3296
3297bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
3298{
3299 size_t row, col;
3300
3301 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3302 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3303 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3304
f85afd4e
MB
3305 if ( pos >= curNumCols )
3306 {
3307 return AppendCols( numCols );
3308 }
3309
3310 for ( row = 0; row < curNumRows; row++ )
3311 {
3312 for ( col = pos; col < pos + numCols; col++ )
3313 {
3314 m_data[row].Insert( wxEmptyString, col );
3315 }
3316 }
f85afd4e
MB
3317 if ( GetView() )
3318 {
3319 wxGridTableMessage msg( this,
3320 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
3321 pos,
3322 numCols );
8f177c8e 3323
f85afd4e
MB
3324 GetView()->ProcessTableMessage( msg );
3325 }
3326
ca65c044 3327 return true;
f85afd4e
MB
3328}
3329
3330bool wxGridStringTable::AppendCols( size_t numCols )
3331{
27f35b66 3332 size_t row;
f85afd4e
MB
3333
3334 size_t curNumRows = m_data.GetCount();
f6bcfd97 3335#if 0
f85afd4e
MB
3336 if ( !curNumRows )
3337 {
3338 // TODO: something better than this ?
3339 //
f6bcfd97 3340 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
ca65c044 3341 return false;
f85afd4e 3342 }
f6bcfd97 3343#endif
8f177c8e 3344
f85afd4e
MB
3345 for ( row = 0; row < curNumRows; row++ )
3346 {
27f35b66 3347 m_data[row].Add( wxEmptyString, numCols );
f85afd4e
MB
3348 }
3349
3350 if ( GetView() )
3351 {
3352 wxGridTableMessage msg( this,
3353 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
3354 numCols );
8f177c8e 3355
f85afd4e
MB
3356 GetView()->ProcessTableMessage( msg );
3357 }
3358
ca65c044 3359 return true;
f85afd4e
MB
3360}
3361
3362bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
3363{
27f35b66 3364 size_t row;
f85afd4e
MB
3365
3366 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3367 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3368 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3369
f85afd4e
MB
3370 if ( pos >= curNumCols )
3371 {
e91d2033
VZ
3372 wxFAIL_MSG( wxString::Format
3373 (
3374 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3375 (unsigned long)pos,
3376 (unsigned long)numCols,
3377 (unsigned long)curNumCols
3378 ) );
ca65c044 3379 return false;
f85afd4e
MB
3380 }
3381
3382 if ( numCols > curNumCols - pos )
3383 {
8f177c8e 3384 numCols = curNumCols - pos;
f85afd4e
MB
3385 }
3386
3387 for ( row = 0; row < curNumRows; row++ )
3388 {
3389 if ( numCols >= curNumCols )
3390 {
dcdce64e 3391 m_data[row].Clear();
f85afd4e
MB
3392 }
3393 else
3394 {
27f35b66 3395 m_data[row].RemoveAt( pos, numCols );
f85afd4e
MB
3396 }
3397 }
f85afd4e
MB
3398 if ( GetView() )
3399 {
3400 wxGridTableMessage msg( this,
3401 wxGRIDTABLE_NOTIFY_COLS_DELETED,
3402 pos,
3403 numCols );
8f177c8e 3404
f85afd4e
MB
3405 GetView()->ProcessTableMessage( msg );
3406 }
3407
ca65c044 3408 return true;
f85afd4e
MB
3409}
3410
3411wxString wxGridStringTable::GetRowLabelValue( int row )
3412{
3413 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3414 {
3415 // using default label
3416 //
3417 return wxGridTableBase::GetRowLabelValue( row );
3418 }
3419 else
3420 {
3421 return m_rowLabels[ row ];
3422 }
3423}
3424
3425wxString wxGridStringTable::GetColLabelValue( int col )
3426{
3427 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3428 {
3429 // using default label
3430 //
3431 return wxGridTableBase::GetColLabelValue( col );
3432 }
3433 else
3434 {
3435 return m_colLabels[ col ];
3436 }
3437}
3438
3439void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
3440{
3441 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3442 {
3443 int n = m_rowLabels.GetCount();
3444 int i;
3445 for ( i = n; i <= row; i++ )
3446 {
3447 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
3448 }
3449 }
3450
3451 m_rowLabels[row] = value;
3452}
3453
3454void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
3455{
3456 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3457 {
3458 int n = m_colLabels.GetCount();
3459 int i;
3460 for ( i = n; i <= col; i++ )
3461 {
3462 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
3463 }
3464 }
3465
3466 m_colLabels[col] = value;
3467}
3468
3469
3470
f85afd4e 3471//////////////////////////////////////////////////////////////////////
2d66e025
MB
3472//////////////////////////////////////////////////////////////////////
3473
3474IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3475
3476BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
3477 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
b51c3f27 3478 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel)
2d66e025
MB
3479 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
3480 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
f6bcfd97 3481 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp )
2d66e025
MB
3482END_EVENT_TABLE()
3483
60ff3b99
VZ
3484wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
3485 wxWindowID id,
2d66e025 3486 const wxPoint &pos, const wxSize &size )
73bb6776 3487 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3488{
3489 m_owner = parent;
3490}
3491
aa5e1f75 3492void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3493{
3494 wxPaintDC dc(this);
3495
3496 // NO - don't do this because it will set both the x and y origin
3497 // coords to match the parent scrolled window and we just want to
3498 // set the y coord - MB
3499 //
3500 // m_owner->PrepareDC( dc );
60ff3b99 3501
790ad94f 3502 int x, y;
2d66e025
MB
3503 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3504 dc.SetDeviceOrigin( 0, -y );
60ff3b99 3505
d10f4bf9
VZ
3506 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
3507 m_owner->DrawRowLabels( dc , rows );
2d66e025
MB
3508}
3509
3510
3511void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3512{
3513 m_owner->ProcessRowLabelMouseEvent( event );
3514}
3515
3516
b51c3f27
RD
3517void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3518{
3519 m_owner->GetEventHandler()->ProcessEvent(event);
3520}
3521
3522
2d66e025
MB
3523// This seems to be required for wxMotif otherwise the mouse
3524// cursor must be in the cell edit control to get key events
3525//
3526void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3527{
ffdd3c98 3528 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3529}
3530
f6bcfd97
BP
3531void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3532{
ffdd3c98 3533 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3534}
3535
2d66e025
MB
3536
3537
3538//////////////////////////////////////////////////////////////////////
3539
3540IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3541
3542BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3543 EVT_PAINT( wxGridColLabelWindow::OnPaint )
b51c3f27 3544 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel)
2d66e025
MB
3545 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3546 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
f6bcfd97 3547 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
2d66e025
MB
3548END_EVENT_TABLE()
3549
60ff3b99
VZ
3550wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3551 wxWindowID id,
2d66e025 3552 const wxPoint &pos, const wxSize &size )
73bb6776 3553 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3554{
3555 m_owner = parent;
3556}
3557
aa5e1f75 3558void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3559{
3560 wxPaintDC dc(this);
3561
3562 // NO - don't do this because it will set both the x and y origin
3563 // coords to match the parent scrolled window and we just want to
3564 // set the x coord - MB
3565 //
3566 // m_owner->PrepareDC( dc );
60ff3b99 3567
790ad94f 3568 int x, y;
2d66e025
MB
3569 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3570 dc.SetDeviceOrigin( -x, 0 );
3571
d10f4bf9
VZ
3572 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
3573 m_owner->DrawColLabels( dc , cols );
2d66e025
MB
3574}
3575
3576
3577void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3578{
3579 m_owner->ProcessColLabelMouseEvent( event );
3580}
3581
b51c3f27
RD
3582void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3583{
3584 m_owner->GetEventHandler()->ProcessEvent(event);
3585}
3586
2d66e025
MB
3587
3588// This seems to be required for wxMotif otherwise the mouse
3589// cursor must be in the cell edit control to get key events
3590//
3591void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3592{
ffdd3c98 3593 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3594}
3595
f6bcfd97
BP
3596void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3597{
ffdd3c98 3598 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3599}
3600
2d66e025
MB
3601
3602
3603//////////////////////////////////////////////////////////////////////
3604
3605IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3606
3607BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
b51c3f27 3608 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel)
2d66e025 3609 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
d2fdd8d2 3610 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
2d66e025 3611 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
f6bcfd97 3612 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
2d66e025
MB
3613END_EVENT_TABLE()
3614
60ff3b99
VZ
3615wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3616 wxWindowID id,
2d66e025 3617 const wxPoint &pos, const wxSize &size )
73bb6776 3618 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3619{
3620 m_owner = parent;
3621}
3622
d2fdd8d2
RR
3623void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3624{
3625 wxPaintDC dc(this);
b99be8fb 3626
d2fdd8d2
RR
3627 int client_height = 0;
3628 int client_width = 0;
3629 GetClientSize( &client_width, &client_height );
b99be8fb 3630
73145b0e 3631 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
d2fdd8d2
RR
3632 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
3633 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
d2fdd8d2
RR
3634 dc.DrawLine( 0, 0, client_width, 0 );
3635 dc.DrawLine( 0, 0, 0, client_height );
73145b0e
JS
3636
3637 dc.SetPen( *wxWHITE_PEN );
3638 dc.DrawLine( 1, 1, client_width-1, 1 );
3639 dc.DrawLine( 1, 1, 1, client_height-1 );
d2fdd8d2
RR
3640}
3641
2d66e025
MB
3642
3643void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3644{
3645 m_owner->ProcessCornerLabelMouseEvent( event );
3646}
3647
3648
b51c3f27
RD
3649void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3650{
3651 m_owner->GetEventHandler()->ProcessEvent(event);
3652}
3653
2d66e025
MB
3654// This seems to be required for wxMotif otherwise the mouse
3655// cursor must be in the cell edit control to get key events
3656//
3657void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3658{
ffdd3c98 3659 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3660}
3661
f6bcfd97
BP
3662void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3663{
ffdd3c98 3664 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3665}
3666
2d66e025
MB
3667
3668
f85afd4e
MB
3669//////////////////////////////////////////////////////////////////////
3670
59ddac01 3671IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
2d66e025 3672
59ddac01 3673BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
2d66e025 3674 EVT_PAINT( wxGridWindow::OnPaint )
b51c3f27 3675 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel)
2d66e025
MB
3676 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3677 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 3678 EVT_KEY_UP( wxGridWindow::OnKeyUp )
80acaf25
JS
3679 EVT_SET_FOCUS( wxGridWindow::OnFocus )
3680 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
2796cce3 3681 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
3682END_EVENT_TABLE()
3683
60ff3b99
VZ
3684wxGridWindow::wxGridWindow( wxGrid *parent,
3685 wxGridRowLabelWindow *rowLblWin,
2d66e025 3686 wxGridColLabelWindow *colLblWin,
04418332
VZ
3687 wxWindowID id,
3688 const wxPoint &pos,
3689 const wxSize &size )
73bb6776 3690 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN|wxFULL_REPAINT_ON_RESIZE,
04418332 3691 wxT("grid window") )
73145b0e 3692
2d66e025
MB
3693{
3694 m_owner = parent;
3695 m_rowLabelWin = rowLblWin;
3696 m_colLabelWin = colLblWin;
2d66e025
MB
3697}
3698
3699
3700wxGridWindow::~wxGridWindow()
3701{
3702}
3703
3704
3705void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3706{
3707 wxPaintDC dc( this );
3708 m_owner->PrepareDC( dc );
796df70a 3709 wxRegion reg = GetUpdateRegion();
d10f4bf9
VZ
3710 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3711 m_owner->DrawGridCellArea( dc , DirtyCells);
9496deb5 3712#if WXGRID_DRAW_LINES
796df70a
SN
3713 m_owner->DrawAllGridLines( dc, reg );
3714#endif
a5777624 3715 m_owner->DrawGridSpace( dc );
d10f4bf9 3716 m_owner->DrawHighlight( dc , DirtyCells );
2d66e025
MB
3717}
3718
3719
3720void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3721{
59ddac01 3722 wxWindow::ScrollWindow( dx, dy, rect );
2d66e025
MB
3723 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3724 m_colLabelWin->ScrollWindow( dx, 0, rect );
3725}
3726
3727
3728void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3729{
3730 m_owner->ProcessGridCellMouseEvent( event );
3731}
3732
b51c3f27
RD
3733void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3734{
3735 m_owner->GetEventHandler()->ProcessEvent(event);
3736}
2d66e025 3737
f6bcfd97 3738// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
3739// cursor must be in the cell edit control to get key events
3740//
3741void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3742{
ffdd3c98 3743 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025 3744}
f85afd4e 3745
f6bcfd97
BP
3746void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3747{
ffdd3c98 3748 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97 3749}
7c8a8ad5 3750
aa5e1f75 3751void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 3752{
8dd4f536 3753}
025562fe 3754
80acaf25
JS
3755void wxGridWindow::OnFocus(wxFocusEvent& event)
3756{
3757 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3758 event.Skip();
3759}
2d66e025
MB
3760
3761//////////////////////////////////////////////////////////////////////
3762
33188aa4
SN
3763// Internal Helper function for computing row or column from some
3764// (unscrolled) coordinate value, using either
70e8d961 3765// m_defaultRowHeight/m_defaultColWidth or binary search on array
33188aa4
SN
3766// of m_rowBottoms/m_ColRights to speed up the search!
3767
3768// Internal helper macros for simpler use of that function
3769
3770static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 3771 const wxArrayInt& BorderArray, int nMax,
a967f048 3772 bool clipToMinMax);
33188aa4
SN
3773
3774#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
b8d24d4e 3775 m_minAcceptableColWidth, \
ca65c044 3776 m_colRights, m_numCols, true)
33188aa4 3777#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
b8d24d4e 3778 m_minAcceptableRowHeight, \
ca65c044 3779 m_rowBottoms, m_numRows, true)
33188aa4 3780/////////////////////////////////////////////////////////////////////
07296f0b 3781
b0a877ec 3782#if wxUSE_EXTENDED_RTTI
73c36334
JS
3783WX_DEFINE_FLAGS( wxGridStyle )
3784
3ff066a4 3785wxBEGIN_FLAGS( wxGridStyle )
73c36334
JS
3786 // new style border flags, we put them first to
3787 // use them for streaming out
3ff066a4
SC
3788 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
3789 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
3790 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
3791 wxFLAGS_MEMBER(wxBORDER_RAISED)
3792 wxFLAGS_MEMBER(wxBORDER_STATIC)
3793 wxFLAGS_MEMBER(wxBORDER_NONE)
ca65c044 3794
73c36334 3795 // old style border flags
3ff066a4
SC
3796 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
3797 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
3798 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
3799 wxFLAGS_MEMBER(wxRAISED_BORDER)
3800 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 3801 wxFLAGS_MEMBER(wxBORDER)
73c36334
JS
3802
3803 // standard window styles
3ff066a4
SC
3804 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
3805 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
3806 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
3807 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 3808 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
3809 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
3810 wxFLAGS_MEMBER(wxVSCROLL)
3811 wxFLAGS_MEMBER(wxHSCROLL)
73c36334 3812
3ff066a4 3813wxEND_FLAGS( wxGridStyle )
73c36334 3814
b0a877ec
SC
3815IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
3816
3ff066a4
SC
3817wxBEGIN_PROPERTIES_TABLE(wxGrid)
3818 wxHIDE_PROPERTY( Children )
af498247 3819 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 3820wxEND_PROPERTIES_TABLE()
b0a877ec 3821
3ff066a4
SC
3822wxBEGIN_HANDLERS_TABLE(wxGrid)
3823wxEND_HANDLERS_TABLE()
b0a877ec 3824
ca65c044 3825wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
b0a877ec
SC
3826
3827/*
2d0c2e79 3828