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