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