1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grideditors.cpp
3 // Purpose: wxGridCellEditorEvtHandler and wxGrid editors
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
33 #include "wx/listbox.h"
36 #include "wx/valnum.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
41 #include "wx/headerctrl.h"
43 #include "wx/generic/gridsel.h"
44 #include "wx/generic/grideditors.h"
45 #include "wx/generic/private/grid.h"
47 #if defined(__WXMOTIF__)
48 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
50 #define WXUNUSED_MOTIF(identifier) identifier
53 #if defined(__WXGTK__)
54 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
56 #define WXUNUSED_GTK(identifier) identifier
59 // Required for wxIs... functions
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxGridCellEditorEvtHandler
68 // ----------------------------------------------------------------------------
70 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
72 // Don't disable the cell if we're just starting to edit it
80 m_grid
->DisableCellEditControl();
82 // notice that we must not skip the event here because the call above may
83 // delete the control which received the kill focus event in the first
84 // place and if we pretend not having processed the event, the search for a
85 // handler for it will continue using the now deleted object resulting in a
89 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
91 switch ( event
.GetKeyCode() )
95 m_grid
->DisableCellEditControl();
99 m_grid
->GetEventHandler()->ProcessEvent( event
);
103 case WXK_NUMPAD_ENTER
:
104 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
105 m_editor
->HandleReturn(event
);
114 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
116 int row
= m_grid
->GetGridCursorRow();
117 int col
= m_grid
->GetGridCursorCol();
118 wxRect rect
= m_grid
->CellToRect( row
, col
);
120 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
122 // if cell width is smaller than grid client area, cell is wholly visible
123 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
125 switch ( event
.GetKeyCode() )
130 case WXK_NUMPAD_ENTER
:
135 if ( wholeCellVisible
)
137 // no special processing needed...
142 // do special processing for partly visible cell...
144 // get the widths of all cells previous to this one
146 for ( int i
= 0; i
< col
; i
++ )
148 colXPos
+= m_grid
->GetColSize(i
);
151 int xUnit
= 1, yUnit
= 1;
152 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
155 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
159 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
167 if ( wholeCellVisible
)
169 // no special processing needed...
174 // do special processing for partly visible cell...
177 wxString value
= m_grid
->GetCellValue(row
, col
);
178 if ( wxEmptyString
!= value
)
180 // get width of cell CONTENTS (text)
182 wxFont font
= m_grid
->GetCellFont(row
, col
);
183 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
185 // try to RIGHT align the text by scrolling
186 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
188 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
189 // otherwise the last part of the cell content might be hidden below the scroll bar
190 // FIXME: maybe there is a more suitable correction?
191 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
198 // get the widths of all cells previous to this one
200 for ( int i
= 0; i
< col
; i
++ )
202 colXPos
+= m_grid
->GetColSize(i
);
205 // and add the (modified) text width of the cell contents
206 // as we'd like to see the last part of the cell contents
207 colXPos
+= textWidth
;
209 int xUnit
= 1, yUnit
= 1;
210 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
211 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 wxGridCellEditor::wxGridCellEditor()
232 wxGridCellEditor::~wxGridCellEditor()
237 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
238 wxWindowID
WXUNUSED(id
),
239 wxEvtHandler
* evtHandler
)
242 m_control
->PushEventHandler(evtHandler
);
245 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
246 wxGridCellAttr
*attr
)
248 // erase the background because we might not fill the cell
249 wxClientDC
dc(m_control
->GetParent());
250 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
252 gridWindow
->GetOwner()->PrepareDC(dc
);
254 dc
.SetPen(*wxTRANSPARENT_PEN
);
255 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
256 dc
.DrawRectangle(rectCell
);
258 // redraw the control we just painted over
259 m_control
->Refresh();
262 void wxGridCellEditor::Destroy()
266 m_control
->PopEventHandler( true /* delete it*/ );
268 m_control
->Destroy();
273 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
275 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
277 m_control
->Show(show
);
281 // set the colours/fonts if we have any
284 m_colFgOld
= m_control
->GetForegroundColour();
285 m_control
->SetForegroundColour(attr
->GetTextColour());
287 m_colBgOld
= m_control
->GetBackgroundColour();
288 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
290 // Workaround for GTK+1 font setting problem on some platforms
291 #if !defined(__WXGTK__) || defined(__WXGTK20__)
292 m_fontOld
= m_control
->GetFont();
293 m_control
->SetFont(attr
->GetFont());
296 // can't do anything more in the base class version, the other
297 // attributes may only be used by the derived classes
302 // restore the standard colours fonts
303 if ( m_colFgOld
.IsOk() )
305 m_control
->SetForegroundColour(m_colFgOld
);
306 m_colFgOld
= wxNullColour
;
309 if ( m_colBgOld
.IsOk() )
311 m_control
->SetBackgroundColour(m_colBgOld
);
312 m_colBgOld
= wxNullColour
;
315 // Workaround for GTK+1 font setting problem on some platforms
316 #if !defined(__WXGTK__) || defined(__WXGTK20__)
317 if ( m_fontOld
.IsOk() )
319 m_control
->SetFont(m_fontOld
);
320 m_fontOld
= wxNullFont
;
326 void wxGridCellEditor::SetSize(const wxRect
& rect
)
328 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
330 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
333 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
338 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
340 bool ctrl
= event
.ControlDown();
344 // On the Mac the Alt key is more like shift and is used for entry of
345 // valid characters, so check for Ctrl and Meta instead.
346 alt
= event
.MetaDown();
348 alt
= event
.AltDown();
349 #endif // __WXMAC__/!__WXMAC__
351 // Assume it's not a valid char if ctrl or alt is down, but if both are
352 // down then it may be because of an AltGr key combination, so let them
353 // through in that case.
354 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
358 if ( static_cast<int>(event
.GetUnicodeKey()) == WXK_NONE
)
361 if ( event
.GetKeyCode() > WXK_START
)
368 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
373 void wxGridCellEditor::StartingClick()
379 // ----------------------------------------------------------------------------
380 // wxGridCellTextEditor
381 // ----------------------------------------------------------------------------
383 wxGridCellTextEditor::wxGridCellTextEditor()
388 void wxGridCellTextEditor::Create(wxWindow
* parent
,
390 wxEvtHandler
* evtHandler
)
392 DoCreate(parent
, id
, evtHandler
);
395 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
397 wxEvtHandler
* evtHandler
,
400 // Use of wxTE_RICH2 is a strange hack to work around the bug #11681: a
401 // plain text control seems to lose its caret somehow when we hide it and
402 // show it again for a different cell.
403 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
| wxTE_RICH2
;
405 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
406 wxDefaultPosition
, wxDefaultSize
,
409 // set max length allowed in the textctrl, if the parameter was set
410 if ( m_maxChars
!= 0 )
412 Text()->SetMaxLength(m_maxChars
);
415 wxGridCellEditor::Create(parent
, id
, evtHandler
);
418 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
419 wxGridCellAttr
* WXUNUSED(attr
))
421 // as we fill the entire client area,
422 // don't do anything here to minimize flicker
425 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
427 wxRect
rect(rectOrig
);
429 // Make the edit control large enough to allow for internal margins
431 // TODO: remove this if the text ctrl sizing is improved esp. for unix
433 #if defined(__WXGTK__)
441 #elif defined(__WXMSW__)
455 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
456 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
458 #if defined(__WXMOTIF__)
463 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
464 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
465 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
466 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
469 wxGridCellEditor::SetSize(rect
);
472 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
474 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
476 m_value
= grid
->GetTable()->GetValue(row
, col
);
478 DoBeginEdit(m_value
);
481 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
483 Text()->SetValue(startValue
);
484 Text()->SetInsertionPointEnd();
485 Text()->SetSelection(-1, -1);
489 bool wxGridCellTextEditor::EndEdit(int WXUNUSED(row
),
491 const wxGrid
* WXUNUSED(grid
),
492 const wxString
& WXUNUSED(oldval
),
495 wxCHECK_MSG( m_control
, false,
496 "wxGridCellTextEditor must be created first!" );
498 const wxString value
= Text()->GetValue();
499 if ( value
== m_value
)
510 void wxGridCellTextEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
512 grid
->GetTable()->SetValue(row
, col
, m_value
);
516 void wxGridCellTextEditor::Reset()
518 wxASSERT_MSG( m_control
, "wxGridCellTextEditor must be created first!" );
523 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
525 Text()->SetValue(startValue
);
526 Text()->SetInsertionPointEnd();
529 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
531 switch ( event
.GetKeyCode() )
538 return wxGridCellEditor::IsAcceptedKey(event
);
542 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
544 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
545 // longer an appropriate way to get the character into the text control.
546 // Do it ourselves instead. We know that if we get this far that we have
547 // a valid character, so not a whole lot of testing needs to be done.
549 wxTextCtrl
* tc
= Text();
555 ch
= event
.GetUnicodeKey();
556 if ( ch
!= WXK_NONE
)
559 #endif // wxUSE_UNICODE
561 ch
= event
.GetKeyCode();
562 isPrintable
= ch
>= WXK_SPACE
&& ch
< WXK_START
;
568 // Delete the initial character when starting to edit with DELETE.
573 // Delete the last character when starting to edit with BACKSPACE.
575 const long pos
= tc
->GetLastPosition();
576 tc
->Remove(pos
- 1, pos
);
582 tc
->WriteText(static_cast<wxChar
>(ch
));
587 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
588 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
590 #if defined(__WXMOTIF__) || defined(__WXGTK__)
591 // wxMotif needs a little extra help...
592 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
593 wxString
s( Text()->GetValue() );
594 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
596 Text()->SetInsertionPoint( pos
);
598 // the other ports can handle a Return key press
604 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
614 if ( params
.ToLong(&tmp
) )
616 m_maxChars
= (size_t)tmp
;
620 wxLogDebug( wxT("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
625 // return the value in the text control
626 wxString
wxGridCellTextEditor::GetValue() const
628 return Text()->GetValue();
631 // ----------------------------------------------------------------------------
632 // wxGridCellNumberEditor
633 // ----------------------------------------------------------------------------
635 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
641 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
643 wxEvtHandler
* evtHandler
)
648 // create a spin ctrl
649 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
650 wxDefaultPosition
, wxDefaultSize
,
654 wxGridCellEditor::Create(parent
, id
, evtHandler
);
659 // just a text control
660 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
663 Text()->SetValidator(wxIntegerValidator
<int>());
668 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
670 // first get the value
671 wxGridTableBase
*table
= grid
->GetTable();
672 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
674 m_value
= table
->GetValueAsLong(row
, col
);
679 wxString sValue
= table
->GetValue(row
, col
);
680 if (! sValue
.ToLong(&m_value
) && ! sValue
.empty())
682 wxFAIL_MSG( wxT("this cell doesn't have numeric value") );
690 Spin()->SetValue((int)m_value
);
696 DoBeginEdit(GetString());
700 bool wxGridCellNumberEditor::EndEdit(int WXUNUSED(row
),
702 const wxGrid
* WXUNUSED(grid
),
703 const wxString
& oldval
, wxString
*newval
)
711 value
= Spin()->GetValue();
712 if ( value
== m_value
)
715 text
.Printf(wxT("%ld"), value
);
717 else // using unconstrained input
718 #endif // wxUSE_SPINCTRL
720 text
= Text()->GetValue();
723 if ( oldval
.empty() )
726 else // non-empty text now (maybe 0)
728 if ( !text
.ToLong(&value
) )
731 // if value == m_value == 0 but old text was "" and new one is
732 // "0" something still did change
733 if ( value
== m_value
&& (value
|| !oldval
.empty()) )
746 void wxGridCellNumberEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
748 wxGridTableBase
* const table
= grid
->GetTable();
749 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
750 table
->SetValueAsLong(row
, col
, m_value
);
752 table
->SetValue(row
, col
, wxString::Format("%ld", m_value
));
755 void wxGridCellNumberEditor::Reset()
760 Spin()->SetValue((int)m_value
);
765 DoReset(GetString());
769 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
771 if ( wxGridCellEditor::IsAcceptedKey(event
) )
773 int keycode
= event
.GetKeyCode();
774 if ( (keycode
< 128) &&
775 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
784 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
786 int keycode
= event
.GetKeyCode();
789 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
791 wxGridCellTextEditor::StartingKey(event
);
800 if ( wxIsdigit(keycode
) )
802 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
803 spin
->SetValue(keycode
- '0');
804 spin
->SetSelection(1,1);
813 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
824 if ( params
.BeforeFirst(wxT(',')).ToLong(&tmp
) )
828 if ( params
.AfterFirst(wxT(',')).ToLong(&tmp
) )
832 // skip the error message below
837 wxLogDebug(wxT("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
841 // return the value in the spin control if it is there (the text control otherwise)
842 wxString
wxGridCellNumberEditor::GetValue() const
849 long value
= Spin()->GetValue();
850 s
.Printf(wxT("%ld"), value
);
855 s
= Text()->GetValue();
861 // ----------------------------------------------------------------------------
862 // wxGridCellFloatEditor
863 // ----------------------------------------------------------------------------
865 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
868 m_precision
= precision
;
871 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
873 wxEvtHandler
* evtHandler
)
875 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
878 Text()->SetValidator(wxFloatingPointValidator
<double>(m_precision
));
882 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
884 // first get the value
885 wxGridTableBase
* const table
= grid
->GetTable();
886 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
888 m_value
= table
->GetValueAsDouble(row
, col
);
894 const wxString value
= table
->GetValue(row
, col
);
895 if ( !value
.empty() )
897 if ( !value
.ToDouble(&m_value
) )
899 wxFAIL_MSG( wxT("this cell doesn't have float value") );
905 DoBeginEdit(GetString());
908 bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row
),
910 const wxGrid
* WXUNUSED(grid
),
911 const wxString
& oldval
, wxString
*newval
)
913 const wxString
text(Text()->GetValue());
918 if ( !text
.ToDouble(&value
) )
921 else // new value is empty string
923 if ( oldval
.empty() )
924 return false; // nothing changed
929 // the test for empty strings ensures that we don't skip the value setting
930 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
931 if ( wxIsSameDouble(value
, m_value
) && !text
.empty() && !oldval
.empty() )
932 return false; // nothing changed
942 void wxGridCellFloatEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
944 wxGridTableBase
* const table
= grid
->GetTable();
946 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
947 table
->SetValueAsDouble(row
, col
, m_value
);
949 table
->SetValue(row
, col
, Text()->GetValue());
952 void wxGridCellFloatEditor::Reset()
954 DoReset(GetString());
957 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
959 int keycode
= event
.GetKeyCode();
961 tmpbuf
[0] = (char) keycode
;
963 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
966 bool is_decimal_point
= ( strbuf
==
967 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
969 bool is_decimal_point
= ( strbuf
== wxT(".") );
972 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
973 || is_decimal_point
)
975 wxGridCellTextEditor::StartingKey(event
);
984 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
995 if ( params
.BeforeFirst(wxT(',')).ToLong(&tmp
) )
999 if ( params
.AfterFirst(wxT(',')).ToLong(&tmp
) )
1001 m_precision
= (int)tmp
;
1003 // skip the error message below
1008 wxLogDebug(wxT("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1012 wxString
wxGridCellFloatEditor::GetString() const
1015 if ( m_precision
== -1 && m_width
!= -1)
1017 // default precision
1018 fmt
.Printf(wxT("%%%d.f"), m_width
);
1020 else if ( m_precision
!= -1 && m_width
== -1)
1023 fmt
.Printf(wxT("%%.%df"), m_precision
);
1025 else if ( m_precision
!= -1 && m_width
!= -1 )
1027 fmt
.Printf(wxT("%%%d.%df"), m_width
, m_precision
);
1031 // default width/precision
1035 return wxString::Format(fmt
, m_value
);
1038 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1040 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1042 const int keycode
= event
.GetKeyCode();
1043 if ( wxIsascii(keycode
) )
1046 const wxString decimalPoint
=
1047 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1049 const wxString
decimalPoint(wxT('.'));
1052 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1053 if ( wxIsdigit(keycode
) ||
1054 tolower(keycode
) == 'e' ||
1055 keycode
== decimalPoint
||
1067 #endif // wxUSE_TEXTCTRL
1071 // ----------------------------------------------------------------------------
1072 // wxGridCellBoolEditor
1073 // ----------------------------------------------------------------------------
1075 // the default values for GetValue()
1076 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { wxT(""), wxT("1") };
1078 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1080 wxEvtHandler
* evtHandler
)
1082 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1083 wxDefaultPosition
, wxDefaultSize
,
1086 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1089 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1091 bool resize
= false;
1092 wxSize size
= m_control
->GetSize();
1093 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1095 // check if the checkbox is not too big/small for this cell
1096 wxSize sizeBest
= m_control
->GetBestSize();
1097 if ( !(size
== sizeBest
) )
1099 // reset to default size if it had been made smaller
1105 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1107 // leave 1 pixel margin
1108 size
.x
= size
.y
= minSize
- 2;
1115 m_control
->SetSize(size
);
1118 // position it in the centre of the rectangle (TODO: support alignment?)
1120 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1121 // the checkbox without label still has some space to the right in wxGTK,
1122 // so shift it to the right
1124 #elif defined(__WXMSW__)
1125 // here too, but in other way
1130 int hAlign
= wxALIGN_CENTRE
;
1131 int vAlign
= wxALIGN_CENTRE
;
1133 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1136 if (hAlign
== wxALIGN_LEFT
)
1144 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1146 else if (hAlign
== wxALIGN_RIGHT
)
1148 x
= r
.x
+ r
.width
- size
.x
- 2;
1149 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1151 else if (hAlign
== wxALIGN_CENTRE
)
1153 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1154 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1157 m_control
->Move(x
, y
);
1160 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1162 m_control
->Show(show
);
1166 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1167 CBox()->SetBackgroundColour(colBg
);
1171 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1173 wxASSERT_MSG(m_control
,
1174 wxT("The wxGridCellEditor must be created first!"));
1176 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1178 m_value
= grid
->GetTable()->GetValueAsBool(row
, col
);
1182 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1184 if ( cellval
== ms_stringValues
[false] )
1186 else if ( cellval
== ms_stringValues
[true] )
1190 // do not try to be smart here and convert it to true or false
1191 // because we'll still overwrite it with something different and
1192 // this risks to be very surprising for the user code, let them
1194 wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") );
1198 CBox()->SetValue(m_value
);
1202 bool wxGridCellBoolEditor::EndEdit(int WXUNUSED(row
),
1204 const wxGrid
* WXUNUSED(grid
),
1205 const wxString
& WXUNUSED(oldval
),
1208 bool value
= CBox()->GetValue();
1209 if ( value
== m_value
)
1215 *newval
= GetValue();
1220 void wxGridCellBoolEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1222 wxGridTableBase
* const table
= grid
->GetTable();
1223 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1224 table
->SetValueAsBool(row
, col
, m_value
);
1226 table
->SetValue(row
, col
, GetValue());
1229 void wxGridCellBoolEditor::Reset()
1231 wxASSERT_MSG(m_control
,
1232 wxT("The wxGridCellEditor must be created first!"));
1234 CBox()->SetValue(m_value
);
1237 void wxGridCellBoolEditor::StartingClick()
1239 CBox()->SetValue(!CBox()->GetValue());
1242 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1244 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1246 int keycode
= event
.GetKeyCode();
1259 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1261 int keycode
= event
.GetKeyCode();
1265 CBox()->SetValue(!CBox()->GetValue());
1269 CBox()->SetValue(true);
1273 CBox()->SetValue(false);
1278 wxString
wxGridCellBoolEditor::GetValue() const
1280 return ms_stringValues
[CBox()->GetValue()];
1284 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1285 const wxString
& valueFalse
)
1287 ms_stringValues
[false] = valueFalse
;
1288 ms_stringValues
[true] = valueTrue
;
1292 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1294 return value
== ms_stringValues
[true];
1297 #endif // wxUSE_CHECKBOX
1301 // ----------------------------------------------------------------------------
1302 // wxGridCellChoiceEditor
1303 // ----------------------------------------------------------------------------
1305 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1307 : m_choices(choices
),
1308 m_allowOthers(allowOthers
) { }
1310 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1311 const wxString choices
[],
1313 : m_allowOthers(allowOthers
)
1317 m_choices
.Alloc(count
);
1318 for ( size_t n
= 0; n
< count
; n
++ )
1320 m_choices
.Add(choices
[n
]);
1325 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1327 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1328 editor
->m_allowOthers
= m_allowOthers
;
1329 editor
->m_choices
= m_choices
;
1334 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1336 wxEvtHandler
* evtHandler
)
1338 int style
= wxTE_PROCESS_ENTER
|
1342 if ( !m_allowOthers
)
1343 style
|= wxCB_READONLY
;
1344 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1345 wxDefaultPosition
, wxDefaultSize
,
1349 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1352 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1353 wxGridCellAttr
* attr
)
1355 // as we fill the entire client area, don't do anything here to minimize
1358 // TODO: It doesn't actually fill the client area since the height of a
1359 // combo always defaults to the standard. Until someone has time to
1360 // figure out the right rectangle to paint, just do it the normal way.
1361 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1364 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1366 wxASSERT_MSG(m_control
,
1367 wxT("The wxGridCellEditor must be created first!"));
1369 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1371 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1373 // Don't immediately end if we get a kill focus event within BeginEdit
1375 evtHandler
->SetInSetFocus(true);
1377 m_value
= grid
->GetTable()->GetValue(row
, col
);
1379 Reset(); // this updates combo box to correspond to m_value
1381 Combo()->SetFocus();
1385 // When dropping down the menu, a kill focus event
1386 // happens after this point, so we can't reset the flag yet.
1387 #if !defined(__WXGTK20__)
1388 evtHandler
->SetInSetFocus(false);
1393 bool wxGridCellChoiceEditor::EndEdit(int WXUNUSED(row
),
1395 const wxGrid
* WXUNUSED(grid
),
1396 const wxString
& WXUNUSED(oldval
),
1399 const wxString value
= Combo()->GetValue();
1400 if ( value
== m_value
)
1411 void wxGridCellChoiceEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1413 grid
->GetTable()->SetValue(row
, col
, m_value
);
1416 void wxGridCellChoiceEditor::Reset()
1420 Combo()->SetValue(m_value
);
1421 Combo()->SetInsertionPointEnd();
1423 else // the combobox is read-only
1425 // find the right position, or default to the first if not found
1426 int pos
= Combo()->FindString(m_value
);
1427 if (pos
== wxNOT_FOUND
)
1429 Combo()->SetSelection(pos
);
1433 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1443 wxStringTokenizer
tk(params
, wxT(','));
1444 while ( tk
.HasMoreTokens() )
1446 m_choices
.Add(tk
.GetNextToken());
1450 // return the value in the text control
1451 wxString
wxGridCellChoiceEditor::GetValue() const
1453 return Combo()->GetValue();
1456 #endif // wxUSE_COMBOBOX
1460 // ----------------------------------------------------------------------------
1461 // wxGridCellEnumEditor
1462 // ----------------------------------------------------------------------------
1464 // A cell editor which displays an enum number as a textual equivalent. eg
1465 // data in cell is 0,1,2 ... n the cell could be displayed as
1466 // "John","Fred"..."Bob" in the combo choice box
1468 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
1469 :wxGridCellChoiceEditor()
1473 if (!choices
.empty())
1474 SetParameters(choices
);
1477 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
1479 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
1480 editor
->m_index
= m_index
;
1484 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1486 wxASSERT_MSG(m_control
,
1487 wxT("The wxGridCellEnumEditor must be Created first!"));
1489 wxGridTableBase
*table
= grid
->GetTable();
1491 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1493 m_index
= table
->GetValueAsLong(row
, col
);
1497 wxString startValue
= table
->GetValue(row
, col
);
1498 if (startValue
.IsNumber() && !startValue
.empty())
1500 startValue
.ToLong(&m_index
);
1508 Combo()->SetSelection(m_index
);
1509 Combo()->SetFocus();
1513 bool wxGridCellEnumEditor::EndEdit(int WXUNUSED(row
),
1515 const wxGrid
* WXUNUSED(grid
),
1516 const wxString
& WXUNUSED(oldval
),
1519 long idx
= Combo()->GetSelection();
1520 if ( idx
== m_index
)
1526 newval
->Printf("%ld", m_index
);
1531 void wxGridCellEnumEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1533 wxGridTableBase
* const table
= grid
->GetTable();
1534 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1535 table
->SetValueAsLong(row
, col
, m_index
);
1537 table
->SetValue(row
, col
, wxString::Format("%ld", m_index
));
1540 #endif // wxUSE_COMBOBOX
1542 // ----------------------------------------------------------------------------
1543 // wxGridCellAutoWrapStringEditor
1544 // ----------------------------------------------------------------------------
1547 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
1549 wxEvtHandler
* evtHandler
)
1551 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
1552 wxTE_MULTILINE
| wxTE_RICH
);
1556 #endif // wxUSE_GRID