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 wxTextCtrl
* const text
= new wxTextCtrl(parent
, id
, wxEmptyString
,
403 wxDefaultPosition
, wxDefaultSize
,
405 text
->SetMargins(0, 0);
409 wxWidgetImpl
* impl
= m_control
->GetPeer();
410 impl
->SetNeedsFocusRect(false);
412 // set max length allowed in the textctrl, if the parameter was set
413 if ( m_maxChars
!= 0 )
415 Text()->SetMaxLength(m_maxChars
);
418 wxGridCellEditor::Create(parent
, id
, evtHandler
);
421 void wxGridCellTextEditor::PaintBackground(wxDC
& WXUNUSED(dc
),
422 const wxRect
& WXUNUSED(rectCell
),
423 const wxGridCellAttr
& WXUNUSED(attr
))
425 // as we fill the entire client area,
426 // don't do anything here to minimize flicker
429 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
431 wxRect
rect(rectOrig
);
433 // Make the edit control large enough to allow for internal margins
435 // TODO: remove this if the text ctrl sizing is improved esp. for unix
437 #if defined(__WXGTK__)
445 #elif defined(__WXMSW__)
458 #elif defined(__WXOSX__)
465 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
466 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
468 #if defined(__WXMOTIF__)
473 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
474 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
475 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
476 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
479 wxGridCellEditor::SetSize(rect
);
482 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
484 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
486 m_value
= grid
->GetTable()->GetValue(row
, col
);
488 DoBeginEdit(m_value
);
491 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
493 Text()->SetValue(startValue
);
494 Text()->SetInsertionPointEnd();
499 bool wxGridCellTextEditor::EndEdit(int WXUNUSED(row
),
501 const wxGrid
* WXUNUSED(grid
),
502 const wxString
& WXUNUSED(oldval
),
505 wxCHECK_MSG( m_control
, false,
506 "wxGridCellTextEditor must be created first!" );
508 const wxString value
= Text()->GetValue();
509 if ( value
== m_value
)
520 void wxGridCellTextEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
522 grid
->GetTable()->SetValue(row
, col
, m_value
);
526 void wxGridCellTextEditor::Reset()
528 wxASSERT_MSG( m_control
, "wxGridCellTextEditor must be created first!" );
533 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
535 Text()->SetValue(startValue
);
536 Text()->SetInsertionPointEnd();
539 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
541 switch ( event
.GetKeyCode() )
548 return wxGridCellEditor::IsAcceptedKey(event
);
552 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
554 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
555 // longer an appropriate way to get the character into the text control.
556 // Do it ourselves instead. We know that if we get this far that we have
557 // a valid character, so not a whole lot of testing needs to be done.
559 wxTextCtrl
* tc
= Text();
565 ch
= event
.GetUnicodeKey();
566 if ( ch
!= WXK_NONE
)
569 #endif // wxUSE_UNICODE
571 ch
= event
.GetKeyCode();
572 isPrintable
= ch
>= WXK_SPACE
&& ch
< WXK_START
;
578 // Delete the initial character when starting to edit with DELETE.
583 // Delete the last character when starting to edit with BACKSPACE.
585 const long pos
= tc
->GetLastPosition();
586 tc
->Remove(pos
- 1, pos
);
592 tc
->WriteText(static_cast<wxChar
>(ch
));
597 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
598 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
600 #if defined(__WXMOTIF__) || defined(__WXGTK__)
601 // wxMotif needs a little extra help...
602 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
603 wxString
s( Text()->GetValue() );
604 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
606 Text()->SetInsertionPoint( pos
);
608 // the other ports can handle a Return key press
614 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
624 if ( params
.ToLong(&tmp
) )
626 m_maxChars
= (size_t)tmp
;
630 wxLogDebug( wxT("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
635 // return the value in the text control
636 wxString
wxGridCellTextEditor::GetValue() const
638 return Text()->GetValue();
641 // ----------------------------------------------------------------------------
642 // wxGridCellNumberEditor
643 // ----------------------------------------------------------------------------
645 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
651 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
653 wxEvtHandler
* evtHandler
)
658 // create a spin ctrl
659 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
660 wxDefaultPosition
, wxDefaultSize
,
664 wxGridCellEditor::Create(parent
, id
, evtHandler
);
669 // just a text control
670 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
673 Text()->SetValidator(wxIntegerValidator
<int>());
678 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
680 // first get the value
681 wxGridTableBase
*table
= grid
->GetTable();
682 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
684 m_value
= table
->GetValueAsLong(row
, col
);
689 wxString sValue
= table
->GetValue(row
, col
);
690 if (! sValue
.ToLong(&m_value
) && ! sValue
.empty())
692 wxFAIL_MSG( wxT("this cell doesn't have numeric value") );
700 Spin()->SetValue((int)m_value
);
706 DoBeginEdit(GetString());
710 bool wxGridCellNumberEditor::EndEdit(int WXUNUSED(row
),
712 const wxGrid
* WXUNUSED(grid
),
713 const wxString
& oldval
, wxString
*newval
)
721 value
= Spin()->GetValue();
722 if ( value
== m_value
)
725 text
.Printf(wxT("%ld"), value
);
727 else // using unconstrained input
728 #endif // wxUSE_SPINCTRL
730 text
= Text()->GetValue();
733 if ( oldval
.empty() )
736 else // non-empty text now (maybe 0)
738 if ( !text
.ToLong(&value
) )
741 // if value == m_value == 0 but old text was "" and new one is
742 // "0" something still did change
743 if ( value
== m_value
&& (value
|| !oldval
.empty()) )
756 void wxGridCellNumberEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
758 wxGridTableBase
* const table
= grid
->GetTable();
759 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
760 table
->SetValueAsLong(row
, col
, m_value
);
762 table
->SetValue(row
, col
, wxString::Format("%ld", m_value
));
765 void wxGridCellNumberEditor::Reset()
770 Spin()->SetValue((int)m_value
);
775 DoReset(GetString());
779 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
781 if ( wxGridCellEditor::IsAcceptedKey(event
) )
783 int keycode
= event
.GetKeyCode();
784 if ( (keycode
< 128) &&
785 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
794 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
796 int keycode
= event
.GetKeyCode();
799 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
801 wxGridCellTextEditor::StartingKey(event
);
810 if ( wxIsdigit(keycode
) )
812 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
813 spin
->SetValue(keycode
- '0');
814 spin
->SetSelection(1,1);
823 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
834 if ( params
.BeforeFirst(wxT(',')).ToLong(&tmp
) )
838 if ( params
.AfterFirst(wxT(',')).ToLong(&tmp
) )
842 // skip the error message below
847 wxLogDebug(wxT("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
851 // return the value in the spin control if it is there (the text control otherwise)
852 wxString
wxGridCellNumberEditor::GetValue() const
859 long value
= Spin()->GetValue();
860 s
.Printf(wxT("%ld"), value
);
865 s
= Text()->GetValue();
871 // ----------------------------------------------------------------------------
872 // wxGridCellFloatEditor
873 // ----------------------------------------------------------------------------
875 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
,
880 m_precision
= precision
;
884 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
886 wxEvtHandler
* evtHandler
)
888 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
891 Text()->SetValidator(wxFloatingPointValidator
<double>(m_precision
));
895 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
897 // first get the value
898 wxGridTableBase
* const table
= grid
->GetTable();
899 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
901 m_value
= table
->GetValueAsDouble(row
, col
);
907 const wxString value
= table
->GetValue(row
, col
);
908 if ( !value
.empty() )
910 if ( !value
.ToDouble(&m_value
) )
912 wxFAIL_MSG( wxT("this cell doesn't have float value") );
918 DoBeginEdit(GetString());
921 bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row
),
923 const wxGrid
* WXUNUSED(grid
),
924 const wxString
& oldval
, wxString
*newval
)
926 const wxString
text(Text()->GetValue());
931 if ( !text
.ToDouble(&value
) )
934 else // new value is empty string
936 if ( oldval
.empty() )
937 return false; // nothing changed
942 // the test for empty strings ensures that we don't skip the value setting
943 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
944 if ( wxIsSameDouble(value
, m_value
) && !text
.empty() && !oldval
.empty() )
945 return false; // nothing changed
955 void wxGridCellFloatEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
957 wxGridTableBase
* const table
= grid
->GetTable();
959 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
960 table
->SetValueAsDouble(row
, col
, m_value
);
962 table
->SetValue(row
, col
, Text()->GetValue());
965 void wxGridCellFloatEditor::Reset()
967 DoReset(GetString());
970 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
972 int keycode
= event
.GetKeyCode();
974 tmpbuf
[0] = (char) keycode
;
976 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
979 bool is_decimal_point
= ( strbuf
==
980 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
982 bool is_decimal_point
= ( strbuf
== wxT(".") );
985 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
986 || is_decimal_point
)
988 wxGridCellTextEditor::StartingKey(event
);
997 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1004 m_style
= wxGRID_FLOAT_FORMAT_DEFAULT
;
1010 wxString tmp
= params
.BeforeFirst(wxT(','), &rest
);
1014 if ( tmp
.ToLong(&width
) )
1016 m_width
= (int)width
;
1020 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1024 tmp
= rest
.BeforeFirst(wxT(','));
1028 if ( tmp
.ToLong(&precision
) )
1030 m_precision
= (int)precision
;
1034 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1038 tmp
= rest
.AfterFirst(wxT(','));
1041 if ( tmp
[0] == wxT('f') )
1043 m_style
= wxGRID_FLOAT_FORMAT_FIXED
;
1045 else if ( tmp
[0] == wxT('e') )
1047 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
;
1049 else if ( tmp
[0] == wxT('g') )
1051 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
;
1053 else if ( tmp
[0] == wxT('E') )
1055 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
|
1056 wxGRID_FLOAT_FORMAT_UPPER
;
1058 else if ( tmp
[0] == wxT('F') )
1060 m_style
= wxGRID_FLOAT_FORMAT_FIXED
|
1061 wxGRID_FLOAT_FORMAT_UPPER
;
1063 else if ( tmp
[0] == wxT('G') )
1065 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
|
1066 wxGRID_FLOAT_FORMAT_UPPER
;
1070 wxLogDebug("Invalid wxGridCellFloatRenderer format "
1071 "parameter string '%s ignored", params
);
1077 wxString
wxGridCellFloatEditor::GetString()
1081 if ( m_precision
== -1 && m_width
!= -1)
1083 // default precision
1084 m_format
.Printf(wxT("%%%d."), m_width
);
1086 else if ( m_precision
!= -1 && m_width
== -1)
1089 m_format
.Printf(wxT("%%.%d"), m_precision
);
1091 else if ( m_precision
!= -1 && m_width
!= -1 )
1093 m_format
.Printf(wxT("%%%d.%d"), m_width
, m_precision
);
1097 // default width/precision
1098 m_format
= wxT("%");
1101 bool isUpper
= (m_style
& wxGRID_FLOAT_FORMAT_UPPER
) != 0;
1102 if ( m_style
& wxGRID_FLOAT_FORMAT_SCIENTIFIC
)
1103 m_format
+= isUpper
? wxT('E') : wxT('e');
1104 else if ( m_style
& wxGRID_FLOAT_FORMAT_COMPACT
)
1105 m_format
+= isUpper
? wxT('G') : wxT('g');
1107 m_format
+= wxT('f');
1110 return wxString::Format(m_format
, m_value
);
1113 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1115 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1117 const int keycode
= event
.GetKeyCode();
1118 if ( wxIsascii(keycode
) )
1121 const wxString decimalPoint
=
1122 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1124 const wxString
decimalPoint(wxT('.'));
1127 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1128 if ( wxIsdigit(keycode
) ||
1129 tolower(keycode
) == 'e' ||
1130 keycode
== decimalPoint
||
1142 #endif // wxUSE_TEXTCTRL
1146 // ----------------------------------------------------------------------------
1147 // wxGridCellBoolEditor
1148 // ----------------------------------------------------------------------------
1150 // the default values for GetValue()
1151 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { wxT(""), wxT("1") };
1153 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1155 wxEvtHandler
* evtHandler
)
1157 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1158 wxDefaultPosition
, wxDefaultSize
,
1161 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1164 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1166 bool resize
= false;
1167 wxSize size
= m_control
->GetSize();
1168 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1170 // check if the checkbox is not too big/small for this cell
1171 wxSize sizeBest
= m_control
->GetBestSize();
1172 if ( !(size
== sizeBest
) )
1174 // reset to default size if it had been made smaller
1180 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1182 // leave 1 pixel margin
1183 size
.x
= size
.y
= minSize
- 2;
1190 m_control
->SetSize(size
);
1193 // position it in the centre of the rectangle (TODO: support alignment?)
1195 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1196 // the checkbox without label still has some space to the right in wxGTK,
1197 // so shift it to the right
1199 #elif defined(__WXMSW__)
1200 // here too, but in other way
1205 int hAlign
= wxALIGN_CENTRE
;
1206 int vAlign
= wxALIGN_CENTRE
;
1208 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1211 if (hAlign
== wxALIGN_LEFT
)
1219 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1221 else if (hAlign
== wxALIGN_RIGHT
)
1223 x
= r
.x
+ r
.width
- size
.x
- 2;
1224 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1226 else if (hAlign
== wxALIGN_CENTRE
)
1228 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1229 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1232 m_control
->Move(x
, y
);
1235 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1237 m_control
->Show(show
);
1241 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1242 CBox()->SetBackgroundColour(colBg
);
1246 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1248 wxASSERT_MSG(m_control
,
1249 wxT("The wxGridCellEditor must be created first!"));
1251 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1253 m_value
= grid
->GetTable()->GetValueAsBool(row
, col
);
1257 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1259 if ( cellval
== ms_stringValues
[false] )
1261 else if ( cellval
== ms_stringValues
[true] )
1265 // do not try to be smart here and convert it to true or false
1266 // because we'll still overwrite it with something different and
1267 // this risks to be very surprising for the user code, let them
1269 wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") );
1273 CBox()->SetValue(m_value
);
1277 bool wxGridCellBoolEditor::EndEdit(int WXUNUSED(row
),
1279 const wxGrid
* WXUNUSED(grid
),
1280 const wxString
& WXUNUSED(oldval
),
1283 bool value
= CBox()->GetValue();
1284 if ( value
== m_value
)
1290 *newval
= GetValue();
1295 void wxGridCellBoolEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1297 wxGridTableBase
* const table
= grid
->GetTable();
1298 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1299 table
->SetValueAsBool(row
, col
, m_value
);
1301 table
->SetValue(row
, col
, GetValue());
1304 void wxGridCellBoolEditor::Reset()
1306 wxASSERT_MSG(m_control
,
1307 wxT("The wxGridCellEditor must be created first!"));
1309 CBox()->SetValue(m_value
);
1312 void wxGridCellBoolEditor::StartingClick()
1314 CBox()->SetValue(!CBox()->GetValue());
1317 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1319 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1321 int keycode
= event
.GetKeyCode();
1334 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1336 int keycode
= event
.GetKeyCode();
1340 CBox()->SetValue(!CBox()->GetValue());
1344 CBox()->SetValue(true);
1348 CBox()->SetValue(false);
1353 wxString
wxGridCellBoolEditor::GetValue() const
1355 return ms_stringValues
[CBox()->GetValue()];
1359 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1360 const wxString
& valueFalse
)
1362 ms_stringValues
[false] = valueFalse
;
1363 ms_stringValues
[true] = valueTrue
;
1367 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1369 return value
== ms_stringValues
[true];
1372 #endif // wxUSE_CHECKBOX
1376 // ----------------------------------------------------------------------------
1377 // wxGridCellChoiceEditor
1378 // ----------------------------------------------------------------------------
1380 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1382 : m_choices(choices
),
1383 m_allowOthers(allowOthers
) { }
1385 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1386 const wxString choices
[],
1388 : m_allowOthers(allowOthers
)
1392 m_choices
.Alloc(count
);
1393 for ( size_t n
= 0; n
< count
; n
++ )
1395 m_choices
.Add(choices
[n
]);
1400 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1402 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1403 editor
->m_allowOthers
= m_allowOthers
;
1404 editor
->m_choices
= m_choices
;
1409 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1411 wxEvtHandler
* evtHandler
)
1413 int style
= wxTE_PROCESS_ENTER
|
1417 if ( !m_allowOthers
)
1418 style
|= wxCB_READONLY
;
1419 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1420 wxDefaultPosition
, wxDefaultSize
,
1424 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1427 void wxGridCellChoiceEditor::SetSize(const wxRect
& rect
)
1429 wxASSERT_MSG(m_control
,
1430 wxT("The wxGridCellChoiceEditor must be created first!"));
1432 // Check that the height is not too small to fit the combobox.
1433 wxRect rectTallEnough
= rect
;
1434 const wxSize bestSize
= m_control
->GetBestSize();
1435 const wxCoord diffY
= bestSize
.GetHeight() - rectTallEnough
.GetHeight();
1438 // Do make it tall enough.
1439 rectTallEnough
.height
+= diffY
;
1441 // Also centre the effective rectangle vertically with respect to the
1443 rectTallEnough
.y
-= diffY
/2;
1445 //else: The rectangle provided is already tall enough.
1447 wxGridCellEditor::SetSize(rectTallEnough
);
1450 void wxGridCellChoiceEditor::PaintBackground(wxDC
& dc
,
1451 const wxRect
& rectCell
,
1452 const wxGridCellAttr
& attr
)
1454 // as we fill the entire client area, don't do anything here to minimize
1457 // TODO: It doesn't actually fill the client area since the height of a
1458 // combo always defaults to the standard. Until someone has time to
1459 // figure out the right rectangle to paint, just do it the normal way.
1460 wxGridCellEditor::PaintBackground(dc
, rectCell
, attr
);
1463 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1465 wxASSERT_MSG(m_control
,
1466 wxT("The wxGridCellEditor must be created first!"));
1468 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1470 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1472 // Don't immediately end if we get a kill focus event within BeginEdit
1474 evtHandler
->SetInSetFocus(true);
1476 m_value
= grid
->GetTable()->GetValue(row
, col
);
1478 Reset(); // this updates combo box to correspond to m_value
1480 Combo()->SetFocus();
1482 #ifdef __WXOSX_COCOA__
1483 // This is a work around for the combobox being simply dismissed when a
1484 // choice is made in it under OS X. The bug is almost certainly due to a
1485 // problem in focus events generation logic but it's not obvious to fix and
1486 // for now this at least allows to use wxGrid.
1492 // When dropping down the menu, a kill focus event
1493 // happens after this point, so we can't reset the flag yet.
1494 #if !defined(__WXGTK20__)
1495 evtHandler
->SetInSetFocus(false);
1500 bool wxGridCellChoiceEditor::EndEdit(int WXUNUSED(row
),
1502 const wxGrid
* WXUNUSED(grid
),
1503 const wxString
& WXUNUSED(oldval
),
1506 const wxString value
= Combo()->GetValue();
1507 if ( value
== m_value
)
1518 void wxGridCellChoiceEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1520 grid
->GetTable()->SetValue(row
, col
, m_value
);
1523 void wxGridCellChoiceEditor::Reset()
1527 Combo()->SetValue(m_value
);
1528 Combo()->SetInsertionPointEnd();
1530 else // the combobox is read-only
1532 // find the right position, or default to the first if not found
1533 int pos
= Combo()->FindString(m_value
);
1534 if (pos
== wxNOT_FOUND
)
1536 Combo()->SetSelection(pos
);
1540 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1550 wxStringTokenizer
tk(params
, wxT(','));
1551 while ( tk
.HasMoreTokens() )
1553 m_choices
.Add(tk
.GetNextToken());
1557 // return the value in the text control
1558 wxString
wxGridCellChoiceEditor::GetValue() const
1560 return Combo()->GetValue();
1563 #endif // wxUSE_COMBOBOX
1567 // ----------------------------------------------------------------------------
1568 // wxGridCellEnumEditor
1569 // ----------------------------------------------------------------------------
1571 // A cell editor which displays an enum number as a textual equivalent. eg
1572 // data in cell is 0,1,2 ... n the cell could be displayed as
1573 // "John","Fred"..."Bob" in the combo choice box
1575 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
1576 :wxGridCellChoiceEditor()
1580 if (!choices
.empty())
1581 SetParameters(choices
);
1584 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
1586 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
1587 editor
->m_index
= m_index
;
1591 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1593 wxASSERT_MSG(m_control
,
1594 wxT("The wxGridCellEnumEditor must be Created first!"));
1596 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1598 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1600 // Don't immediately end if we get a kill focus event within BeginEdit
1602 evtHandler
->SetInSetFocus(true);
1604 wxGridTableBase
*table
= grid
->GetTable();
1606 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1608 m_index
= table
->GetValueAsLong(row
, col
);
1612 wxString startValue
= table
->GetValue(row
, col
);
1613 if (startValue
.IsNumber() && !startValue
.empty())
1615 startValue
.ToLong(&m_index
);
1623 Combo()->SetSelection(m_index
);
1624 Combo()->SetFocus();
1626 #ifdef __WXOSX_COCOA__
1627 // This is a work around for the combobox being simply dismissed when a
1628 // choice is made in it under OS X. The bug is almost certainly due to a
1629 // problem in focus events generation logic but it's not obvious to fix and
1630 // for now this at least allows to use wxGrid.
1636 // When dropping down the menu, a kill focus event
1637 // happens after this point, so we can't reset the flag yet.
1638 #if !defined(__WXGTK20__)
1639 evtHandler
->SetInSetFocus(false);
1644 bool wxGridCellEnumEditor::EndEdit(int WXUNUSED(row
),
1646 const wxGrid
* WXUNUSED(grid
),
1647 const wxString
& WXUNUSED(oldval
),
1650 long idx
= Combo()->GetSelection();
1651 if ( idx
== m_index
)
1657 newval
->Printf("%ld", m_index
);
1662 void wxGridCellEnumEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1664 wxGridTableBase
* const table
= grid
->GetTable();
1665 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1666 table
->SetValueAsLong(row
, col
, m_index
);
1668 table
->SetValue(row
, col
, wxString::Format("%ld", m_index
));
1671 #endif // wxUSE_COMBOBOX
1673 // ----------------------------------------------------------------------------
1674 // wxGridCellAutoWrapStringEditor
1675 // ----------------------------------------------------------------------------
1678 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
1680 wxEvtHandler
* evtHandler
)
1682 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
1683 wxTE_MULTILINE
| wxTE_RICH
);
1687 #endif // wxUSE_GRID