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