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