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