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