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
,
870 m_precision
= precision
;
874 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
876 wxEvtHandler
* evtHandler
)
878 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
881 Text()->SetValidator(wxFloatingPointValidator
<double>(m_precision
));
885 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
887 // first get the value
888 wxGridTableBase
* const table
= grid
->GetTable();
889 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
891 m_value
= table
->GetValueAsDouble(row
, col
);
897 const wxString value
= table
->GetValue(row
, col
);
898 if ( !value
.empty() )
900 if ( !value
.ToDouble(&m_value
) )
902 wxFAIL_MSG( wxT("this cell doesn't have float value") );
908 DoBeginEdit(GetString());
911 bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row
),
913 const wxGrid
* WXUNUSED(grid
),
914 const wxString
& oldval
, wxString
*newval
)
916 const wxString
text(Text()->GetValue());
921 if ( !text
.ToDouble(&value
) )
924 else // new value is empty string
926 if ( oldval
.empty() )
927 return false; // nothing changed
932 // the test for empty strings ensures that we don't skip the value setting
933 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
934 if ( wxIsSameDouble(value
, m_value
) && !text
.empty() && !oldval
.empty() )
935 return false; // nothing changed
945 void wxGridCellFloatEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
947 wxGridTableBase
* const table
= grid
->GetTable();
949 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
950 table
->SetValueAsDouble(row
, col
, m_value
);
952 table
->SetValue(row
, col
, Text()->GetValue());
955 void wxGridCellFloatEditor::Reset()
957 DoReset(GetString());
960 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
962 int keycode
= event
.GetKeyCode();
964 tmpbuf
[0] = (char) keycode
;
966 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
969 bool is_decimal_point
= ( strbuf
==
970 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
972 bool is_decimal_point
= ( strbuf
== wxT(".") );
975 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
976 || is_decimal_point
)
978 wxGridCellTextEditor::StartingKey(event
);
987 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
994 m_style
= wxGRID_FLOAT_FORMAT_DEFAULT
;
1000 wxString tmp
= params
.BeforeFirst(wxT(','), &rest
);
1004 if ( tmp
.ToLong(&width
) )
1006 m_width
= (int)width
;
1010 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1014 tmp
= rest
.BeforeFirst(wxT(','));
1018 if ( tmp
.ToLong(&precision
) )
1020 m_precision
= (int)precision
;
1024 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1028 tmp
= rest
.AfterFirst(wxT(','));
1031 if ( tmp
[0] == wxT('f') )
1033 m_style
= wxGRID_FLOAT_FORMAT_FIXED
;
1035 else if ( tmp
[0] == wxT('e') )
1037 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
;
1039 else if ( tmp
[0] == wxT('g') )
1041 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
;
1043 else if ( tmp
[0] == wxT('E') )
1045 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
|
1046 wxGRID_FLOAT_FORMAT_UPPER
;
1048 else if ( tmp
[0] == wxT('F') )
1050 m_style
= wxGRID_FLOAT_FORMAT_FIXED
|
1051 wxGRID_FLOAT_FORMAT_UPPER
;
1053 else if ( tmp
[0] == wxT('G') )
1055 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
|
1056 wxGRID_FLOAT_FORMAT_UPPER
;
1060 wxLogDebug("Invalid wxGridCellFloatRenderer format "
1061 "parameter string '%s ignored", params
);
1067 wxString
wxGridCellFloatEditor::GetString()
1071 if ( m_precision
== -1 && m_width
!= -1)
1073 // default precision
1074 m_format
.Printf(wxT("%%%d."), m_width
);
1076 else if ( m_precision
!= -1 && m_width
== -1)
1079 m_format
.Printf(wxT("%%.%d"), m_precision
);
1081 else if ( m_precision
!= -1 && m_width
!= -1 )
1083 m_format
.Printf(wxT("%%%d.%d"), m_width
, m_precision
);
1087 // default width/precision
1088 m_format
= wxT("%");
1091 bool isUpper
= (m_style
& wxGRID_FLOAT_FORMAT_UPPER
) != 0;
1092 if ( m_style
& wxGRID_FLOAT_FORMAT_SCIENTIFIC
)
1093 m_format
+= isUpper
? wxT('E') : wxT('e');
1094 else if ( m_style
& wxGRID_FLOAT_FORMAT_COMPACT
)
1095 m_format
+= isUpper
? wxT('G') : wxT('g');
1097 m_format
+= wxT('f');
1100 return wxString::Format(m_format
, m_value
);
1103 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1105 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1107 const int keycode
= event
.GetKeyCode();
1108 if ( wxIsascii(keycode
) )
1111 const wxString decimalPoint
=
1112 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1114 const wxString
decimalPoint(wxT('.'));
1117 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1118 if ( wxIsdigit(keycode
) ||
1119 tolower(keycode
) == 'e' ||
1120 keycode
== decimalPoint
||
1132 #endif // wxUSE_TEXTCTRL
1136 // ----------------------------------------------------------------------------
1137 // wxGridCellBoolEditor
1138 // ----------------------------------------------------------------------------
1140 // the default values for GetValue()
1141 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { wxT(""), wxT("1") };
1143 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1145 wxEvtHandler
* evtHandler
)
1147 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1148 wxDefaultPosition
, wxDefaultSize
,
1151 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1154 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1156 bool resize
= false;
1157 wxSize size
= m_control
->GetSize();
1158 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1160 // check if the checkbox is not too big/small for this cell
1161 wxSize sizeBest
= m_control
->GetBestSize();
1162 if ( !(size
== sizeBest
) )
1164 // reset to default size if it had been made smaller
1170 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1172 // leave 1 pixel margin
1173 size
.x
= size
.y
= minSize
- 2;
1180 m_control
->SetSize(size
);
1183 // position it in the centre of the rectangle (TODO: support alignment?)
1185 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1186 // the checkbox without label still has some space to the right in wxGTK,
1187 // so shift it to the right
1189 #elif defined(__WXMSW__)
1190 // here too, but in other way
1195 int hAlign
= wxALIGN_CENTRE
;
1196 int vAlign
= wxALIGN_CENTRE
;
1198 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1201 if (hAlign
== wxALIGN_LEFT
)
1209 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1211 else if (hAlign
== wxALIGN_RIGHT
)
1213 x
= r
.x
+ r
.width
- size
.x
- 2;
1214 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1216 else if (hAlign
== wxALIGN_CENTRE
)
1218 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1219 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1222 m_control
->Move(x
, y
);
1225 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1227 m_control
->Show(show
);
1231 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1232 CBox()->SetBackgroundColour(colBg
);
1236 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1238 wxASSERT_MSG(m_control
,
1239 wxT("The wxGridCellEditor must be created first!"));
1241 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1243 m_value
= grid
->GetTable()->GetValueAsBool(row
, col
);
1247 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1249 if ( cellval
== ms_stringValues
[false] )
1251 else if ( cellval
== ms_stringValues
[true] )
1255 // do not try to be smart here and convert it to true or false
1256 // because we'll still overwrite it with something different and
1257 // this risks to be very surprising for the user code, let them
1259 wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") );
1263 CBox()->SetValue(m_value
);
1267 bool wxGridCellBoolEditor::EndEdit(int WXUNUSED(row
),
1269 const wxGrid
* WXUNUSED(grid
),
1270 const wxString
& WXUNUSED(oldval
),
1273 bool value
= CBox()->GetValue();
1274 if ( value
== m_value
)
1280 *newval
= GetValue();
1285 void wxGridCellBoolEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1287 wxGridTableBase
* const table
= grid
->GetTable();
1288 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1289 table
->SetValueAsBool(row
, col
, m_value
);
1291 table
->SetValue(row
, col
, GetValue());
1294 void wxGridCellBoolEditor::Reset()
1296 wxASSERT_MSG(m_control
,
1297 wxT("The wxGridCellEditor must be created first!"));
1299 CBox()->SetValue(m_value
);
1302 void wxGridCellBoolEditor::StartingClick()
1304 CBox()->SetValue(!CBox()->GetValue());
1307 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1309 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1311 int keycode
= event
.GetKeyCode();
1324 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1326 int keycode
= event
.GetKeyCode();
1330 CBox()->SetValue(!CBox()->GetValue());
1334 CBox()->SetValue(true);
1338 CBox()->SetValue(false);
1343 wxString
wxGridCellBoolEditor::GetValue() const
1345 return ms_stringValues
[CBox()->GetValue()];
1349 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1350 const wxString
& valueFalse
)
1352 ms_stringValues
[false] = valueFalse
;
1353 ms_stringValues
[true] = valueTrue
;
1357 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1359 return value
== ms_stringValues
[true];
1362 #endif // wxUSE_CHECKBOX
1366 // ----------------------------------------------------------------------------
1367 // wxGridCellChoiceEditor
1368 // ----------------------------------------------------------------------------
1370 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1372 : m_choices(choices
),
1373 m_allowOthers(allowOthers
) { }
1375 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1376 const wxString choices
[],
1378 : m_allowOthers(allowOthers
)
1382 m_choices
.Alloc(count
);
1383 for ( size_t n
= 0; n
< count
; n
++ )
1385 m_choices
.Add(choices
[n
]);
1390 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1392 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1393 editor
->m_allowOthers
= m_allowOthers
;
1394 editor
->m_choices
= m_choices
;
1399 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1401 wxEvtHandler
* evtHandler
)
1403 int style
= wxTE_PROCESS_ENTER
|
1407 if ( !m_allowOthers
)
1408 style
|= wxCB_READONLY
;
1409 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1410 wxDefaultPosition
, wxDefaultSize
,
1414 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1417 void wxGridCellChoiceEditor::SetSize(const wxRect
& rect
)
1419 wxASSERT_MSG(m_control
,
1420 wxT("The wxGridCellChoiceEditor must be created first!"));
1422 // Check that the height is not too small to fit the combobox.
1423 wxRect rectTallEnough
= rect
;
1424 const wxSize bestSize
= m_control
->GetBestSize();
1425 const wxCoord diffY
= bestSize
.GetHeight() - rectTallEnough
.GetHeight();
1428 // Do make it tall enough.
1429 rectTallEnough
.height
+= diffY
;
1431 // Also centre the effective rectangle vertically with respect to the
1433 rectTallEnough
.y
-= diffY
/2;
1435 //else: The rectangle provided is already tall enough.
1437 wxGridCellEditor::SetSize(rectTallEnough
);
1440 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1441 wxGridCellAttr
* attr
)
1443 // as we fill the entire client area, don't do anything here to minimize
1446 // TODO: It doesn't actually fill the client area since the height of a
1447 // combo always defaults to the standard. Until someone has time to
1448 // figure out the right rectangle to paint, just do it the normal way.
1449 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1452 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1454 wxASSERT_MSG(m_control
,
1455 wxT("The wxGridCellEditor must be created first!"));
1457 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1459 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1461 // Don't immediately end if we get a kill focus event within BeginEdit
1463 evtHandler
->SetInSetFocus(true);
1465 m_value
= grid
->GetTable()->GetValue(row
, col
);
1467 Reset(); // this updates combo box to correspond to m_value
1469 Combo()->SetFocus();
1471 #ifdef __WXOSX_COCOA__
1472 // This is a work around for the combobox being simply dismissed when a
1473 // choice is made in it under OS X. The bug is almost certainly due to a
1474 // problem in focus events generation logic but it's not obvious to fix and
1475 // for now this at least allows to use wxGrid.
1481 // When dropping down the menu, a kill focus event
1482 // happens after this point, so we can't reset the flag yet.
1483 #if !defined(__WXGTK20__)
1484 evtHandler
->SetInSetFocus(false);
1489 bool wxGridCellChoiceEditor::EndEdit(int WXUNUSED(row
),
1491 const wxGrid
* WXUNUSED(grid
),
1492 const wxString
& WXUNUSED(oldval
),
1495 const wxString value
= Combo()->GetValue();
1496 if ( value
== m_value
)
1507 void wxGridCellChoiceEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1509 grid
->GetTable()->SetValue(row
, col
, m_value
);
1512 void wxGridCellChoiceEditor::Reset()
1516 Combo()->SetValue(m_value
);
1517 Combo()->SetInsertionPointEnd();
1519 else // the combobox is read-only
1521 // find the right position, or default to the first if not found
1522 int pos
= Combo()->FindString(m_value
);
1523 if (pos
== wxNOT_FOUND
)
1525 Combo()->SetSelection(pos
);
1529 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1539 wxStringTokenizer
tk(params
, wxT(','));
1540 while ( tk
.HasMoreTokens() )
1542 m_choices
.Add(tk
.GetNextToken());
1546 // return the value in the text control
1547 wxString
wxGridCellChoiceEditor::GetValue() const
1549 return Combo()->GetValue();
1552 #endif // wxUSE_COMBOBOX
1556 // ----------------------------------------------------------------------------
1557 // wxGridCellEnumEditor
1558 // ----------------------------------------------------------------------------
1560 // A cell editor which displays an enum number as a textual equivalent. eg
1561 // data in cell is 0,1,2 ... n the cell could be displayed as
1562 // "John","Fred"..."Bob" in the combo choice box
1564 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
1565 :wxGridCellChoiceEditor()
1569 if (!choices
.empty())
1570 SetParameters(choices
);
1573 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
1575 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
1576 editor
->m_index
= m_index
;
1580 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1582 wxASSERT_MSG(m_control
,
1583 wxT("The wxGridCellEnumEditor must be Created first!"));
1585 wxGridTableBase
*table
= grid
->GetTable();
1587 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1589 m_index
= table
->GetValueAsLong(row
, col
);
1593 wxString startValue
= table
->GetValue(row
, col
);
1594 if (startValue
.IsNumber() && !startValue
.empty())
1596 startValue
.ToLong(&m_index
);
1604 Combo()->SetSelection(m_index
);
1605 Combo()->SetFocus();
1609 bool wxGridCellEnumEditor::EndEdit(int WXUNUSED(row
),
1611 const wxGrid
* WXUNUSED(grid
),
1612 const wxString
& WXUNUSED(oldval
),
1615 long idx
= Combo()->GetSelection();
1616 if ( idx
== m_index
)
1622 newval
->Printf("%ld", m_index
);
1627 void wxGridCellEnumEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1629 wxGridTableBase
* const table
= grid
->GetTable();
1630 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1631 table
->SetValueAsLong(row
, col
, m_index
);
1633 table
->SetValue(row
, col
, wxString::Format("%ld", m_index
));
1636 #endif // wxUSE_COMBOBOX
1638 // ----------------------------------------------------------------------------
1639 // wxGridCellAutoWrapStringEditor
1640 // ----------------------------------------------------------------------------
1643 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
1645 wxEvtHandler
* evtHandler
)
1647 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
1648 wxTE_MULTILINE
| wxTE_RICH
);
1652 #endif // wxUSE_GRID