]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Applied patch [ 1428990 ] wxUniv: patch for toolbar button remain in pressed status
[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 9910.
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{
7db713ae
JS
1590 int row = m_grid->GetGridCursorRow();
1591 int col = m_grid->GetGridCursorCol();
1592 wxRect rect = m_grid->CellToRect( row, col );
1593 int cw, ch;
1594 m_grid->GetGridWindow()->GetClientSize( &cw, &ch );
1595 // if cell width is smaller than grid client area, cell is wholly visible
1596 bool wholeCellVisible = (rect.GetWidth() < cw);
1597
12a3f227 1598 switch ( event.GetKeyCode() )
fb0de762
RD
1599 {
1600 case WXK_ESCAPE:
1601 case WXK_TAB:
1602 case WXK_RETURN:
a4f7bf58 1603 case WXK_NUMPAD_ENTER:
fb0de762
RD
1604 break;
1605
7db713ae
JS
1606 case WXK_HOME:
1607 {
1608 if( wholeCellVisible )
1609 {
1610 // no special processing needed...
1611 event.Skip();
1612 break;
1613 }
1614
1615 // do special processing for partly visible cell...
1616
1617 // get the widths of all cells previous to this one
1618 int colXPos = 0;
1619 for ( int i = 0; i < col; i++ )
1620 {
1621 colXPos += m_grid->GetColSize(i);
1622 }
1623
1624 int xUnit = 1, yUnit = 1;
1625 m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit);
1626 if (col != 0)
1627 {
1628 m_grid->Scroll(colXPos/xUnit-1, m_grid->GetScrollPos(wxVERTICAL));
1629 }
1630 else
1631 {
1632 m_grid->Scroll(colXPos/xUnit, m_grid->GetScrollPos(wxVERTICAL));
1633 }
1634 event.Skip();
1635 break;
1636 }
1637 case WXK_END:
1638 {
1639 if( wholeCellVisible )
1640 {
1641 // no special processing needed...
1642 event.Skip();
1643 break;
1644 }
1645
1646 // do special processing for partly visible cell...
1647
1648 int textWidth = 0;
1649 wxString value = m_grid->GetCellValue(row, col);
1650 if ( wxEmptyString != value )
1651 {
1652 // get width of cell CONTENTS (text)
1653 int y;
1654 wxFont font = m_grid->GetCellFont(row, col);
1655 m_grid->GetTextExtent(value, &textWidth, &y, NULL, NULL, &font);
1656 // try to RIGHT align the text by scrolling
1657 int client_right = m_grid->GetGridWindow()->GetClientSize().GetWidth();
1658 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1659 // otherwise the last part of the cell content might be hidden below the scroll bar
1660 // FIXME: maybe there is a more suitable correction?
1661 textWidth -= (client_right - (m_grid->GetScrollLineX()*2));
1662 if ( textWidth < 0 )
1663 {
1664 textWidth = 0;
1665 }
1666 }
1667
1668 // get the widths of all cells previous to this one
1669 int colXPos = 0;
1670 for ( int i = 0; i < col; i++ )
1671 {
1672 colXPos += m_grid->GetColSize(i);
1673 }
1674 // and add the (modified) text width of the cell contents
1675 // as we'd like to see the last part of the cell contents
1676 colXPos += textWidth;
1677
1678 int xUnit = 1, yUnit = 1;
1679 m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit);
1680 m_grid->Scroll(colXPos/xUnit-1, m_grid->GetScrollPos(wxVERTICAL));
1681 event.Skip();
1682 break;
1683 }
1684
fb0de762
RD
1685 default:
1686 event.Skip();
1687 }
1688}
1689
c4608a8a
VZ
1690// ----------------------------------------------------------------------------
1691// wxGridCellWorker is an (almost) empty common base class for
1692// wxGridCellRenderer and wxGridCellEditor managing ref counting
1693// ----------------------------------------------------------------------------
1694
1695void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1696{
1697 // nothing to do
1698}
1699
1700wxGridCellWorker::~wxGridCellWorker()
1701{
1702}
1703
508011ce
VZ
1704// ============================================================================
1705// renderer classes
1706// ============================================================================
1707
ab79958a
VZ
1708// ----------------------------------------------------------------------------
1709// wxGridCellRenderer
1710// ----------------------------------------------------------------------------
1711
1712void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 1713 wxGridCellAttr& attr,
ab79958a
VZ
1714 wxDC& dc,
1715 const wxRect& rect,
c78b3acd 1716 int WXUNUSED(row), int WXUNUSED(col),
ab79958a
VZ
1717 bool isSelected)
1718{
1719 dc.SetBackgroundMode( wxSOLID );
1720
04418332 1721 // grey out fields if the grid is disabled
4db6714b 1722 if ( grid.IsEnabled() )
ab79958a 1723 {
ec157c8f
WS
1724 if ( isSelected )
1725 {
1726 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
1727 }
1728 else
1729 {
1730 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
1731 }
52d6f640 1732 }
ab79958a
VZ
1733 else
1734 {
ec157c8f 1735 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxSOLID));
ab79958a
VZ
1736 }
1737
1738 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
1739 dc.DrawRectangle(rect);
1740}
1741
508011ce
VZ
1742// ----------------------------------------------------------------------------
1743// wxGridCellStringRenderer
1744// ----------------------------------------------------------------------------
1745
fbfb8bcc
VZ
1746void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid,
1747 const wxGridCellAttr& attr,
816be743
VZ
1748 wxDC& dc,
1749 bool isSelected)
ab79958a 1750{
ab79958a
VZ
1751 dc.SetBackgroundMode( wxTRANSPARENT );
1752
283b7808
VZ
1753 // TODO some special colours for attr.IsReadOnly() case?
1754
04418332 1755 // different coloured text when the grid is disabled
4db6714b 1756 if ( grid.IsEnabled() )
ab79958a 1757 {
73145b0e
JS
1758 if ( isSelected )
1759 {
1760 dc.SetTextBackground( grid.GetSelectionBackground() );
1761 dc.SetTextForeground( grid.GetSelectionForeground() );
1762 }
1763 else
1764 {
1765 dc.SetTextBackground( attr.GetBackgroundColour() );
1766 dc.SetTextForeground( attr.GetTextColour() );
1767 }
ab79958a
VZ
1768 }
1769 else
1770 {
ec157c8f
WS
1771 dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1772 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
ab79958a 1773 }
816be743 1774
2796cce3 1775 dc.SetFont( attr.GetFont() );
816be743
VZ
1776}
1777
fbfb8bcc 1778wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr,
65e4e78e
VZ
1779 wxDC& dc,
1780 const wxString& text)
1781{
f6bcfd97 1782 wxCoord x = 0, y = 0, max_x = 0;
65e4e78e 1783 dc.SetFont(attr.GetFont());
f6bcfd97
BP
1784 wxStringTokenizer tk(text, _T('\n'));
1785 while ( tk.HasMoreTokens() )
1786 {
1787 dc.GetTextExtent(tk.GetNextToken(), &x, &y);
1788 max_x = wxMax(max_x, x);
1789 }
1790
1791 y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
65e4e78e 1792
f6bcfd97 1793 return wxSize(max_x, y);
65e4e78e
VZ
1794}
1795
1796wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1797 wxGridCellAttr& attr,
1798 wxDC& dc,
1799 int row, int col)
1800{
1801 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1802}
1803
816be743
VZ
1804void wxGridCellStringRenderer::Draw(wxGrid& grid,
1805 wxGridCellAttr& attr,
1806 wxDC& dc,
1807 const wxRect& rectCell,
1808 int row, int col,
1809 bool isSelected)
1810{
27f35b66 1811 wxRect rect = rectCell;
dc1f566f
SN
1812 rect.Inflate(-1);
1813
1814 // erase only this cells background, overflow cells should have been erased
1815 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1816
1817 int hAlign, vAlign;
1818 attr.GetAlignment(&hAlign, &vAlign);
27f35b66 1819
39b80349
SN
1820 int overflowCols = 0;
1821
27f35b66
SN
1822 if (attr.GetOverflow())
1823 {
1824 int cols = grid.GetNumberCols();
1825 int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
1826 int cell_rows, cell_cols;
1827 attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0
1828 if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
1829 {
1830 int i, c_cols, c_rows;
1831 for (i = col+cell_cols; i < cols; i++)
1832 {
ca65c044 1833 bool is_empty = true;
39b80349 1834 for (int j=row; j<row+cell_rows; j++)
27f35b66 1835 {
39b80349 1836 // check w/ anchor cell for multicell block
ef4d6ce8 1837 grid.GetCellSize(j, i, &c_rows, &c_cols);
39b80349 1838 if (c_rows > 0) c_rows = 0;
ef4d6ce8 1839 if (!grid.GetTable()->IsEmptyCell(j+c_rows, i))
39b80349 1840 {
ca65c044 1841 is_empty = false;
ef4d6ce8 1842 break;
39b80349 1843 }
27f35b66 1844 }
39b80349
SN
1845 if (is_empty)
1846 rect.width += grid.GetColSize(i);
dc1f566f
SN
1847 else
1848 {
1849 i--;
1850 break;
1851 }
4db6714b
KH
1852 if (rect.width >= best_width)
1853 break;
2b5f62a0 1854 }
39b80349 1855 overflowCols = i - col - cell_cols + 1;
4db6714b
KH
1856 if (overflowCols >= cols)
1857 overflowCols = cols - 1;
39b80349 1858 }
27f35b66 1859
dc1f566f
SN
1860 if (overflowCols > 0) // redraw overflow cells w/ proper hilight
1861 {
39b80349 1862 hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
2b5f62a0 1863 wxRect clip = rect;
39b80349 1864 clip.x += rectCell.width;
dc1f566f
SN
1865 // draw each overflow cell individually
1866 int col_end = col+cell_cols+overflowCols;
1867 if (col_end >= grid.GetNumberCols())
2b5f62a0 1868 col_end = grid.GetNumberCols() - 1;
dc1f566f
SN
1869 for (int i = col+cell_cols; i <= col_end; i++)
1870 {
dc1f566f
SN
1871 clip.width = grid.GetColSize(i) - 1;
1872 dc.DestroyClippingRegion();
1873 dc.SetClippingRegion(clip);
39b80349
SN
1874
1875 SetTextColoursAndFont(grid, attr, dc,
2b5f62a0 1876 grid.IsInSelection(row,i));
39b80349 1877
dc1f566f 1878 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
2b5f62a0 1879 rect, hAlign, vAlign);
39b80349 1880 clip.x += grid.GetColSize(i) - 1;
dc1f566f 1881 }
ab79958a 1882
39b80349 1883 rect = rectCell;
2b5f62a0 1884 rect.Inflate(-1);
dc1f566f 1885 rect.width++;
39b80349 1886 dc.DestroyClippingRegion();
dc1f566f 1887 }
39b80349 1888 }
ab79958a 1889
dc1f566f
SN
1890 // now we only have to draw the text
1891 SetTextColoursAndFont(grid, attr, dc, isSelected);
39b80349 1892
ab79958a
VZ
1893 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1894 rect, hAlign, vAlign);
1895}
1896
65e4e78e
VZ
1897// ----------------------------------------------------------------------------
1898// wxGridCellNumberRenderer
1899// ----------------------------------------------------------------------------
1900
fbfb8bcc 1901wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
1902{
1903 wxGridTableBase *table = grid.GetTable();
1904 wxString text;
1905 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1906 {
1907 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1908 }
9c4ba614
VZ
1909 else
1910 {
1911 text = table->GetValue(row, col);
1912 }
65e4e78e
VZ
1913
1914 return text;
1915}
1916
816be743
VZ
1917void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1918 wxGridCellAttr& attr,
1919 wxDC& dc,
1920 const wxRect& rectCell,
1921 int row, int col,
1922 bool isSelected)
1923{
1924 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1925
1926 SetTextColoursAndFont(grid, attr, dc, isSelected);
1927
1928 // draw the text right aligned by default
1929 int hAlign, vAlign;
1930 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 1931 hAlign = wxALIGN_RIGHT;
816be743
VZ
1932
1933 wxRect rect = rectCell;
1934 rect.Inflate(-1);
1935
65e4e78e
VZ
1936 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1937}
816be743 1938
65e4e78e
VZ
1939wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1940 wxGridCellAttr& attr,
1941 wxDC& dc,
1942 int row, int col)
1943{
1944 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1945}
1946
1947// ----------------------------------------------------------------------------
1948// wxGridCellFloatRenderer
1949// ----------------------------------------------------------------------------
1950
1951wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1952{
1953 SetWidth(width);
1954 SetPrecision(precision);
1955}
1956
e72b4213
VZ
1957wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1958{
1959 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1960 renderer->m_width = m_width;
1961 renderer->m_precision = m_precision;
1962 renderer->m_format = m_format;
1963
1964 return renderer;
1965}
1966
fbfb8bcc 1967wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
1968{
1969 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
1970
1971 bool hasDouble;
1972 double val;
65e4e78e
VZ
1973 wxString text;
1974 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1975 {
0b190b0f 1976 val = table->GetValueAsDouble(row, col);
ca65c044 1977 hasDouble = true;
65e4e78e 1978 }
9c4ba614
VZ
1979 else
1980 {
1981 text = table->GetValue(row, col);
0b190b0f 1982 hasDouble = text.ToDouble(&val);
9c4ba614 1983 }
65e4e78e 1984
0b190b0f
VZ
1985 if ( hasDouble )
1986 {
1987 if ( !m_format )
1988 {
1989 if ( m_width == -1 )
1990 {
19d7140e
VZ
1991 if ( m_precision == -1 )
1992 {
2b5f62a0
VZ
1993 // default width/precision
1994 m_format = _T("%f");
1995 }
19d7140e
VZ
1996 else
1997 {
1998 m_format.Printf(_T("%%.%df"), m_precision);
1999 }
0b190b0f
VZ
2000 }
2001 else if ( m_precision == -1 )
2002 {
2003 // default precision
2004 m_format.Printf(_T("%%%d.f"), m_width);
2005 }
2006 else
2007 {
2008 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
2009 }
2010 }
2011
2012 text.Printf(m_format, val);
19d7140e 2013
0b190b0f
VZ
2014 }
2015 //else: text already contains the string
2016
65e4e78e
VZ
2017 return text;
2018}
2019
816be743
VZ
2020void wxGridCellFloatRenderer::Draw(wxGrid& grid,
2021 wxGridCellAttr& attr,
2022 wxDC& dc,
2023 const wxRect& rectCell,
2024 int row, int col,
2025 bool isSelected)
2026{
2027 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
2028
2029 SetTextColoursAndFont(grid, attr, dc, isSelected);
2030
2031 // draw the text right aligned by default
2032 int hAlign, vAlign;
2033 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 2034 hAlign = wxALIGN_RIGHT;
816be743
VZ
2035
2036 wxRect rect = rectCell;
2037 rect.Inflate(-1);
2038
65e4e78e
VZ
2039 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
2040}
816be743 2041
65e4e78e
VZ
2042wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
2043 wxGridCellAttr& attr,
2044 wxDC& dc,
2045 int row, int col)
2046{
2047 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
2048}
2049
0b190b0f
VZ
2050void wxGridCellFloatRenderer::SetParameters(const wxString& params)
2051{
0b190b0f
VZ
2052 if ( !params )
2053 {
2054 // reset to defaults
2055 SetWidth(-1);
2056 SetPrecision(-1);
2057 }
2058 else
2059 {
2060 wxString tmp = params.BeforeFirst(_T(','));
ec157c8f 2061 if ( !tmp.empty() )
0b190b0f
VZ
2062 {
2063 long width;
19d7140e 2064 if ( tmp.ToLong(&width) )
0b190b0f 2065 {
19d7140e 2066 SetWidth((int)width);
0b190b0f
VZ
2067 }
2068 else
2069 {
19d7140e
VZ
2070 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
2071 }
19d7140e 2072 }
7448de8d
WS
2073 tmp = params.AfterFirst(_T(','));
2074 if ( !tmp.empty() )
2075 {
2076 long precision;
19d7140e 2077 if ( tmp.ToLong(&precision) )
7448de8d 2078 {
19d7140e 2079 SetPrecision((int)precision);
7448de8d
WS
2080 }
2081 else
2082 {
19d7140e 2083 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
7448de8d 2084 }
0b190b0f
VZ
2085 }
2086 }
2087}
2088
19d7140e 2089
508011ce
VZ
2090// ----------------------------------------------------------------------------
2091// wxGridCellBoolRenderer
2092// ----------------------------------------------------------------------------
2093
65e4e78e 2094wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 2095
b94ae1ea
VZ
2096// FIXME these checkbox size calculations are really ugly...
2097
65e4e78e 2098// between checkmark and box
a95e38c0 2099static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 2100
65e4e78e
VZ
2101wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
2102 wxGridCellAttr& WXUNUSED(attr),
2103 wxDC& WXUNUSED(dc),
2104 int WXUNUSED(row),
2105 int WXUNUSED(col))
2106{
2107 // compute it only once (no locks for MT safeness in GUI thread...)
2108 if ( !ms_sizeCheckMark.x )
297da4ba 2109 {
65e4e78e 2110 // get checkbox size
ca65c044 2111 wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString);
297da4ba 2112 wxSize size = checkbox->GetBestSize();
999836aa 2113 wxCoord checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
297da4ba 2114
65e4e78e 2115 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
69d8f612 2116#if defined(__WXGTK__) || defined(__WXMOTIF__)
65e4e78e 2117 checkSize -= size.y / 2;
297da4ba
VZ
2118#endif
2119
2120 delete checkbox;
65e4e78e
VZ
2121
2122 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
2123 }
2124
65e4e78e
VZ
2125 return ms_sizeCheckMark;
2126}
2127
2128void wxGridCellBoolRenderer::Draw(wxGrid& grid,
2129 wxGridCellAttr& attr,
2130 wxDC& dc,
2131 const wxRect& rect,
2132 int row, int col,
2133 bool isSelected)
2134{
2135 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
2136
297da4ba 2137 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 2138 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
2139
2140 // don't draw outside the cell
2141 wxCoord minSize = wxMin(rect.width, rect.height);
2142 if ( size.x >= minSize || size.y >= minSize )
2143 {
2144 // and even leave (at least) 1 pixel margin
2145 size.x = size.y = minSize - 2;
2146 }
2147
2148 // draw a border around checkmark
1bd71df9
JS
2149 int vAlign, hAlign;
2150 attr.GetAlignment(& hAlign, &vAlign);
52d6f640 2151
a95e38c0 2152 wxRect rectBorder;
1bd71df9
JS
2153 if (hAlign == wxALIGN_CENTRE)
2154 {
2155 rectBorder.x = rect.x + rect.width/2 - size.x/2;
2156 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2157 rectBorder.width = size.x;
2158 rectBorder.height = size.y;
2159 }
2160 else if (hAlign == wxALIGN_LEFT)
2161 {
2162 rectBorder.x = rect.x + 2;
2163 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2164 rectBorder.width = size.x;
52d6f640 2165 rectBorder.height = size.y;
1bd71df9
JS
2166 }
2167 else if (hAlign == wxALIGN_RIGHT)
2168 {
2169 rectBorder.x = rect.x + rect.width - size.x - 2;
2170 rectBorder.y = rect.y + rect.height/2 - size.y/2;
2171 rectBorder.width = size.x;
52d6f640 2172 rectBorder.height = size.y;
1bd71df9 2173 }
b94ae1ea 2174
f2d76237 2175 bool value;
b94ae1ea 2176 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
f2d76237
RD
2177 value = grid.GetTable()->GetValueAsBool(row, col);
2178 else
695a3263
MB
2179 {
2180 wxString cellval( grid.GetTable()->GetValue(row, col) );
8dd8f875 2181 value = !( !cellval || (cellval == wxT("0")) );
695a3263 2182 }
f2d76237
RD
2183
2184 if ( value )
508011ce 2185 {
a95e38c0
VZ
2186 wxRect rectMark = rectBorder;
2187#ifdef __WXMSW__
2188 // MSW DrawCheckMark() is weird (and should probably be changed...)
2189 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
2190 rectMark.x++;
2191 rectMark.y++;
2192#else // !MSW
2193 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
2194#endif // MSW/!MSW
2195
508011ce
VZ
2196 dc.SetTextForeground(attr.GetTextColour());
2197 dc.DrawCheckMark(rectMark);
2198 }
a95e38c0
VZ
2199
2200 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2201 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
2202 dc.DrawRectangle(rectBorder);
508011ce
VZ
2203}
2204
2796cce3
RD
2205// ----------------------------------------------------------------------------
2206// wxGridCellAttr
2207// ----------------------------------------------------------------------------
2208
1df4050d
VZ
2209void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
2210{
2211 m_nRef = 1;
2212
2213 m_isReadOnly = Unset;
2214
2215 m_renderer = NULL;
2216 m_editor = NULL;
2217
2218 m_attrkind = wxGridCellAttr::Cell;
2219
27f35b66 2220 m_sizeRows = m_sizeCols = 1;
b63fce94 2221 m_overflow = UnsetOverflow;
27f35b66 2222
1df4050d
VZ
2223 SetDefAttr(attrDefault);
2224}
2225
39bcce60 2226wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246 2227{
1df4050d
VZ
2228 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
2229
a68c1246
VZ
2230 if ( HasTextColour() )
2231 attr->SetTextColour(GetTextColour());
2232 if ( HasBackgroundColour() )
2233 attr->SetBackgroundColour(GetBackgroundColour());
2234 if ( HasFont() )
2235 attr->SetFont(GetFont());
2236 if ( HasAlignment() )
2237 attr->SetAlignment(m_hAlign, m_vAlign);
2238
27f35b66
SN
2239 attr->SetSize( m_sizeRows, m_sizeCols );
2240
a68c1246
VZ
2241 if ( m_renderer )
2242 {
2243 attr->SetRenderer(m_renderer);
39bcce60 2244 m_renderer->IncRef();
a68c1246
VZ
2245 }
2246 if ( m_editor )
2247 {
2248 attr->SetEditor(m_editor);
39bcce60 2249 m_editor->IncRef();
a68c1246
VZ
2250 }
2251
2252 if ( IsReadOnly() )
2253 attr->SetReadOnly();
2254
19d7140e
VZ
2255 attr->SetKind( m_attrkind );
2256
a68c1246
VZ
2257 return attr;
2258}
2259
19d7140e
VZ
2260void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
2261{
2262 if ( !HasTextColour() && mergefrom->HasTextColour() )
2263 SetTextColour(mergefrom->GetTextColour());
2264 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
2265 SetBackgroundColour(mergefrom->GetBackgroundColour());
2266 if ( !HasFont() && mergefrom->HasFont() )
2267 SetFont(mergefrom->GetFont());
4db6714b
KH
2268 if ( !HasAlignment() && mergefrom->HasAlignment() )
2269 {
19d7140e
VZ
2270 int hAlign, vAlign;
2271 mergefrom->GetAlignment( &hAlign, &vAlign);
2272 SetAlignment(hAlign, vAlign);
2273 }
3100c3db
RD
2274 if ( !HasSize() && mergefrom->HasSize() )
2275 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
27f35b66 2276
19d7140e
VZ
2277 // Directly access member functions as GetRender/Editor don't just return
2278 // m_renderer/m_editor
2279 //
2280 // Maybe add support for merge of Render and Editor?
2281 if (!HasRenderer() && mergefrom->HasRenderer() )
bf7945ce 2282 {
19d7140e
VZ
2283 m_renderer = mergefrom->m_renderer;
2284 m_renderer->IncRef();
2285 }
2286 if ( !HasEditor() && mergefrom->HasEditor() )
2287 {
2288 m_editor = mergefrom->m_editor;
2289 m_editor->IncRef();
2290 }
2291 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
2292 SetReadOnly(mergefrom->IsReadOnly());
2293
ef5df12b 2294 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
ff699386 2295 SetOverflow(mergefrom->GetOverflow());
ef5df12b 2296
19d7140e
VZ
2297 SetDefAttr(mergefrom->m_defGridAttr);
2298}
2299
27f35b66
SN
2300void wxGridCellAttr::SetSize(int num_rows, int num_cols)
2301{
2302 // The size of a cell is normally 1,1
2303
2304 // If this cell is larger (2,2) then this is the top left cell
2305 // the other cells that will be covered (lower right cells) must be
2306 // set to negative or zero values such that
2307 // row + num_rows of the covered cell points to the larger cell (this cell)
2308 // same goes for the col + num_cols.
2309
2310 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2311
2312 wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) ||
2313 !((num_rows<=0)&&(num_cols>0)) ||
2314 !((num_rows==0)&&(num_cols==0))),
2315 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2316
2317 m_sizeRows = num_rows;
2318 m_sizeCols = num_cols;
2319}
2320
2796cce3
RD
2321const wxColour& wxGridCellAttr::GetTextColour() const
2322{
2323 if (HasTextColour())
508011ce 2324 {
2796cce3 2325 return m_colText;
508011ce 2326 }
0926b2fc 2327 else if (m_defGridAttr && m_defGridAttr != this)
508011ce 2328 {
2796cce3 2329 return m_defGridAttr->GetTextColour();
508011ce
VZ
2330 }
2331 else
2332 {
2796cce3
RD
2333 wxFAIL_MSG(wxT("Missing default cell attribute"));
2334 return wxNullColour;
2335 }
2336}
2337
2338
2339const wxColour& wxGridCellAttr::GetBackgroundColour() const
2340{
2341 if (HasBackgroundColour())
2342 return m_colBack;
0926b2fc 2343 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2344 return m_defGridAttr->GetBackgroundColour();
508011ce
VZ
2345 else
2346 {
2796cce3
RD
2347 wxFAIL_MSG(wxT("Missing default cell attribute"));
2348 return wxNullColour;
2349 }
2350}
2351
2352
2353const wxFont& wxGridCellAttr::GetFont() const
2354{
2355 if (HasFont())
2356 return m_font;
0926b2fc 2357 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2358 return m_defGridAttr->GetFont();
508011ce
VZ
2359 else
2360 {
2796cce3
RD
2361 wxFAIL_MSG(wxT("Missing default cell attribute"));
2362 return wxNullFont;
2363 }
2364}
2365
2366
2367void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
2368{
508011ce
VZ
2369 if (HasAlignment())
2370 {
4db6714b
KH
2371 if ( hAlign )
2372 *hAlign = m_hAlign;
2373 if ( vAlign )
2374 *vAlign = m_vAlign;
2796cce3 2375 }
0926b2fc 2376 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2377 m_defGridAttr->GetAlignment(hAlign, vAlign);
508011ce
VZ
2378 else
2379 {
2796cce3
RD
2380 wxFAIL_MSG(wxT("Missing default cell attribute"));
2381 }
2382}
2383
27f35b66
SN
2384void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
2385{
4db6714b
KH
2386 if ( num_rows )
2387 *num_rows = m_sizeRows;
2388 if ( num_cols )
2389 *num_cols = m_sizeCols;
27f35b66 2390}
2796cce3 2391
f2d76237 2392// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
2393// which attribute to use. If a non-default attr object has one then it is
2394// used, otherwise the default editor or renderer is fetched from the grid and
2395// used. It should be the default for the data type of the cell. If it is
2396// NULL (because the table has a type that the grid does not have in its
2397// registry,) then the grid's default editor or renderer is used.
2398
2399wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
2400{
3cf883a2 2401 wxGridCellRenderer *renderer;
28a77bc4 2402
3cf883a2 2403 if ( m_renderer && this != m_defGridAttr )
0b190b0f 2404 {
3cf883a2
VZ
2405 // use the cells renderer if it has one
2406 renderer = m_renderer;
2407 renderer->IncRef();
0b190b0f 2408 }
3cf883a2 2409 else // no non default cell renderer
0b190b0f 2410 {
3cf883a2
VZ
2411 // get default renderer for the data type
2412 if ( grid )
2413 {
2414 // GetDefaultRendererForCell() will do IncRef() for us
2415 renderer = grid->GetDefaultRendererForCell(row, col);
2416 }
2417 else
2418 {
2419 renderer = NULL;
2420 }
0b190b0f 2421
3cf883a2
VZ
2422 if ( !renderer )
2423 {
0926b2fc 2424 if (m_defGridAttr && this != m_defGridAttr )
3cf883a2
VZ
2425 {
2426 // if we still don't have one then use the grid default
2427 // (no need for IncRef() here neither)
2428 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
2429 }
2430 else // default grid attr
2431 {
2432 // use m_renderer which we had decided not to use initially
2433 renderer = m_renderer;
2434 if ( renderer )
2435 renderer->IncRef();
2436 }
2437 }
0b190b0f 2438 }
28a77bc4 2439
3cf883a2
VZ
2440 // we're supposed to always find something
2441 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
28a77bc4
RD
2442
2443 return renderer;
2796cce3
RD
2444}
2445
3cf883a2 2446// same as above, except for s/renderer/editor/g
28a77bc4 2447wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
07296f0b 2448{
3cf883a2 2449 wxGridCellEditor *editor;
0b190b0f 2450
3cf883a2 2451 if ( m_editor && this != m_defGridAttr )
0b190b0f 2452 {
3cf883a2
VZ
2453 // use the cells editor if it has one
2454 editor = m_editor;
2455 editor->IncRef();
0b190b0f 2456 }
3cf883a2 2457 else // no non default cell editor
0b190b0f 2458 {
3cf883a2
VZ
2459 // get default editor for the data type
2460 if ( grid )
2461 {
2462 // GetDefaultEditorForCell() will do IncRef() for us
2463 editor = grid->GetDefaultEditorForCell(row, col);
2464 }
2465 else
2466 {
2467 editor = NULL;
2468 }
2469
2470 if ( !editor )
2471 {
0926b2fc 2472 if ( m_defGridAttr && this != m_defGridAttr )
3cf883a2
VZ
2473 {
2474 // if we still don't have one then use the grid default
2475 // (no need for IncRef() here neither)
2476 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
2477 }
2478 else // default grid attr
2479 {
2480 // use m_editor which we had decided not to use initially
2481 editor = m_editor;
2482 if ( editor )
2483 editor->IncRef();
2484 }
2485 }
0b190b0f 2486 }
28a77bc4 2487
3cf883a2
VZ
2488 // we're supposed to always find something
2489 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
2490
28a77bc4 2491 return editor;
07296f0b
RD
2492}
2493
b99be8fb 2494// ----------------------------------------------------------------------------
758cbedf 2495// wxGridCellAttrData
b99be8fb
VZ
2496// ----------------------------------------------------------------------------
2497
758cbedf 2498void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb
VZ
2499{
2500 int n = FindIndex(row, col);
2501 if ( n == wxNOT_FOUND )
2502 {
2503 // add the attribute
2504 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
2505 }
2506 else
2507 {
6f36917b
VZ
2508 // free the old attribute
2509 m_attrs[(size_t)n].attr->DecRef();
2510
b99be8fb
VZ
2511 if ( attr )
2512 {
2513 // change the attribute
2e9a6788 2514 m_attrs[(size_t)n].attr = attr;
b99be8fb
VZ
2515 }
2516 else
2517 {
2518 // remove this attribute
2519 m_attrs.RemoveAt((size_t)n);
2520 }
2521 }
b99be8fb
VZ
2522}
2523
758cbedf 2524wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb
VZ
2525{
2526 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2527
2528 int n = FindIndex(row, col);
2529 if ( n != wxNOT_FOUND )
2530 {
2e9a6788
VZ
2531 attr = m_attrs[(size_t)n].attr;
2532 attr->IncRef();
b99be8fb
VZ
2533 }
2534
2535 return attr;
2536}
2537
4d60017a
SN
2538void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
2539{
2540 size_t count = m_attrs.GetCount();
2541 for ( size_t n = 0; n < count; n++ )
2542 {
2543 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2544 wxCoord row = coords.GetRow();
2545 if ((size_t)row >= pos)
2546 {
2547 if (numRows > 0)
2548 {
2549 // If rows inserted, include row counter where necessary
2550 coords.SetRow(row + numRows);
2551 }
2552 else if (numRows < 0)
2553 {
2554 // If rows deleted ...
2555 if ((size_t)row >= pos - numRows)
2556 {
2557 // ...either decrement row counter (if row still exists)...
2558 coords.SetRow(row + numRows);
2559 }
2560 else
2561 {
2562 // ...or remove the attribute
d3e9dd94
SN
2563 // No need to DecRef the attribute itself since this is
2564 // done be wxGridCellWithAttr's destructor!
01dd42b6 2565 m_attrs.RemoveAt(n);
4db6714b
KH
2566 n--;
2567 count--;
d1c0b4f9
VZ
2568 }
2569 }
4d60017a
SN
2570 }
2571 }
2572}
2573
2574void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
2575{
2576 size_t count = m_attrs.GetCount();
2577 for ( size_t n = 0; n < count; n++ )
2578 {
2579 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2580 wxCoord col = coords.GetCol();
2581 if ( (size_t)col >= pos )
2582 {
2583 if ( numCols > 0 )
2584 {
2585 // If rows inserted, include row counter where necessary
2586 coords.SetCol(col + numCols);
2587 }
2588 else if (numCols < 0)
2589 {
2590 // If rows deleted ...
2591 if ((size_t)col >= pos - numCols)
2592 {
2593 // ...either decrement row counter (if row still exists)...
2594 coords.SetCol(col + numCols);
2595 }
2596 else
2597 {
2598 // ...or remove the attribute
d3e9dd94
SN
2599 // No need to DecRef the attribute itself since this is
2600 // done be wxGridCellWithAttr's destructor!
01dd42b6 2601 m_attrs.RemoveAt(n);
d1c0b4f9
VZ
2602 n--; count--;
2603 }
2604 }
4d60017a
SN
2605 }
2606 }
2607}
2608
758cbedf 2609int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
2610{
2611 size_t count = m_attrs.GetCount();
2612 for ( size_t n = 0; n < count; n++ )
2613 {
2614 const wxGridCellCoords& coords = m_attrs[n].coords;
2615 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
2616 {
2617 return n;
2618 }
2619 }
2620
2621 return wxNOT_FOUND;
2622}
2623
758cbedf
VZ
2624// ----------------------------------------------------------------------------
2625// wxGridRowOrColAttrData
2626// ----------------------------------------------------------------------------
2627
2628wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2629{
2630 size_t count = m_attrs.Count();
2631 for ( size_t n = 0; n < count; n++ )
2632 {
2633 m_attrs[n]->DecRef();
2634 }
2635}
2636
2637wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
2638{
2639 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2640
2641 int n = m_rowsOrCols.Index(rowOrCol);
2642 if ( n != wxNOT_FOUND )
2643 {
2644 attr = m_attrs[(size_t)n];
2645 attr->IncRef();
2646 }
2647
2648 return attr;
2649}
2650
2651void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
2652{
a95e38c0
VZ
2653 int i = m_rowsOrCols.Index(rowOrCol);
2654 if ( i == wxNOT_FOUND )
758cbedf
VZ
2655 {
2656 // add the attribute
2657 m_rowsOrCols.Add(rowOrCol);
2658 m_attrs.Add(attr);
2659 }
2660 else
2661 {
a95e38c0 2662 size_t n = (size_t)i;
758cbedf
VZ
2663 if ( attr )
2664 {
2665 // change the attribute
a95e38c0
VZ
2666 m_attrs[n]->DecRef();
2667 m_attrs[n] = attr;
758cbedf
VZ
2668 }
2669 else
2670 {
2671 // remove this attribute
a95e38c0
VZ
2672 m_attrs[n]->DecRef();
2673 m_rowsOrCols.RemoveAt(n);
2674 m_attrs.RemoveAt(n);
758cbedf
VZ
2675 }
2676 }
2677}
2678
4d60017a
SN
2679void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
2680{
2681 size_t count = m_attrs.GetCount();
2682 for ( size_t n = 0; n < count; n++ )
2683 {
2684 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
2685 if ( (size_t)rowOrCol >= pos )
2686 {
2687 if ( numRowsOrCols > 0 )
2688 {
2689 // If rows inserted, include row counter where necessary
2690 rowOrCol += numRowsOrCols;
2691 }
2692 else if ( numRowsOrCols < 0)
2693 {
2694 // If rows deleted, either decrement row counter (if row still exists)
2695 if ((size_t)rowOrCol >= pos - numRowsOrCols)
2696 rowOrCol += numRowsOrCols;
2697 else
2698 {
01dd42b6
VZ
2699 m_rowsOrCols.RemoveAt(n);
2700 m_attrs[n]->DecRef();
2701 m_attrs.RemoveAt(n);
4db6714b
KH
2702 n--;
2703 count--;
d1c0b4f9
VZ
2704 }
2705 }
4d60017a
SN
2706 }
2707 }
2708}
2709
b99be8fb
VZ
2710// ----------------------------------------------------------------------------
2711// wxGridCellAttrProvider
2712// ----------------------------------------------------------------------------
2713
2714wxGridCellAttrProvider::wxGridCellAttrProvider()
2715{
2716 m_data = (wxGridCellAttrProviderData *)NULL;
2717}
2718
2719wxGridCellAttrProvider::~wxGridCellAttrProvider()
2720{
2721 delete m_data;
2722}
2723
2724void wxGridCellAttrProvider::InitData()
2725{
2726 m_data = new wxGridCellAttrProviderData;
2727}
2728
19d7140e
VZ
2729wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
2730 wxGridCellAttr::wxAttrKind kind ) const
b99be8fb 2731{
758cbedf
VZ
2732 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2733 if ( m_data )
2734 {
962a48f6 2735 switch (kind)
758cbedf 2736 {
19d7140e 2737 case (wxGridCellAttr::Any):
962a48f6
DS
2738 // Get cached merge attributes.
2739 // Currently not used as no cache implemented as not mutable
19d7140e 2740 // attr = m_data->m_mergeAttr.GetAttr(row, col);
4db6714b 2741 if (!attr)
19d7140e 2742 {
962a48f6
DS
2743 // Basically implement old version.
2744 // Also check merge cache, so we don't have to re-merge every time..
999836aa
VZ
2745 wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col);
2746 wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row);
2747 wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col);
19d7140e 2748
4db6714b
KH
2749 if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol))
2750 {
2d0c2e79 2751 // Two or more are non NULL
19d7140e
VZ
2752 attr = new wxGridCellAttr;
2753 attr->SetKind(wxGridCellAttr::Merged);
2754
962a48f6 2755 // Order is important..
4db6714b
KH
2756 if (attrcell)
2757 {
19d7140e
VZ
2758 attr->MergeWith(attrcell);
2759 attrcell->DecRef();
2760 }
4db6714b
KH
2761 if (attrcol)
2762 {
19d7140e
VZ
2763 attr->MergeWith(attrcol);
2764 attrcol->DecRef();
2765 }
4db6714b
KH
2766 if (attrrow)
2767 {
19d7140e
VZ
2768 attr->MergeWith(attrrow);
2769 attrrow->DecRef();
2770 }
962a48f6
DS
2771
2772 // store merge attr if cache implemented
19d7140e
VZ
2773 //attr->IncRef();
2774 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2775 }
2776 else
2d0c2e79 2777 {
19d7140e 2778 // one or none is non null return it or null.
4db6714b
KH
2779 if (attrrow)
2780 attr = attrrow;
2781 if (attrcol)
2d0c2e79 2782 {
962a48f6 2783 if (attr)
2d0c2e79
RD
2784 attr->DecRef();
2785 attr = attrcol;
2786 }
4db6714b 2787 if (attrcell)
2d0c2e79 2788 {
4db6714b 2789 if (attr)
2d0c2e79
RD
2790 attr->DecRef();
2791 attr = attrcell;
2792 }
19d7140e
VZ
2793 }
2794 }
2795 break;
4db6714b 2796
19d7140e
VZ
2797 case (wxGridCellAttr::Cell):
2798 attr = m_data->m_cellAttrs.GetAttr(row, col);
2799 break;
4db6714b 2800
19d7140e 2801 case (wxGridCellAttr::Col):
2d0c2e79 2802 attr = m_data->m_colAttrs.GetAttr(col);
19d7140e 2803 break;
4db6714b 2804
19d7140e 2805 case (wxGridCellAttr::Row):
2d0c2e79 2806 attr = m_data->m_rowAttrs.GetAttr(row);
19d7140e 2807 break;
4db6714b 2808
19d7140e
VZ
2809 default:
2810 // unused as yet...
2811 // (wxGridCellAttr::Default):
2812 // (wxGridCellAttr::Merged):
2813 break;
758cbedf
VZ
2814 }
2815 }
758cbedf 2816 return attr;
b99be8fb
VZ
2817}
2818
2e9a6788 2819void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
2820 int row, int col)
2821{
2822 if ( !m_data )
2823 InitData();
2824
758cbedf
VZ
2825 m_data->m_cellAttrs.SetAttr(attr, row, col);
2826}
2827
2828void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
2829{
2830 if ( !m_data )
2831 InitData();
2832
2833 m_data->m_rowAttrs.SetAttr(attr, row);
2834}
2835
2836void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
2837{
2838 if ( !m_data )
2839 InitData();
2840
2841 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
2842}
2843
4d60017a
SN
2844void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
2845{
2846 if ( m_data )
2847 {
2848 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2849
d1c0b4f9 2850 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
2851 }
2852}
2853
2854void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
2855{
2856 if ( m_data )
2857 {
2858 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2859
d1c0b4f9 2860 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
2861 }
2862}
2863
f2d76237
RD
2864// ----------------------------------------------------------------------------
2865// wxGridTypeRegistry
2866// ----------------------------------------------------------------------------
2867
2868wxGridTypeRegistry::~wxGridTypeRegistry()
2869{
b94ae1ea
VZ
2870 size_t count = m_typeinfo.Count();
2871 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
2872 delete m_typeinfo[i];
2873}
2874
2875
2876void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2877 wxGridCellRenderer* renderer,
2878 wxGridCellEditor* editor)
2879{
f2d76237
RD
2880 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2881
2882 // is it already registered?
c4608a8a 2883 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
2884 if ( loc != wxNOT_FOUND )
2885 {
f2d76237
RD
2886 delete m_typeinfo[loc];
2887 m_typeinfo[loc] = info;
2888 }
39bcce60
VZ
2889 else
2890 {
f2d76237
RD
2891 m_typeinfo.Add(info);
2892 }
2893}
2894
c4608a8a
VZ
2895int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2896{
2897 size_t count = m_typeinfo.GetCount();
2898 for ( size_t i = 0; i < count; i++ )
2899 {
2900 if ( typeName == m_typeinfo[i]->m_typeName )
2901 {
2902 return i;
2903 }
2904 }
2905
2906 return wxNOT_FOUND;
2907}
2908
f2d76237
RD
2909int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2910{
c4608a8a
VZ
2911 int index = FindRegisteredDataType(typeName);
2912 if ( index == wxNOT_FOUND )
2913 {
2914 // check whether this is one of the standard ones, in which case
2915 // register it "on the fly"
3a8c693a 2916#if wxUSE_TEXTCTRL
c4608a8a
VZ
2917 if ( typeName == wxGRID_VALUE_STRING )
2918 {
2919 RegisterDataType(wxGRID_VALUE_STRING,
2920 new wxGridCellStringRenderer,
2921 new wxGridCellTextEditor);
4db6714b
KH
2922 }
2923 else
3a8c693a
VZ
2924#endif // wxUSE_TEXTCTRL
2925#if wxUSE_CHECKBOX
2926 if ( typeName == wxGRID_VALUE_BOOL )
c4608a8a
VZ
2927 {
2928 RegisterDataType(wxGRID_VALUE_BOOL,
2929 new wxGridCellBoolRenderer,
2930 new wxGridCellBoolEditor);
4db6714b
KH
2931 }
2932 else
3a8c693a
VZ
2933#endif // wxUSE_CHECKBOX
2934#if wxUSE_TEXTCTRL
2935 if ( typeName == wxGRID_VALUE_NUMBER )
c4608a8a
VZ
2936 {
2937 RegisterDataType(wxGRID_VALUE_NUMBER,
2938 new wxGridCellNumberRenderer,
2939 new wxGridCellNumberEditor);
2940 }
2941 else if ( typeName == wxGRID_VALUE_FLOAT )
2942 {
2943 RegisterDataType(wxGRID_VALUE_FLOAT,
2944 new wxGridCellFloatRenderer,
2945 new wxGridCellFloatEditor);
4db6714b
KH
2946 }
2947 else
3a8c693a
VZ
2948#endif // wxUSE_TEXTCTRL
2949#if wxUSE_COMBOBOX
2950 if ( typeName == wxGRID_VALUE_CHOICE )
c4608a8a
VZ
2951 {
2952 RegisterDataType(wxGRID_VALUE_CHOICE,
2953 new wxGridCellStringRenderer,
2954 new wxGridCellChoiceEditor);
4db6714b
KH
2955 }
2956 else
3a8c693a 2957#endif // wxUSE_COMBOBOX
c4608a8a
VZ
2958 {
2959 return wxNOT_FOUND;
2960 }
f2d76237 2961
c4608a8a
VZ
2962 // we get here only if just added the entry for this type, so return
2963 // the last index
2964 index = m_typeinfo.GetCount() - 1;
2965 }
2966
2967 return index;
2968}
2969
2970int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
2971{
2972 int index = FindDataType(typeName);
2973 if ( index == wxNOT_FOUND )
2974 {
2975 // the first part of the typename is the "real" type, anything after ':'
2976 // are the parameters for the renderer
2977 index = FindDataType(typeName.BeforeFirst(_T(':')));
2978 if ( index == wxNOT_FOUND )
2979 {
2980 return wxNOT_FOUND;
f2d76237 2981 }
c4608a8a
VZ
2982
2983 wxGridCellRenderer *renderer = GetRenderer(index);
2984 wxGridCellRenderer *rendererOld = renderer;
2985 renderer = renderer->Clone();
2986 rendererOld->DecRef();
2987
2988 wxGridCellEditor *editor = GetEditor(index);
2989 wxGridCellEditor *editorOld = editor;
2990 editor = editor->Clone();
2991 editorOld->DecRef();
2992
2993 // do it even if there are no parameters to reset them to defaults
2994 wxString params = typeName.AfterFirst(_T(':'));
2995 renderer->SetParameters(params);
2996 editor->SetParameters(params);
2997
2998 // register the new typename
c4608a8a
VZ
2999 RegisterDataType(typeName, renderer, editor);
3000
3001 // we just registered it, it's the last one
3002 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
3003 }
3004
c4608a8a 3005 return index;
f2d76237
RD
3006}
3007
3008wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
3009{
3010 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
faec5a43
SN
3011 if (renderer)
3012 renderer->IncRef();
f2d76237
RD
3013 return renderer;
3014}
3015
0b190b0f 3016wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
3017{
3018 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
faec5a43
SN
3019 if (editor)
3020 editor->IncRef();
f2d76237
RD
3021 return editor;
3022}
3023
758cbedf
VZ
3024// ----------------------------------------------------------------------------
3025// wxGridTableBase
3026// ----------------------------------------------------------------------------
3027
f85afd4e
MB
3028IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
3029
3030
3031wxGridTableBase::wxGridTableBase()
f85afd4e
MB
3032{
3033 m_view = (wxGrid *) NULL;
b99be8fb 3034 m_attrProvider = (wxGridCellAttrProvider *) NULL;
f85afd4e
MB
3035}
3036
3037wxGridTableBase::~wxGridTableBase()
3038{
b99be8fb
VZ
3039 delete m_attrProvider;
3040}
3041
3042void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
3043{
3044 delete m_attrProvider;
3045 m_attrProvider = attrProvider;
f85afd4e
MB
3046}
3047
f2d76237
RD
3048bool wxGridTableBase::CanHaveAttributes()
3049{
3050 if ( ! GetAttrProvider() )
3051 {
3052 // use the default attr provider by default
3053 SetAttrProvider(new wxGridCellAttrProvider);
3054 }
ca65c044 3055 return true;
f2d76237
RD
3056}
3057
19d7140e 3058wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
b99be8fb
VZ
3059{
3060 if ( m_attrProvider )
19d7140e 3061 return m_attrProvider->GetAttr(row, col, kind);
b99be8fb
VZ
3062 else
3063 return (wxGridCellAttr *)NULL;
3064}
3065
3100c3db 3066
758cbedf 3067void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
3068{
3069 if ( m_attrProvider )
3070 {
19d7140e 3071 attr->SetKind(wxGridCellAttr::Cell);
b99be8fb
VZ
3072 m_attrProvider->SetAttr(attr, row, col);
3073 }
3074 else
3075 {
3076 // as we take ownership of the pointer and don't store it, we must
3077 // free it now
39bcce60 3078 wxSafeDecRef(attr);
b99be8fb
VZ
3079 }
3080}
3081
758cbedf
VZ
3082void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
3083{
3084 if ( m_attrProvider )
3085 {
19d7140e 3086 attr->SetKind(wxGridCellAttr::Row);
758cbedf
VZ
3087 m_attrProvider->SetRowAttr(attr, row);
3088 }
3089 else
3090 {
3091 // as we take ownership of the pointer and don't store it, we must
3092 // free it now
39bcce60 3093 wxSafeDecRef(attr);
758cbedf
VZ
3094 }
3095}
3096
3097void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
3098{
3099 if ( m_attrProvider )
3100 {
19d7140e 3101 attr->SetKind(wxGridCellAttr::Col);
758cbedf
VZ
3102 m_attrProvider->SetColAttr(attr, col);
3103 }
3104 else
3105 {
3106 // as we take ownership of the pointer and don't store it, we must
3107 // free it now
39bcce60 3108 wxSafeDecRef(attr);
758cbedf
VZ
3109 }
3110}
3111
aa5e1f75
SN
3112bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
3113 size_t WXUNUSED(numRows) )
f85afd4e 3114{
f6bcfd97 3115 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
8f177c8e 3116
ca65c044 3117 return false;
f85afd4e
MB
3118}
3119
aa5e1f75 3120bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 3121{
f6bcfd97 3122 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
8f177c8e 3123
ca65c044 3124 return false;
f85afd4e
MB
3125}
3126
aa5e1f75
SN
3127bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
3128 size_t WXUNUSED(numRows) )
f85afd4e 3129{
f6bcfd97 3130 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
8f177c8e 3131
ca65c044 3132 return false;
f85afd4e
MB
3133}
3134
aa5e1f75
SN
3135bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
3136 size_t WXUNUSED(numCols) )
f85afd4e 3137{
f6bcfd97 3138 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
8f177c8e 3139
ca65c044 3140 return false;
f85afd4e
MB
3141}
3142
aa5e1f75 3143bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 3144{
f6bcfd97 3145 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
8f177c8e 3146
ca65c044 3147 return false;
f85afd4e
MB
3148}
3149
aa5e1f75
SN
3150bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
3151 size_t WXUNUSED(numCols) )
f85afd4e 3152{
f6bcfd97 3153 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
8f177c8e 3154
ca65c044 3155 return false;
f85afd4e
MB
3156}
3157
f85afd4e
MB
3158wxString wxGridTableBase::GetRowLabelValue( int row )
3159{
3160 wxString s;
f2d76237
RD
3161 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
3162 // how much it makes sense to us geeks.
f85afd4e
MB
3163 return s;
3164}
3165
3166wxString wxGridTableBase::GetColLabelValue( int col )
3167{
3168 // default col labels are:
3169 // cols 0 to 25 : A-Z
3170 // cols 26 to 675 : AA-ZZ
3171 // etc.
3172
3173 wxString s;
3174 unsigned int i, n;
3175 for ( n = 1; ; n++ )
3176 {
fef5c556 3177 s += (wxChar) (_T('A') + (wxChar)( col%26 ));
f85afd4e 3178 col = col/26 - 1;
4db6714b
KH
3179 if ( col < 0 )
3180 break;
f85afd4e
MB
3181 }
3182
3183 // reverse the string...
3184 wxString s2;
3185 for ( i = 0; i < n; i++ )
3186 {
3187 s2 += s[n-i-1];
3188 }
3189
3190 return s2;
3191}
3192
f2d76237
RD
3193wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
3194{
816be743 3195 return wxGRID_VALUE_STRING;
f2d76237
RD
3196}
3197
3198bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
3199 const wxString& typeName )
3200{
816be743 3201 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
3202}
3203
3204bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
3205{
3206 return CanGetValueAs(row, col, typeName);
3207}
3208
3209long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
3210{
3211 return 0;
3212}
3213
3214double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
3215{
3216 return 0.0;
3217}
3218
3219bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
3220{
ca65c044 3221 return false;
f2d76237
RD
3222}
3223
3224void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
3225 long WXUNUSED(value) )
3226{
3227}
3228
3229void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
3230 double WXUNUSED(value) )
3231{
3232}
3233
3234void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
3235 bool WXUNUSED(value) )
3236{
3237}
3238
3239
3240void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3241 const wxString& WXUNUSED(typeName) )
3242{
3243 return NULL;
3244}
3245
3246void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3247 const wxString& WXUNUSED(typeName),
3248 void* WXUNUSED(value) )
3249{
3250}
3251
f85afd4e
MB
3252//////////////////////////////////////////////////////////////////////
3253//
3254// Message class for the grid table to send requests and notifications
3255// to the grid view
3256//
3257
3258wxGridTableMessage::wxGridTableMessage()
3259{
3260 m_table = (wxGridTableBase *) NULL;
3261 m_id = -1;
3262 m_comInt1 = -1;
3263 m_comInt2 = -1;
3264}
3265
3266wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
3267 int commandInt1, int commandInt2 )
3268{
3269 m_table = table;
3270 m_id = id;
3271 m_comInt1 = commandInt1;
3272 m_comInt2 = commandInt2;
3273}
3274
3275
3276
3277//////////////////////////////////////////////////////////////////////
3278//
3279// A basic grid table for string data. An object of this class will
3280// created by wxGrid if you don't specify an alternative table class.
3281//
3282
223d09f6 3283WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
3284
3285IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
3286
3287wxGridStringTable::wxGridStringTable()
3288 : wxGridTableBase()
3289{
3290}
3291
3292wxGridStringTable::wxGridStringTable( int numRows, int numCols )
3293 : wxGridTableBase()
3294{
f85afd4e
MB
3295 m_data.Alloc( numRows );
3296
3297 wxArrayString sa;
3298 sa.Alloc( numCols );
27f35b66 3299 sa.Add( wxEmptyString, numCols );
8f177c8e 3300
27f35b66 3301 m_data.Add( sa, numRows );
f85afd4e
MB
3302}
3303
3304wxGridStringTable::~wxGridStringTable()
3305{
3306}
3307
e32352cf 3308int wxGridStringTable::GetNumberRows()
f85afd4e
MB
3309{
3310 return m_data.GetCount();
3311}
3312
e32352cf 3313int wxGridStringTable::GetNumberCols()
f85afd4e
MB
3314{
3315 if ( m_data.GetCount() > 0 )
3316 return m_data[0].GetCount();
3317 else
3318 return 0;
3319}
3320
3321wxString wxGridStringTable::GetValue( int row, int col )
3322{
3e13956a
RD
3323 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3324 wxEmptyString,
3325 _T("invalid row or column index in wxGridStringTable") );
af547d51 3326
f85afd4e
MB
3327 return m_data[row][col];
3328}
3329
f2d76237 3330void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 3331{
3e13956a
RD
3332 wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()),
3333 _T("invalid row or column index in wxGridStringTable") );
af547d51 3334
f2d76237 3335 m_data[row][col] = value;
f85afd4e
MB
3336}
3337
3338bool wxGridStringTable::IsEmptyCell( int row, int col )
3339{
3e13956a
RD
3340 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3341 true,
af547d51
VZ
3342 _T("invalid row or column index in wxGridStringTable") );
3343
f85afd4e
MB
3344 return (m_data[row][col] == wxEmptyString);
3345}
3346
f85afd4e
MB
3347void wxGridStringTable::Clear()
3348{
3349 int row, col;
3350 int numRows, numCols;
8f177c8e 3351
f85afd4e
MB
3352 numRows = m_data.GetCount();
3353 if ( numRows > 0 )
3354 {
3355 numCols = m_data[0].GetCount();
3356
3357 for ( row = 0; row < numRows; row++ )
3358 {
3359 for ( col = 0; col < numCols; col++ )
3360 {
3361 m_data[row][col] = wxEmptyString;
3362 }
3363 }
3364 }
3365}
3366
3367
3368bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
3369{
f85afd4e 3370 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3371 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3372 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3373
f85afd4e
MB
3374 if ( pos >= curNumRows )
3375 {
3376 return AppendRows( numRows );
3377 }
8f177c8e 3378
f85afd4e
MB
3379 wxArrayString sa;
3380 sa.Alloc( curNumCols );
27f35b66
SN
3381 sa.Add( wxEmptyString, curNumCols );
3382 m_data.Insert( sa, pos, numRows );
f85afd4e
MB
3383 if ( GetView() )
3384 {
3385 wxGridTableMessage msg( this,
3386 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
3387 pos,
3388 numRows );
8f177c8e 3389
f85afd4e
MB
3390 GetView()->ProcessTableMessage( msg );
3391 }
3392
ca65c044 3393 return true;
f85afd4e
MB
3394}
3395
3396bool wxGridStringTable::AppendRows( size_t numRows )
3397{
f85afd4e 3398 size_t curNumRows = m_data.GetCount();
4db6714b
KH
3399 size_t curNumCols = ( curNumRows > 0
3400 ? m_data[0].GetCount()
3401 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3402
f85afd4e
MB
3403 wxArrayString sa;
3404 if ( curNumCols > 0 )
3405 {
3406 sa.Alloc( curNumCols );
27f35b66 3407 sa.Add( wxEmptyString, curNumCols );
f85afd4e 3408 }
8f177c8e 3409
27f35b66 3410 m_data.Add( sa, numRows );
f85afd4e
MB
3411
3412 if ( GetView() )
3413 {
3414 wxGridTableMessage msg( this,
3415 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
3416 numRows );
8f177c8e 3417
f85afd4e
MB
3418 GetView()->ProcessTableMessage( msg );
3419 }
3420
ca65c044 3421 return true;
f85afd4e
MB
3422}
3423
3424bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
3425{
f85afd4e 3426 size_t curNumRows = m_data.GetCount();
8f177c8e 3427
f85afd4e
MB
3428 if ( pos >= curNumRows )
3429 {
e91d2033
VZ
3430 wxFAIL_MSG( wxString::Format
3431 (
3432 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3433 (unsigned long)pos,
3434 (unsigned long)numRows,
3435 (unsigned long)curNumRows
3436 ) );
3437
ca65c044 3438 return false;
f85afd4e
MB
3439 }
3440
3441 if ( numRows > curNumRows - pos )
3442 {
3443 numRows = curNumRows - pos;
3444 }
8f177c8e 3445
f85afd4e
MB
3446 if ( numRows >= curNumRows )
3447 {
d57ad377 3448 m_data.Clear();
f85afd4e
MB
3449 }
3450 else
3451 {
27f35b66 3452 m_data.RemoveAt( pos, numRows );
f85afd4e 3453 }
4db6714b 3454
f85afd4e
MB
3455 if ( GetView() )
3456 {
3457 wxGridTableMessage msg( this,
3458 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
3459 pos,
3460 numRows );
8f177c8e 3461
f85afd4e
MB
3462 GetView()->ProcessTableMessage( msg );
3463 }
3464
ca65c044 3465 return true;
f85afd4e
MB
3466}
3467
3468bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
3469{
3470 size_t row, col;
3471
3472 size_t curNumRows = m_data.GetCount();
4db6714b
KH
3473 size_t curNumCols = ( curNumRows > 0
3474 ? m_data[0].GetCount()
3475 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3476
f85afd4e
MB
3477 if ( pos >= curNumCols )
3478 {
3479 return AppendCols( numCols );
3480 }
3481
3482 for ( row = 0; row < curNumRows; row++ )
3483 {
3484 for ( col = pos; col < pos + numCols; col++ )
3485 {
3486 m_data[row].Insert( wxEmptyString, col );
3487 }
3488 }
4db6714b 3489
f85afd4e
MB
3490 if ( GetView() )
3491 {
3492 wxGridTableMessage msg( this,
3493 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
3494 pos,
3495 numCols );
8f177c8e 3496
f85afd4e
MB
3497 GetView()->ProcessTableMessage( msg );
3498 }
3499
ca65c044 3500 return true;
f85afd4e
MB
3501}
3502
3503bool wxGridStringTable::AppendCols( size_t numCols )
3504{
27f35b66 3505 size_t row;
f85afd4e
MB
3506
3507 size_t curNumRows = m_data.GetCount();
f6bcfd97 3508#if 0
f85afd4e
MB
3509 if ( !curNumRows )
3510 {
3511 // TODO: something better than this ?
3512 //
f6bcfd97 3513 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
ca65c044 3514 return false;
f85afd4e 3515 }
f6bcfd97 3516#endif
8f177c8e 3517
f85afd4e
MB
3518 for ( row = 0; row < curNumRows; row++ )
3519 {
27f35b66 3520 m_data[row].Add( wxEmptyString, numCols );
f85afd4e
MB
3521 }
3522
3523 if ( GetView() )
3524 {
3525 wxGridTableMessage msg( this,
3526 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
3527 numCols );
8f177c8e 3528
f85afd4e
MB
3529 GetView()->ProcessTableMessage( msg );
3530 }
3531
ca65c044 3532 return true;
f85afd4e
MB
3533}
3534
3535bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
3536{
27f35b66 3537 size_t row;
f85afd4e
MB
3538
3539 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3540 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3541 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3542
f85afd4e
MB
3543 if ( pos >= curNumCols )
3544 {
e91d2033
VZ
3545 wxFAIL_MSG( wxString::Format
3546 (
3547 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3548 (unsigned long)pos,
3549 (unsigned long)numCols,
3550 (unsigned long)curNumCols
3551 ) );
ca65c044 3552 return false;
f85afd4e
MB
3553 }
3554
3555 if ( numCols > curNumCols - pos )
3556 {
8f177c8e 3557 numCols = curNumCols - pos;
f85afd4e
MB
3558 }
3559
3560 for ( row = 0; row < curNumRows; row++ )
3561 {
3562 if ( numCols >= curNumCols )
3563 {
dcdce64e 3564 m_data[row].Clear();
f85afd4e
MB
3565 }
3566 else
3567 {
27f35b66 3568 m_data[row].RemoveAt( pos, numCols );
f85afd4e
MB
3569 }
3570 }
4db6714b 3571
f85afd4e
MB
3572 if ( GetView() )
3573 {
3574 wxGridTableMessage msg( this,
3575 wxGRIDTABLE_NOTIFY_COLS_DELETED,
3576 pos,
3577 numCols );
8f177c8e 3578
f85afd4e
MB
3579 GetView()->ProcessTableMessage( msg );
3580 }
3581
ca65c044 3582 return true;
f85afd4e
MB
3583}
3584
3585wxString wxGridStringTable::GetRowLabelValue( int row )
3586{
3587 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3588 {
3589 // using default label
3590 //
3591 return wxGridTableBase::GetRowLabelValue( row );
3592 }
3593 else
3594 {
3595 return m_rowLabels[ row ];
3596 }
3597}
3598
3599wxString wxGridStringTable::GetColLabelValue( int col )
3600{
3601 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3602 {
3603 // using default label
3604 //
3605 return wxGridTableBase::GetColLabelValue( col );
3606 }
3607 else
3608 {
3609 return m_colLabels[ col ];
3610 }
3611}
3612
3613void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
3614{
3615 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3616 {
3617 int n = m_rowLabels.GetCount();
3618 int i;
3619 for ( i = n; i <= row; i++ )
3620 {
3621 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
3622 }
3623 }
3624
3625 m_rowLabels[row] = value;
3626}
3627
3628void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
3629{
3630 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3631 {
3632 int n = m_colLabels.GetCount();
3633 int i;
3634 for ( i = n; i <= col; i++ )
3635 {
3636 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
3637 }
3638 }
3639
3640 m_colLabels[col] = value;
3641}
3642
3643
f85afd4e 3644//////////////////////////////////////////////////////////////////////
2d66e025
MB
3645//////////////////////////////////////////////////////////////////////
3646
3647IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3648
3649BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
3650 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
b51c3f27 3651 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel)
2d66e025
MB
3652 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
3653 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
f6bcfd97 3654 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp )
63e2147c 3655 EVT_CHAR ( wxGridRowLabelWindow::OnChar )
2d66e025
MB
3656END_EVENT_TABLE()
3657
60ff3b99
VZ
3658wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
3659 wxWindowID id,
2d66e025 3660 const wxPoint &pos, const wxSize &size )
73bb6776 3661 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3662{
3663 m_owner = parent;
3664}
3665
aa5e1f75 3666void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3667{
3668 wxPaintDC dc(this);
3669
3670 // NO - don't do this because it will set both the x and y origin
3671 // coords to match the parent scrolled window and we just want to
3672 // set the y coord - MB
3673 //
3674 // m_owner->PrepareDC( dc );
60ff3b99 3675
790ad94f 3676 int x, y;
2d66e025
MB
3677 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3678 dc.SetDeviceOrigin( 0, -y );
60ff3b99 3679
d10f4bf9
VZ
3680 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
3681 m_owner->DrawRowLabels( dc , rows );
2d66e025
MB
3682}
3683
2d66e025
MB
3684void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3685{
3686 m_owner->ProcessRowLabelMouseEvent( event );
3687}
3688
b51c3f27
RD
3689void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3690{
3691 m_owner->GetEventHandler()->ProcessEvent(event);
3692}
3693
2d66e025
MB
3694// This seems to be required for wxMotif otherwise the mouse
3695// cursor must be in the cell edit control to get key events
3696//
3697void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3698{
ffdd3c98 3699 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3700}
3701
f6bcfd97
BP
3702void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3703{
ffdd3c98 3704 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3705}
3706
63e2147c
RD
3707void wxGridRowLabelWindow::OnChar( wxKeyEvent& event )
3708{
3709 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3710}
3711
2d66e025 3712
2d66e025
MB
3713//////////////////////////////////////////////////////////////////////
3714
3715IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3716
3717BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3718 EVT_PAINT( wxGridColLabelWindow::OnPaint )
b51c3f27 3719 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel)
2d66e025
MB
3720 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3721 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
f6bcfd97 3722 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
63e2147c 3723 EVT_CHAR ( wxGridColLabelWindow::OnChar )
2d66e025
MB
3724END_EVENT_TABLE()
3725
60ff3b99
VZ
3726wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3727 wxWindowID id,
2d66e025 3728 const wxPoint &pos, const wxSize &size )
73bb6776 3729 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3730{
3731 m_owner = parent;
3732}
3733
aa5e1f75 3734void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3735{
3736 wxPaintDC dc(this);
3737
3738 // NO - don't do this because it will set both the x and y origin
3739 // coords to match the parent scrolled window and we just want to
3740 // set the x coord - MB
3741 //
3742 // m_owner->PrepareDC( dc );
60ff3b99 3743
790ad94f 3744 int x, y;
2d66e025
MB
3745 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3746 dc.SetDeviceOrigin( -x, 0 );
3747
d10f4bf9
VZ
3748 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
3749 m_owner->DrawColLabels( dc , cols );
2d66e025
MB
3750}
3751
2d66e025
MB
3752void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3753{
3754 m_owner->ProcessColLabelMouseEvent( event );
3755}
3756
b51c3f27
RD
3757void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3758{
3759 m_owner->GetEventHandler()->ProcessEvent(event);
3760}
3761
2d66e025
MB
3762// This seems to be required for wxMotif otherwise the mouse
3763// cursor must be in the cell edit control to get key events
3764//
3765void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3766{
4db6714b
KH
3767 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3768 event.Skip();
2d66e025
MB
3769}
3770
f6bcfd97
BP
3771void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3772{
4db6714b
KH
3773 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3774 event.Skip();
f6bcfd97
BP
3775}
3776
63e2147c
RD
3777void wxGridColLabelWindow::OnChar( wxKeyEvent& event )
3778{
4db6714b
KH
3779 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3780 event.Skip();
63e2147c 3781}
2d66e025
MB
3782
3783
3784//////////////////////////////////////////////////////////////////////
3785
3786IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3787
3788BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
b51c3f27 3789 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel)
2d66e025 3790 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
d2fdd8d2 3791 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
2d66e025 3792 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
f6bcfd97 3793 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
63e2147c 3794 EVT_CHAR ( wxGridCornerLabelWindow::OnChar )
2d66e025
MB
3795END_EVENT_TABLE()
3796
60ff3b99
VZ
3797wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3798 wxWindowID id,
2d66e025 3799 const wxPoint &pos, const wxSize &size )
73bb6776 3800 : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3801{
3802 m_owner = parent;
3803}
3804
d2fdd8d2
RR
3805void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3806{
3807 wxPaintDC dc(this);
b99be8fb 3808
d2fdd8d2
RR
3809 int client_height = 0;
3810 int client_width = 0;
3811 GetClientSize( &client_width, &client_height );
b99be8fb 3812
11850ff3
VZ
3813 // VZ: any reason for this ifdef? (FIXME)
3814#ifdef __WXGTK__
4d1bc39c
RR
3815 wxRect rect;
3816 rect.SetX( 1 );
3817 rect.SetY( 1 );
3818 rect.SetWidth( client_width - 2 );
3819 rect.SetHeight( client_height - 2 );
ec157c8f 3820
4d1bc39c 3821 wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 );
11850ff3 3822#else // !__WXGTK__
73145b0e 3823 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
d2fdd8d2
RR
3824 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
3825 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
d2fdd8d2
RR
3826 dc.DrawLine( 0, 0, client_width, 0 );
3827 dc.DrawLine( 0, 0, 0, client_height );
73145b0e
JS
3828
3829 dc.SetPen( *wxWHITE_PEN );
3830 dc.DrawLine( 1, 1, client_width-1, 1 );
3831 dc.DrawLine( 1, 1, 1, client_height-1 );
11850ff3 3832#endif // __WXGTK__/!__WXGTK__
d2fdd8d2
RR
3833}
3834
2d66e025
MB
3835void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3836{
3837 m_owner->ProcessCornerLabelMouseEvent( event );
3838}
3839
b51c3f27
RD
3840void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3841{
3842 m_owner->GetEventHandler()->ProcessEvent(event);
3843}
3844
2d66e025
MB
3845// This seems to be required for wxMotif otherwise the mouse
3846// cursor must be in the cell edit control to get key events
3847//
3848void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3849{
ffdd3c98 3850 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
2d66e025
MB
3851}
3852
f6bcfd97
BP
3853void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3854{
ffdd3c98 3855 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
f6bcfd97
BP
3856}
3857
63e2147c
RD
3858void wxGridCornerLabelWindow::OnChar( wxKeyEvent& event )
3859{
3860 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3861}
2d66e025
MB
3862
3863
f85afd4e
MB
3864//////////////////////////////////////////////////////////////////////
3865
59ddac01 3866IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
2d66e025 3867
59ddac01 3868BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
2d66e025 3869 EVT_PAINT( wxGridWindow::OnPaint )
b51c3f27 3870 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel)
2d66e025
MB
3871 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3872 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 3873 EVT_KEY_UP( wxGridWindow::OnKeyUp )
63e2147c 3874 EVT_CHAR ( wxGridWindow::OnChar )
80acaf25
JS
3875 EVT_SET_FOCUS( wxGridWindow::OnFocus )
3876 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
2796cce3 3877 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
3878END_EVENT_TABLE()
3879
60ff3b99
VZ
3880wxGridWindow::wxGridWindow( wxGrid *parent,
3881 wxGridRowLabelWindow *rowLblWin,
2d66e025 3882 wxGridColLabelWindow *colLblWin,
04418332
VZ
3883 wxWindowID id,
3884 const wxPoint &pos,
3885 const wxSize &size )
73bb6776 3886 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN|wxFULL_REPAINT_ON_RESIZE,
04418332 3887 wxT("grid window") )
2d66e025
MB
3888{
3889 m_owner = parent;
3890 m_rowLabelWin = rowLblWin;
3891 m_colLabelWin = colLblWin;
2d66e025
MB
3892}
3893
2d66e025
MB
3894void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3895{
3896 wxPaintDC dc( this );
3897 m_owner->PrepareDC( dc );
796df70a 3898 wxRegion reg = GetUpdateRegion();
d10f4bf9
VZ
3899 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3900 m_owner->DrawGridCellArea( dc , DirtyCells);
9496deb5 3901#if WXGRID_DRAW_LINES
796df70a
SN
3902 m_owner->DrawAllGridLines( dc, reg );
3903#endif
a5777624 3904 m_owner->DrawGridSpace( dc );
d10f4bf9 3905 m_owner->DrawHighlight( dc , DirtyCells );
2d66e025
MB
3906}
3907
2d66e025
MB
3908void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3909{
59ddac01 3910 wxWindow::ScrollWindow( dx, dy, rect );
2d66e025
MB
3911 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3912 m_colLabelWin->ScrollWindow( dx, 0, rect );
3913}
3914
2d66e025
MB
3915void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3916{
33e9fc54
RD
3917 if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
3918 SetFocus();
902725ee 3919
2d66e025
MB
3920 m_owner->ProcessGridCellMouseEvent( event );
3921}
3922
b51c3f27
RD
3923void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3924{
3925 m_owner->GetEventHandler()->ProcessEvent(event);
3926}
2d66e025 3927
f6bcfd97 3928// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
3929// cursor must be in the cell edit control to get key events
3930//
3931void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3932{
4db6714b
KH
3933 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3934 event.Skip();
2d66e025 3935}
f85afd4e 3936
f6bcfd97
BP
3937void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3938{
4db6714b
KH
3939 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3940 event.Skip();
f6bcfd97 3941}
7c8a8ad5 3942
63e2147c
RD
3943void wxGridWindow::OnChar( wxKeyEvent& event )
3944{
4db6714b
KH
3945 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3946 event.Skip();
63e2147c
RD
3947}
3948
aa5e1f75 3949void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 3950{
8dd4f536 3951}
025562fe 3952
80acaf25
JS
3953void wxGridWindow::OnFocus(wxFocusEvent& event)
3954{
3955 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3956 event.Skip();
3957}
2d66e025
MB
3958
3959//////////////////////////////////////////////////////////////////////
3960
33188aa4
SN
3961// Internal Helper function for computing row or column from some
3962// (unscrolled) coordinate value, using either
70e8d961 3963// m_defaultRowHeight/m_defaultColWidth or binary search on array
33188aa4
SN
3964// of m_rowBottoms/m_ColRights to speed up the search!
3965
3966// Internal helper macros for simpler use of that function
3967
3968static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 3969 const wxArrayInt& BorderArray, int nMax,
a967f048 3970 bool clipToMinMax);
33188aa4
SN
3971
3972#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
b8d24d4e 3973 m_minAcceptableColWidth, \
ca65c044 3974 m_colRights, m_numCols, true)
33188aa4 3975#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
b8d24d4e 3976 m_minAcceptableRowHeight, \
ca65c044 3977 m_rowBottoms, m_numRows, true)
33188aa4 3978/////////////////////////////////////////////////////////////////////
07296f0b 3979
b0a877ec 3980#if wxUSE_EXTENDED_RTTI
73c36334
JS
3981WX_DEFINE_FLAGS( wxGridStyle )
3982
3ff066a4 3983wxBEGIN_FLAGS( wxGridStyle )
73c36334
JS
3984 // new style border flags, we put them first to
3985 // use them for streaming out
3ff066a4
SC
3986 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
3987 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
3988 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
3989 wxFLAGS_MEMBER(wxBORDER_RAISED)
3990 wxFLAGS_MEMBER(wxBORDER_STATIC)
3991 wxFLAGS_MEMBER(wxBORDER_NONE)
ca65c044 3992
73c36334 3993 // old style border flags
3ff066a4
SC
3994 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
3995 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
3996 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
3997 wxFLAGS_MEMBER(wxRAISED_BORDER)
3998 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 3999 wxFLAGS_MEMBER(wxBORDER)
73c36334
JS
4000
4001 // standard window styles
3ff066a4
SC
4002 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
4003 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
4004 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
4005 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 4006 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
4007 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
4008 wxFLAGS_MEMBER(wxVSCROLL)
4009 wxFLAGS_MEMBER(wxHSCROLL)
73c36334 4010
3ff066a4 4011wxEND_FLAGS( wxGridStyle )
73c36334 4012
b0a877ec
SC
4013IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
4014
3ff066a4
SC
4015wxBEGIN_PROPERTIES_TABLE(wxGrid)
4016 wxHIDE_PROPERTY( Children )
af498247 4017 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 4018wxEND_PROPERTIES_TABLE()
b0a877ec 4019
3ff066a4
SC
4020wxBEGIN_HANDLERS_TABLE(wxGrid)
4021wxEND_HANDLERS_TABLE()
b0a877ec 4022
ca65c044 4023wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
b0a877ec
SC
4024
4025/*
2d0c2e79 4026