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
60 #include "wx/osx/private.h"
63 // Required for wxIs... functions
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
71 // wxGridCellEditorEvtHandler
72 // ----------------------------------------------------------------------------
74 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
76 // Don't disable the cell if we're just starting to edit it
84 m_grid
->DisableCellEditControl();
86 // notice that we must not skip the event here because the call above may
87 // delete the control which received the kill focus event in the first
88 // place and if we pretend not having processed the event, the search for a
89 // handler for it will continue using the now deleted object resulting in a
93 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
95 switch ( event
.GetKeyCode() )
99 m_grid
->DisableCellEditControl();
103 m_grid
->GetEventHandler()->ProcessEvent( event
);
107 case WXK_NUMPAD_ENTER
:
108 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
109 m_editor
->HandleReturn(event
);
118 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
120 int row
= m_grid
->GetGridCursorRow();
121 int col
= m_grid
->GetGridCursorCol();
122 wxRect rect
= m_grid
->CellToRect( row
, col
);
124 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
126 // if cell width is smaller than grid client area, cell is wholly visible
127 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
129 switch ( event
.GetKeyCode() )
134 case WXK_NUMPAD_ENTER
:
139 if ( wholeCellVisible
)
141 // no special processing needed...
146 // do special processing for partly visible cell...
148 // get the widths of all cells previous to this one
150 for ( int i
= 0; i
< col
; i
++ )
152 colXPos
+= m_grid
->GetColSize(i
);
155 int xUnit
= 1, yUnit
= 1;
156 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
159 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
163 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
171 if ( wholeCellVisible
)
173 // no special processing needed...
178 // do special processing for partly visible cell...
181 wxString value
= m_grid
->GetCellValue(row
, col
);
182 if ( wxEmptyString
!= value
)
184 // get width of cell CONTENTS (text)
186 wxFont font
= m_grid
->GetCellFont(row
, col
);
187 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
189 // try to RIGHT align the text by scrolling
190 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
192 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
193 // otherwise the last part of the cell content might be hidden below the scroll bar
194 // FIXME: maybe there is a more suitable correction?
195 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
202 // get the widths of all cells previous to this one
204 for ( int i
= 0; i
< col
; i
++ )
206 colXPos
+= m_grid
->GetColSize(i
);
209 // and add the (modified) text width of the cell contents
210 // as we'd like to see the last part of the cell contents
211 colXPos
+= textWidth
;
213 int xUnit
= 1, yUnit
= 1;
214 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
215 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 wxGridCellEditor::wxGridCellEditor()
236 wxGridCellEditor::~wxGridCellEditor()
241 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
242 wxWindowID
WXUNUSED(id
),
243 wxEvtHandler
* evtHandler
)
246 m_control
->PushEventHandler(evtHandler
);
249 void wxGridCellEditor::PaintBackground(wxDC
& dc
,
250 const wxRect
& rectCell
,
251 const wxGridCellAttr
& attr
)
253 // erase the background because we might not fill the cell
254 dc
.SetPen(*wxTRANSPARENT_PEN
);
255 dc
.SetBrush(wxBrush(attr
.GetBackgroundColour()));
256 dc
.DrawRectangle(rectCell
);
259 void wxGridCellEditor::Destroy()
263 m_control
->PopEventHandler( true /* delete it*/ );
265 m_control
->Destroy();
270 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
272 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
274 m_control
->Show(show
);
278 // set the colours/fonts if we have any
281 m_colFgOld
= m_control
->GetForegroundColour();
282 m_control
->SetForegroundColour(attr
->GetTextColour());
284 m_colBgOld
= m_control
->GetBackgroundColour();
285 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
287 // Workaround for GTK+1 font setting problem on some platforms
288 #if !defined(__WXGTK__) || defined(__WXGTK20__)
289 m_fontOld
= m_control
->GetFont();
290 m_control
->SetFont(attr
->GetFont());
293 // can't do anything more in the base class version, the other
294 // attributes may only be used by the derived classes
299 // restore the standard colours fonts
300 if ( m_colFgOld
.IsOk() )
302 m_control
->SetForegroundColour(m_colFgOld
);
303 m_colFgOld
= wxNullColour
;
306 if ( m_colBgOld
.IsOk() )
308 m_control
->SetBackgroundColour(m_colBgOld
);
309 m_colBgOld
= wxNullColour
;
312 // Workaround for GTK+1 font setting problem on some platforms
313 #if !defined(__WXGTK__) || defined(__WXGTK20__)
314 if ( m_fontOld
.IsOk() )
316 m_control
->SetFont(m_fontOld
);
317 m_fontOld
= wxNullFont
;
323 void wxGridCellEditor::SetSize(const wxRect
& rect
)
325 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
327 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
330 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
335 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
337 bool ctrl
= event
.ControlDown();
341 // On the Mac the Alt key is more like shift and is used for entry of
342 // valid characters, so check for Ctrl and Meta instead.
343 alt
= event
.MetaDown();
345 alt
= event
.AltDown();
346 #endif // __WXMAC__/!__WXMAC__
348 // Assume it's not a valid char if ctrl or alt is down, but if both are
349 // down then it may be because of an AltGr key combination, so let them
350 // through in that case.
351 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
355 if ( static_cast<int>(event
.GetUnicodeKey()) == WXK_NONE
)
358 if ( event
.GetKeyCode() > WXK_START
)
365 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
370 void wxGridCellEditor::StartingClick()
376 // ----------------------------------------------------------------------------
377 // wxGridCellTextEditor
378 // ----------------------------------------------------------------------------
380 wxGridCellTextEditor::wxGridCellTextEditor()
385 void wxGridCellTextEditor::Create(wxWindow
* parent
,
387 wxEvtHandler
* evtHandler
)
389 DoCreate(parent
, id
, evtHandler
);
392 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
394 wxEvtHandler
* evtHandler
,
397 // Use of wxTE_RICH2 is a strange hack to work around the bug #11681: a
398 // plain text control seems to lose its caret somehow when we hide it and
399 // show it again for a different cell.
400 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
| wxTE_RICH2
;
402 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
403 wxDefaultPosition
, wxDefaultSize
,
407 wxWidgetImpl
* impl
= m_control
->GetPeer();
408 impl
->SetNeedsFocusRect(false);
410 // set max length allowed in the textctrl, if the parameter was set
411 if ( m_maxChars
!= 0 )
413 Text()->SetMaxLength(m_maxChars
);
416 wxGridCellEditor::Create(parent
, id
, evtHandler
);
419 void wxGridCellTextEditor::PaintBackground(wxDC
& WXUNUSED(dc
),
420 const wxRect
& WXUNUSED(rectCell
),
421 const wxGridCellAttr
& WXUNUSED(attr
))
423 // as we fill the entire client area,
424 // don't do anything here to minimize flicker
427 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
429 wxRect
rect(rectOrig
);
431 // Make the edit control large enough to allow for internal margins
433 // TODO: remove this if the text ctrl sizing is improved esp. for unix
435 #if defined(__WXGTK__)
443 #elif defined(__WXMSW__)
456 #elif defined(__WXOSX__)
463 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
464 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
466 #if defined(__WXMOTIF__)
471 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
472 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
473 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
474 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
477 wxGridCellEditor::SetSize(rect
);
480 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
482 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
484 m_value
= grid
->GetTable()->GetValue(row
, col
);
486 DoBeginEdit(m_value
);
489 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
491 Text()->SetValue(startValue
);
492 Text()->SetInsertionPointEnd();
497 bool wxGridCellTextEditor::EndEdit(int WXUNUSED(row
),
499 const wxGrid
* WXUNUSED(grid
),
500 const wxString
& WXUNUSED(oldval
),
503 wxCHECK_MSG( m_control
, false,
504 "wxGridCellTextEditor must be created first!" );
506 const wxString value
= Text()->GetValue();
507 if ( value
== m_value
)
518 void wxGridCellTextEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
520 grid
->GetTable()->SetValue(row
, col
, m_value
);
524 void wxGridCellTextEditor::Reset()
526 wxASSERT_MSG( m_control
, "wxGridCellTextEditor must be created first!" );
531 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
533 Text()->SetValue(startValue
);
534 Text()->SetInsertionPointEnd();
537 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
539 switch ( event
.GetKeyCode() )
546 return wxGridCellEditor::IsAcceptedKey(event
);
550 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
552 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
553 // longer an appropriate way to get the character into the text control.
554 // Do it ourselves instead. We know that if we get this far that we have
555 // a valid character, so not a whole lot of testing needs to be done.
557 wxTextCtrl
* tc
= Text();
563 ch
= event
.GetUnicodeKey();
564 if ( ch
!= WXK_NONE
)
567 #endif // wxUSE_UNICODE
569 ch
= event
.GetKeyCode();
570 isPrintable
= ch
>= WXK_SPACE
&& ch
< WXK_START
;
576 // Delete the initial character when starting to edit with DELETE.
581 // Delete the last character when starting to edit with BACKSPACE.
583 const long pos
= tc
->GetLastPosition();
584 tc
->Remove(pos
- 1, pos
);
590 tc
->WriteText(static_cast<wxChar
>(ch
));
595 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
596 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
598 #if defined(__WXMOTIF__) || defined(__WXGTK__)
599 // wxMotif needs a little extra help...
600 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
601 wxString
s( Text()->GetValue() );
602 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
604 Text()->SetInsertionPoint( pos
);
606 // the other ports can handle a Return key press
612 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
622 if ( params
.ToLong(&tmp
) )
624 m_maxChars
= (size_t)tmp
;
628 wxLogDebug( wxT("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
633 // return the value in the text control
634 wxString
wxGridCellTextEditor::GetValue() const
636 return Text()->GetValue();
639 // ----------------------------------------------------------------------------
640 // wxGridCellNumberEditor
641 // ----------------------------------------------------------------------------
643 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
649 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
651 wxEvtHandler
* evtHandler
)
656 // create a spin ctrl
657 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
658 wxDefaultPosition
, wxDefaultSize
,
662 wxGridCellEditor::Create(parent
, id
, evtHandler
);
667 // just a text control
668 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
671 Text()->SetValidator(wxIntegerValidator
<int>());
676 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
678 // first get the value
679 wxGridTableBase
*table
= grid
->GetTable();
680 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
682 m_value
= table
->GetValueAsLong(row
, col
);
687 wxString sValue
= table
->GetValue(row
, col
);
688 if (! sValue
.ToLong(&m_value
) && ! sValue
.empty())
690 wxFAIL_MSG( wxT("this cell doesn't have numeric value") );
698 Spin()->SetValue((int)m_value
);
704 DoBeginEdit(GetString());
708 bool wxGridCellNumberEditor::EndEdit(int WXUNUSED(row
),
710 const wxGrid
* WXUNUSED(grid
),
711 const wxString
& oldval
, wxString
*newval
)
719 value
= Spin()->GetValue();
720 if ( value
== m_value
)
723 text
.Printf(wxT("%ld"), value
);
725 else // using unconstrained input
726 #endif // wxUSE_SPINCTRL
728 text
= Text()->GetValue();
731 if ( oldval
.empty() )
734 else // non-empty text now (maybe 0)
736 if ( !text
.ToLong(&value
) )
739 // if value == m_value == 0 but old text was "" and new one is
740 // "0" something still did change
741 if ( value
== m_value
&& (value
|| !oldval
.empty()) )
754 void wxGridCellNumberEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
756 wxGridTableBase
* const table
= grid
->GetTable();
757 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
758 table
->SetValueAsLong(row
, col
, m_value
);
760 table
->SetValue(row
, col
, wxString::Format("%ld", m_value
));
763 void wxGridCellNumberEditor::Reset()
768 Spin()->SetValue((int)m_value
);
773 DoReset(GetString());
777 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
779 if ( wxGridCellEditor::IsAcceptedKey(event
) )
781 int keycode
= event
.GetKeyCode();
782 if ( (keycode
< 128) &&
783 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
792 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
794 int keycode
= event
.GetKeyCode();
797 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
799 wxGridCellTextEditor::StartingKey(event
);
808 if ( wxIsdigit(keycode
) )
810 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
811 spin
->SetValue(keycode
- '0');
812 spin
->SetSelection(1,1);
821 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
832 if ( params
.BeforeFirst(wxT(',')).ToLong(&tmp
) )
836 if ( params
.AfterFirst(wxT(',')).ToLong(&tmp
) )
840 // skip the error message below
845 wxLogDebug(wxT("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
849 // return the value in the spin control if it is there (the text control otherwise)
850 wxString
wxGridCellNumberEditor::GetValue() const
857 long value
= Spin()->GetValue();
858 s
.Printf(wxT("%ld"), value
);
863 s
= Text()->GetValue();
869 // ----------------------------------------------------------------------------
870 // wxGridCellFloatEditor
871 // ----------------------------------------------------------------------------
873 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
,
878 m_precision
= precision
;
882 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
884 wxEvtHandler
* evtHandler
)
886 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
889 Text()->SetValidator(wxFloatingPointValidator
<double>(m_precision
));
893 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
895 // first get the value
896 wxGridTableBase
* const table
= grid
->GetTable();
897 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
899 m_value
= table
->GetValueAsDouble(row
, col
);
905 const wxString value
= table
->GetValue(row
, col
);
906 if ( !value
.empty() )
908 if ( !value
.ToDouble(&m_value
) )
910 wxFAIL_MSG( wxT("this cell doesn't have float value") );
916 DoBeginEdit(GetString());
919 bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row
),
921 const wxGrid
* WXUNUSED(grid
),
922 const wxString
& oldval
, wxString
*newval
)
924 const wxString
text(Text()->GetValue());
929 if ( !text
.ToDouble(&value
) )
932 else // new value is empty string
934 if ( oldval
.empty() )
935 return false; // nothing changed
940 // the test for empty strings ensures that we don't skip the value setting
941 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
942 if ( wxIsSameDouble(value
, m_value
) && !text
.empty() && !oldval
.empty() )
943 return false; // nothing changed
953 void wxGridCellFloatEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
955 wxGridTableBase
* const table
= grid
->GetTable();
957 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
958 table
->SetValueAsDouble(row
, col
, m_value
);
960 table
->SetValue(row
, col
, Text()->GetValue());
963 void wxGridCellFloatEditor::Reset()
965 DoReset(GetString());
968 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
970 int keycode
= event
.GetKeyCode();
972 tmpbuf
[0] = (char) keycode
;
974 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
977 bool is_decimal_point
= ( strbuf
==
978 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
980 bool is_decimal_point
= ( strbuf
== wxT(".") );
983 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
984 || is_decimal_point
)
986 wxGridCellTextEditor::StartingKey(event
);
995 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1002 m_style
= wxGRID_FLOAT_FORMAT_DEFAULT
;
1008 wxString tmp
= params
.BeforeFirst(wxT(','), &rest
);
1012 if ( tmp
.ToLong(&width
) )
1014 m_width
= (int)width
;
1018 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1022 tmp
= rest
.BeforeFirst(wxT(','));
1026 if ( tmp
.ToLong(&precision
) )
1028 m_precision
= (int)precision
;
1032 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1036 tmp
= rest
.AfterFirst(wxT(','));
1039 if ( tmp
[0] == wxT('f') )
1041 m_style
= wxGRID_FLOAT_FORMAT_FIXED
;
1043 else if ( tmp
[0] == wxT('e') )
1045 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
;
1047 else if ( tmp
[0] == wxT('g') )
1049 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
;
1051 else if ( tmp
[0] == wxT('E') )
1053 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
|
1054 wxGRID_FLOAT_FORMAT_UPPER
;
1056 else if ( tmp
[0] == wxT('F') )
1058 m_style
= wxGRID_FLOAT_FORMAT_FIXED
|
1059 wxGRID_FLOAT_FORMAT_UPPER
;
1061 else if ( tmp
[0] == wxT('G') )
1063 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
|
1064 wxGRID_FLOAT_FORMAT_UPPER
;
1068 wxLogDebug("Invalid wxGridCellFloatRenderer format "
1069 "parameter string '%s ignored", params
);
1075 wxString
wxGridCellFloatEditor::GetString()
1079 if ( m_precision
== -1 && m_width
!= -1)
1081 // default precision
1082 m_format
.Printf(wxT("%%%d."), m_width
);
1084 else if ( m_precision
!= -1 && m_width
== -1)
1087 m_format
.Printf(wxT("%%.%d"), m_precision
);
1089 else if ( m_precision
!= -1 && m_width
!= -1 )
1091 m_format
.Printf(wxT("%%%d.%d"), m_width
, m_precision
);
1095 // default width/precision
1096 m_format
= wxT("%");
1099 bool isUpper
= (m_style
& wxGRID_FLOAT_FORMAT_UPPER
) != 0;
1100 if ( m_style
& wxGRID_FLOAT_FORMAT_SCIENTIFIC
)
1101 m_format
+= isUpper
? wxT('E') : wxT('e');
1102 else if ( m_style
& wxGRID_FLOAT_FORMAT_COMPACT
)
1103 m_format
+= isUpper
? wxT('G') : wxT('g');
1105 m_format
+= wxT('f');
1108 return wxString::Format(m_format
, m_value
);
1111 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1113 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1115 const int keycode
= event
.GetKeyCode();
1116 if ( wxIsascii(keycode
) )
1119 const wxString decimalPoint
=
1120 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1122 const wxString
decimalPoint(wxT('.'));
1125 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1126 if ( wxIsdigit(keycode
) ||
1127 tolower(keycode
) == 'e' ||
1128 keycode
== decimalPoint
||
1140 #endif // wxUSE_TEXTCTRL
1144 // ----------------------------------------------------------------------------
1145 // wxGridCellBoolEditor
1146 // ----------------------------------------------------------------------------
1148 // the default values for GetValue()
1149 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { wxT(""), wxT("1") };
1151 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1153 wxEvtHandler
* evtHandler
)
1155 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1156 wxDefaultPosition
, wxDefaultSize
,
1159 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1162 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1164 bool resize
= false;
1165 wxSize size
= m_control
->GetSize();
1166 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1168 // check if the checkbox is not too big/small for this cell
1169 wxSize sizeBest
= m_control
->GetBestSize();
1170 if ( !(size
== sizeBest
) )
1172 // reset to default size if it had been made smaller
1178 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1180 // leave 1 pixel margin
1181 size
.x
= size
.y
= minSize
- 2;
1188 m_control
->SetSize(size
);
1191 // position it in the centre of the rectangle (TODO: support alignment?)
1193 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1194 // the checkbox without label still has some space to the right in wxGTK,
1195 // so shift it to the right
1197 #elif defined(__WXMSW__)
1198 // here too, but in other way
1203 int hAlign
= wxALIGN_CENTRE
;
1204 int vAlign
= wxALIGN_CENTRE
;
1206 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1209 if (hAlign
== wxALIGN_LEFT
)
1217 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1219 else if (hAlign
== wxALIGN_RIGHT
)
1221 x
= r
.x
+ r
.width
- size
.x
- 2;
1222 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1224 else if (hAlign
== wxALIGN_CENTRE
)
1226 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1227 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1230 m_control
->Move(x
, y
);
1233 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1235 m_control
->Show(show
);
1239 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1240 CBox()->SetBackgroundColour(colBg
);
1244 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1246 wxASSERT_MSG(m_control
,
1247 wxT("The wxGridCellEditor must be created first!"));
1249 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1251 m_value
= grid
->GetTable()->GetValueAsBool(row
, col
);
1255 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1257 if ( cellval
== ms_stringValues
[false] )
1259 else if ( cellval
== ms_stringValues
[true] )
1263 // do not try to be smart here and convert it to true or false
1264 // because we'll still overwrite it with something different and
1265 // this risks to be very surprising for the user code, let them
1267 wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") );
1271 CBox()->SetValue(m_value
);
1275 bool wxGridCellBoolEditor::EndEdit(int WXUNUSED(row
),
1277 const wxGrid
* WXUNUSED(grid
),
1278 const wxString
& WXUNUSED(oldval
),
1281 bool value
= CBox()->GetValue();
1282 if ( value
== m_value
)
1288 *newval
= GetValue();
1293 void wxGridCellBoolEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1295 wxGridTableBase
* const table
= grid
->GetTable();
1296 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1297 table
->SetValueAsBool(row
, col
, m_value
);
1299 table
->SetValue(row
, col
, GetValue());
1302 void wxGridCellBoolEditor::Reset()
1304 wxASSERT_MSG(m_control
,
1305 wxT("The wxGridCellEditor must be created first!"));
1307 CBox()->SetValue(m_value
);
1310 void wxGridCellBoolEditor::StartingClick()
1312 CBox()->SetValue(!CBox()->GetValue());
1315 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1317 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1319 int keycode
= event
.GetKeyCode();
1332 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1334 int keycode
= event
.GetKeyCode();
1338 CBox()->SetValue(!CBox()->GetValue());
1342 CBox()->SetValue(true);
1346 CBox()->SetValue(false);
1351 wxString
wxGridCellBoolEditor::GetValue() const
1353 return ms_stringValues
[CBox()->GetValue()];
1357 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1358 const wxString
& valueFalse
)
1360 ms_stringValues
[false] = valueFalse
;
1361 ms_stringValues
[true] = valueTrue
;
1365 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1367 return value
== ms_stringValues
[true];
1370 #endif // wxUSE_CHECKBOX
1374 // ----------------------------------------------------------------------------
1375 // wxGridCellChoiceEditor
1376 // ----------------------------------------------------------------------------
1378 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1380 : m_choices(choices
),
1381 m_allowOthers(allowOthers
) { }
1383 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1384 const wxString choices
[],
1386 : m_allowOthers(allowOthers
)
1390 m_choices
.Alloc(count
);
1391 for ( size_t n
= 0; n
< count
; n
++ )
1393 m_choices
.Add(choices
[n
]);
1398 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1400 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1401 editor
->m_allowOthers
= m_allowOthers
;
1402 editor
->m_choices
= m_choices
;
1407 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1409 wxEvtHandler
* evtHandler
)
1411 int style
= wxTE_PROCESS_ENTER
|
1415 if ( !m_allowOthers
)
1416 style
|= wxCB_READONLY
;
1417 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1418 wxDefaultPosition
, wxDefaultSize
,
1422 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1425 void wxGridCellChoiceEditor::SetSize(const wxRect
& rect
)
1427 wxASSERT_MSG(m_control
,
1428 wxT("The wxGridCellChoiceEditor must be created first!"));
1430 // Check that the height is not too small to fit the combobox.
1431 wxRect rectTallEnough
= rect
;
1432 const wxSize bestSize
= m_control
->GetBestSize();
1433 const wxCoord diffY
= bestSize
.GetHeight() - rectTallEnough
.GetHeight();
1436 // Do make it tall enough.
1437 rectTallEnough
.height
+= diffY
;
1439 // Also centre the effective rectangle vertically with respect to the
1441 rectTallEnough
.y
-= diffY
/2;
1443 //else: The rectangle provided is already tall enough.
1445 wxGridCellEditor::SetSize(rectTallEnough
);
1448 void wxGridCellChoiceEditor::PaintBackground(wxDC
& dc
,
1449 const wxRect
& rectCell
,
1450 const wxGridCellAttr
& attr
)
1452 // as we fill the entire client area, don't do anything here to minimize
1455 // TODO: It doesn't actually fill the client area since the height of a
1456 // combo always defaults to the standard. Until someone has time to
1457 // figure out the right rectangle to paint, just do it the normal way.
1458 wxGridCellEditor::PaintBackground(dc
, rectCell
, attr
);
1461 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1463 wxASSERT_MSG(m_control
,
1464 wxT("The wxGridCellEditor must be created first!"));
1466 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1468 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1470 // Don't immediately end if we get a kill focus event within BeginEdit
1472 evtHandler
->SetInSetFocus(true);
1474 m_value
= grid
->GetTable()->GetValue(row
, col
);
1476 Reset(); // this updates combo box to correspond to m_value
1478 Combo()->SetFocus();
1480 #ifdef __WXOSX_COCOA__
1481 // This is a work around for the combobox being simply dismissed when a
1482 // choice is made in it under OS X. The bug is almost certainly due to a
1483 // problem in focus events generation logic but it's not obvious to fix and
1484 // for now this at least allows to use wxGrid.
1490 // When dropping down the menu, a kill focus event
1491 // happens after this point, so we can't reset the flag yet.
1492 #if !defined(__WXGTK20__)
1493 evtHandler
->SetInSetFocus(false);
1498 bool wxGridCellChoiceEditor::EndEdit(int WXUNUSED(row
),
1500 const wxGrid
* WXUNUSED(grid
),
1501 const wxString
& WXUNUSED(oldval
),
1504 const wxString value
= Combo()->GetValue();
1505 if ( value
== m_value
)
1516 void wxGridCellChoiceEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1518 grid
->GetTable()->SetValue(row
, col
, m_value
);
1521 void wxGridCellChoiceEditor::Reset()
1525 Combo()->SetValue(m_value
);
1526 Combo()->SetInsertionPointEnd();
1528 else // the combobox is read-only
1530 // find the right position, or default to the first if not found
1531 int pos
= Combo()->FindString(m_value
);
1532 if (pos
== wxNOT_FOUND
)
1534 Combo()->SetSelection(pos
);
1538 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1548 wxStringTokenizer
tk(params
, wxT(','));
1549 while ( tk
.HasMoreTokens() )
1551 m_choices
.Add(tk
.GetNextToken());
1555 // return the value in the text control
1556 wxString
wxGridCellChoiceEditor::GetValue() const
1558 return Combo()->GetValue();
1561 #endif // wxUSE_COMBOBOX
1565 // ----------------------------------------------------------------------------
1566 // wxGridCellEnumEditor
1567 // ----------------------------------------------------------------------------
1569 // A cell editor which displays an enum number as a textual equivalent. eg
1570 // data in cell is 0,1,2 ... n the cell could be displayed as
1571 // "John","Fred"..."Bob" in the combo choice box
1573 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
1574 :wxGridCellChoiceEditor()
1578 if (!choices
.empty())
1579 SetParameters(choices
);
1582 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
1584 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
1585 editor
->m_index
= m_index
;
1589 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1591 wxASSERT_MSG(m_control
,
1592 wxT("The wxGridCellEnumEditor must be Created first!"));
1594 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1596 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1598 // Don't immediately end if we get a kill focus event within BeginEdit
1600 evtHandler
->SetInSetFocus(true);
1602 wxGridTableBase
*table
= grid
->GetTable();
1604 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1606 m_index
= table
->GetValueAsLong(row
, col
);
1610 wxString startValue
= table
->GetValue(row
, col
);
1611 if (startValue
.IsNumber() && !startValue
.empty())
1613 startValue
.ToLong(&m_index
);
1621 Combo()->SetSelection(m_index
);
1622 Combo()->SetFocus();
1624 #ifdef __WXOSX_COCOA__
1625 // This is a work around for the combobox being simply dismissed when a
1626 // choice is made in it under OS X. The bug is almost certainly due to a
1627 // problem in focus events generation logic but it's not obvious to fix and
1628 // for now this at least allows to use wxGrid.
1634 // When dropping down the menu, a kill focus event
1635 // happens after this point, so we can't reset the flag yet.
1636 #if !defined(__WXGTK20__)
1637 evtHandler
->SetInSetFocus(false);
1642 bool wxGridCellEnumEditor::EndEdit(int WXUNUSED(row
),
1644 const wxGrid
* WXUNUSED(grid
),
1645 const wxString
& WXUNUSED(oldval
),
1648 long idx
= Combo()->GetSelection();
1649 if ( idx
== m_index
)
1655 newval
->Printf("%ld", m_index
);
1660 void wxGridCellEnumEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1662 wxGridTableBase
* const table
= grid
->GetTable();
1663 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1664 table
->SetValueAsLong(row
, col
, m_index
);
1666 table
->SetValue(row
, col
, wxString::Format("%ld", m_index
));
1669 #endif // wxUSE_COMBOBOX
1671 // ----------------------------------------------------------------------------
1672 // wxGridCellAutoWrapStringEditor
1673 // ----------------------------------------------------------------------------
1676 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
1678 wxEvtHandler
* evtHandler
)
1680 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
1681 wxTE_MULTILINE
| wxTE_RICH
);
1685 #endif // wxUSE_GRID