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 wxDEFINE_EVENT( wxEVT_GRID_HIDE_EDITOR
, wxCommandEvent
);
72 // ----------------------------------------------------------------------------
73 // wxGridCellEditorEvtHandler
74 // ----------------------------------------------------------------------------
76 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
78 // We must let the native control have this event so in any case don't mark
79 // it as handled, otherwise various weird problems can happen (see #11681).
82 // Don't disable the cell if we're just starting to edit it
86 // Tell the grid to dismiss the control but don't do it immediately as it
87 // could result in the editor being destroyed right now and a crash in the
88 // code searching for the next event handler, so post an event asking the
89 // grid to do it slightly later instead.
91 // FIXME-VC6: Once we drop support for VC6, we should use a simpler
92 // m_grid->CallAfter(&wxGrid::DisableCellEditControl) and get
93 // rid of wxEVT_GRID_HIDE_EDITOR entirely.
94 m_grid
->GetEventHandler()->
95 AddPendingEvent(wxCommandEvent(wxEVT_GRID_HIDE_EDITOR
));
98 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
100 switch ( event
.GetKeyCode() )
104 m_grid
->DisableCellEditControl();
108 m_grid
->GetEventHandler()->ProcessEvent( event
);
112 case WXK_NUMPAD_ENTER
:
113 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
114 m_editor
->HandleReturn(event
);
123 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
125 int row
= m_grid
->GetGridCursorRow();
126 int col
= m_grid
->GetGridCursorCol();
127 wxRect rect
= m_grid
->CellToRect( row
, col
);
129 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
131 // if cell width is smaller than grid client area, cell is wholly visible
132 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
134 switch ( event
.GetKeyCode() )
139 case WXK_NUMPAD_ENTER
:
144 if ( wholeCellVisible
)
146 // no special processing needed...
151 // do special processing for partly visible cell...
153 // get the widths of all cells previous to this one
155 for ( int i
= 0; i
< col
; i
++ )
157 colXPos
+= m_grid
->GetColSize(i
);
160 int xUnit
= 1, yUnit
= 1;
161 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
164 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
168 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
176 if ( wholeCellVisible
)
178 // no special processing needed...
183 // do special processing for partly visible cell...
186 wxString value
= m_grid
->GetCellValue(row
, col
);
187 if ( wxEmptyString
!= value
)
189 // get width of cell CONTENTS (text)
191 wxFont font
= m_grid
->GetCellFont(row
, col
);
192 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
194 // try to RIGHT align the text by scrolling
195 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
197 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
198 // otherwise the last part of the cell content might be hidden below the scroll bar
199 // FIXME: maybe there is a more suitable correction?
200 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
207 // get the widths of all cells previous to this one
209 for ( int i
= 0; i
< col
; i
++ )
211 colXPos
+= m_grid
->GetColSize(i
);
214 // and add the (modified) text width of the cell contents
215 // as we'd like to see the last part of the cell contents
216 colXPos
+= textWidth
;
218 int xUnit
= 1, yUnit
= 1;
219 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
220 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 wxGridCellEditor::wxGridCellEditor()
241 wxGridCellEditor::~wxGridCellEditor()
246 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
247 wxWindowID
WXUNUSED(id
),
248 wxEvtHandler
* evtHandler
)
251 m_control
->PushEventHandler(evtHandler
);
254 void wxGridCellEditor::PaintBackground(wxDC
& dc
,
255 const wxRect
& rectCell
,
256 const wxGridCellAttr
& attr
)
258 // erase the background because we might not fill the cell
259 dc
.SetPen(*wxTRANSPARENT_PEN
);
260 dc
.SetBrush(wxBrush(attr
.GetBackgroundColour()));
261 dc
.DrawRectangle(rectCell
);
264 void wxGridCellEditor::Destroy()
268 m_control
->PopEventHandler( true /* delete it*/ );
270 m_control
->Destroy();
275 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
277 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
279 m_control
->Show(show
);
283 // set the colours/fonts if we have any
286 m_colFgOld
= m_control
->GetForegroundColour();
287 m_control
->SetForegroundColour(attr
->GetTextColour());
289 m_colBgOld
= m_control
->GetBackgroundColour();
290 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
292 // Workaround for GTK+1 font setting problem on some platforms
293 #if !defined(__WXGTK__) || defined(__WXGTK20__)
294 m_fontOld
= m_control
->GetFont();
295 m_control
->SetFont(attr
->GetFont());
298 // can't do anything more in the base class version, the other
299 // attributes may only be used by the derived classes
304 // restore the standard colours fonts
305 if ( m_colFgOld
.IsOk() )
307 m_control
->SetForegroundColour(m_colFgOld
);
308 m_colFgOld
= wxNullColour
;
311 if ( m_colBgOld
.IsOk() )
313 m_control
->SetBackgroundColour(m_colBgOld
);
314 m_colBgOld
= wxNullColour
;
317 // Workaround for GTK+1 font setting problem on some platforms
318 #if !defined(__WXGTK__) || defined(__WXGTK20__)
319 if ( m_fontOld
.IsOk() )
321 m_control
->SetFont(m_fontOld
);
322 m_fontOld
= wxNullFont
;
328 void wxGridCellEditor::SetSize(const wxRect
& rect
)
330 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
332 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
335 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
340 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
342 bool ctrl
= event
.ControlDown();
346 // On the Mac the Alt key is more like shift and is used for entry of
347 // valid characters, so check for Ctrl and Meta instead.
348 alt
= event
.MetaDown();
350 alt
= event
.AltDown();
351 #endif // __WXMAC__/!__WXMAC__
353 // Assume it's not a valid char if ctrl or alt is down, but if both are
354 // down then it may be because of an AltGr key combination, so let them
355 // through in that case.
356 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
360 if ( static_cast<int>(event
.GetUnicodeKey()) == WXK_NONE
)
363 if ( event
.GetKeyCode() > WXK_START
)
370 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
375 void wxGridCellEditor::StartingClick()
381 // ----------------------------------------------------------------------------
382 // wxGridCellTextEditor
383 // ----------------------------------------------------------------------------
385 wxGridCellTextEditor::wxGridCellTextEditor(size_t maxChars
)
387 m_maxChars
= maxChars
;
390 void wxGridCellTextEditor::Create(wxWindow
* parent
,
392 wxEvtHandler
* evtHandler
)
394 DoCreate(parent
, id
, evtHandler
);
397 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
399 wxEvtHandler
* evtHandler
,
402 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
404 wxTextCtrl
* const text
= new wxTextCtrl(parent
, id
, wxEmptyString
,
405 wxDefaultPosition
, wxDefaultSize
,
407 text
->SetMargins(0, 0);
411 wxWidgetImpl
* impl
= m_control
->GetPeer();
412 impl
->SetNeedsFocusRect(false);
414 // set max length allowed in the textctrl, if the parameter was set
415 if ( m_maxChars
!= 0 )
417 Text()->SetMaxLength(m_maxChars
);
419 // validate text in textctrl, if validator is set
422 Text()->SetValidator(*m_validator
);
425 wxGridCellEditor::Create(parent
, id
, evtHandler
);
428 void wxGridCellTextEditor::PaintBackground(wxDC
& WXUNUSED(dc
),
429 const wxRect
& WXUNUSED(rectCell
),
430 const wxGridCellAttr
& WXUNUSED(attr
))
432 // as we fill the entire client area,
433 // don't do anything here to minimize flicker
436 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
438 wxRect
rect(rectOrig
);
440 // Make the edit control large enough to allow for internal margins
442 // TODO: remove this if the text ctrl sizing is improved esp. for unix
444 #if defined(__WXGTK__)
452 #elif defined(__WXMSW__)
465 #elif defined(__WXOSX__)
472 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
473 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
475 #if defined(__WXMOTIF__)
480 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
481 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
482 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
483 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
486 wxGridCellEditor::SetSize(rect
);
489 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
491 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
493 m_value
= grid
->GetTable()->GetValue(row
, col
);
495 DoBeginEdit(m_value
);
498 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
500 Text()->SetValue(startValue
);
501 Text()->SetInsertionPointEnd();
506 bool wxGridCellTextEditor::EndEdit(int WXUNUSED(row
),
508 const wxGrid
* WXUNUSED(grid
),
509 const wxString
& WXUNUSED(oldval
),
512 wxCHECK_MSG( m_control
, false,
513 "wxGridCellTextEditor must be created first!" );
515 const wxString value
= Text()->GetValue();
516 if ( value
== m_value
)
527 void wxGridCellTextEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
529 grid
->GetTable()->SetValue(row
, col
, m_value
);
533 void wxGridCellTextEditor::Reset()
535 wxASSERT_MSG( m_control
, "wxGridCellTextEditor must be created first!" );
540 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
542 Text()->SetValue(startValue
);
543 Text()->SetInsertionPointEnd();
546 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
548 switch ( event
.GetKeyCode() )
555 return wxGridCellEditor::IsAcceptedKey(event
);
559 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
561 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
562 // longer an appropriate way to get the character into the text control.
563 // Do it ourselves instead. We know that if we get this far that we have
564 // a valid character, so not a whole lot of testing needs to be done.
566 wxTextCtrl
* tc
= Text();
572 ch
= event
.GetUnicodeKey();
573 if ( ch
!= WXK_NONE
)
576 #endif // wxUSE_UNICODE
578 ch
= event
.GetKeyCode();
579 isPrintable
= ch
>= WXK_SPACE
&& ch
< WXK_START
;
585 // Delete the initial character when starting to edit with DELETE.
590 // Delete the last character when starting to edit with BACKSPACE.
592 const long pos
= tc
->GetLastPosition();
593 tc
->Remove(pos
- 1, pos
);
599 tc
->WriteText(static_cast<wxChar
>(ch
));
604 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
605 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
607 #if defined(__WXMOTIF__) || defined(__WXGTK__)
608 // wxMotif needs a little extra help...
609 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
610 wxString
s( Text()->GetValue() );
611 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
613 Text()->SetInsertionPoint( pos
);
615 // the other ports can handle a Return key press
621 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
631 if ( params
.ToLong(&tmp
) )
633 m_maxChars
= (size_t)tmp
;
637 wxLogDebug( wxT("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
642 void wxGridCellTextEditor::SetValidator(const wxValidator
& validator
)
644 m_validator
.reset(static_cast<wxValidator
*>(validator
.Clone()));
647 wxGridCellEditor
*wxGridCellTextEditor::Clone() const
649 wxGridCellTextEditor
* editor
= new wxGridCellTextEditor(m_maxChars
);
652 editor
->SetValidator(*m_validator
);
657 // return the value in the text control
658 wxString
wxGridCellTextEditor::GetValue() const
660 return Text()->GetValue();
663 // ----------------------------------------------------------------------------
664 // wxGridCellNumberEditor
665 // ----------------------------------------------------------------------------
667 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
673 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
675 wxEvtHandler
* evtHandler
)
680 // create a spin ctrl
681 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
682 wxDefaultPosition
, wxDefaultSize
,
686 wxGridCellEditor::Create(parent
, id
, evtHandler
);
691 // just a text control
692 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
695 Text()->SetValidator(wxIntegerValidator
<int>());
700 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
702 // first get the value
703 wxGridTableBase
*table
= grid
->GetTable();
704 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
706 m_value
= table
->GetValueAsLong(row
, col
);
711 wxString sValue
= table
->GetValue(row
, col
);
712 if (! sValue
.ToLong(&m_value
) && ! sValue
.empty())
714 wxFAIL_MSG( wxT("this cell doesn't have numeric value") );
722 Spin()->SetValue((int)m_value
);
728 DoBeginEdit(GetString());
732 bool wxGridCellNumberEditor::EndEdit(int WXUNUSED(row
),
734 const wxGrid
* WXUNUSED(grid
),
735 const wxString
& oldval
, wxString
*newval
)
743 value
= Spin()->GetValue();
744 if ( value
== m_value
)
747 text
.Printf(wxT("%ld"), value
);
749 else // using unconstrained input
750 #endif // wxUSE_SPINCTRL
752 text
= Text()->GetValue();
755 if ( oldval
.empty() )
758 else // non-empty text now (maybe 0)
760 if ( !text
.ToLong(&value
) )
763 // if value == m_value == 0 but old text was "" and new one is
764 // "0" something still did change
765 if ( value
== m_value
&& (value
|| !oldval
.empty()) )
778 void wxGridCellNumberEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
780 wxGridTableBase
* const table
= grid
->GetTable();
781 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
782 table
->SetValueAsLong(row
, col
, m_value
);
784 table
->SetValue(row
, col
, wxString::Format("%ld", m_value
));
787 void wxGridCellNumberEditor::Reset()
792 Spin()->SetValue((int)m_value
);
797 DoReset(GetString());
801 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
803 if ( wxGridCellEditor::IsAcceptedKey(event
) )
805 int keycode
= event
.GetKeyCode();
806 if ( (keycode
< 128) &&
807 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
816 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
818 int keycode
= event
.GetKeyCode();
821 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
823 wxGridCellTextEditor::StartingKey(event
);
832 if ( wxIsdigit(keycode
) )
834 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
835 spin
->SetValue(keycode
- '0');
836 spin
->SetSelection(1,1);
845 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
856 if ( params
.BeforeFirst(wxT(',')).ToLong(&tmp
) )
860 if ( params
.AfterFirst(wxT(',')).ToLong(&tmp
) )
864 // skip the error message below
869 wxLogDebug(wxT("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
873 // return the value in the spin control if it is there (the text control otherwise)
874 wxString
wxGridCellNumberEditor::GetValue() const
881 long value
= Spin()->GetValue();
882 s
.Printf(wxT("%ld"), value
);
887 s
= Text()->GetValue();
893 // ----------------------------------------------------------------------------
894 // wxGridCellFloatEditor
895 // ----------------------------------------------------------------------------
897 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
,
902 m_precision
= precision
;
906 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
908 wxEvtHandler
* evtHandler
)
910 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
913 Text()->SetValidator(wxFloatingPointValidator
<double>(m_precision
));
917 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
919 // first get the value
920 wxGridTableBase
* const table
= grid
->GetTable();
921 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
923 m_value
= table
->GetValueAsDouble(row
, col
);
929 const wxString value
= table
->GetValue(row
, col
);
930 if ( !value
.empty() )
932 if ( !value
.ToDouble(&m_value
) )
934 wxFAIL_MSG( wxT("this cell doesn't have float value") );
940 DoBeginEdit(GetString());
943 bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row
),
945 const wxGrid
* WXUNUSED(grid
),
946 const wxString
& oldval
, wxString
*newval
)
948 const wxString
text(Text()->GetValue());
953 if ( !text
.ToDouble(&value
) )
956 else // new value is empty string
958 if ( oldval
.empty() )
959 return false; // nothing changed
964 // the test for empty strings ensures that we don't skip the value setting
965 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
966 if ( wxIsSameDouble(value
, m_value
) && !text
.empty() && !oldval
.empty() )
967 return false; // nothing changed
977 void wxGridCellFloatEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
979 wxGridTableBase
* const table
= grid
->GetTable();
981 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
982 table
->SetValueAsDouble(row
, col
, m_value
);
984 table
->SetValue(row
, col
, Text()->GetValue());
987 void wxGridCellFloatEditor::Reset()
989 DoReset(GetString());
992 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
994 int keycode
= event
.GetKeyCode();
996 tmpbuf
[0] = (char) keycode
;
998 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1001 bool is_decimal_point
= ( strbuf
==
1002 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1004 bool is_decimal_point
= ( strbuf
== wxT(".") );
1007 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1008 || is_decimal_point
)
1010 wxGridCellTextEditor::StartingKey(event
);
1012 // skip Skip() below
1019 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1026 m_style
= wxGRID_FLOAT_FORMAT_DEFAULT
;
1032 wxString tmp
= params
.BeforeFirst(wxT(','), &rest
);
1036 if ( tmp
.ToLong(&width
) )
1038 m_width
= (int)width
;
1042 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1046 tmp
= rest
.BeforeFirst(wxT(','));
1050 if ( tmp
.ToLong(&precision
) )
1052 m_precision
= (int)precision
;
1056 wxLogDebug(wxT("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1060 tmp
= rest
.AfterFirst(wxT(','));
1063 if ( tmp
[0] == wxT('f') )
1065 m_style
= wxGRID_FLOAT_FORMAT_FIXED
;
1067 else if ( tmp
[0] == wxT('e') )
1069 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
;
1071 else if ( tmp
[0] == wxT('g') )
1073 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
;
1075 else if ( tmp
[0] == wxT('E') )
1077 m_style
= wxGRID_FLOAT_FORMAT_SCIENTIFIC
|
1078 wxGRID_FLOAT_FORMAT_UPPER
;
1080 else if ( tmp
[0] == wxT('F') )
1082 m_style
= wxGRID_FLOAT_FORMAT_FIXED
|
1083 wxGRID_FLOAT_FORMAT_UPPER
;
1085 else if ( tmp
[0] == wxT('G') )
1087 m_style
= wxGRID_FLOAT_FORMAT_COMPACT
|
1088 wxGRID_FLOAT_FORMAT_UPPER
;
1092 wxLogDebug("Invalid wxGridCellFloatRenderer format "
1093 "parameter string '%s ignored", params
);
1099 wxString
wxGridCellFloatEditor::GetString()
1103 if ( m_precision
== -1 && m_width
!= -1)
1105 // default precision
1106 m_format
.Printf(wxT("%%%d."), m_width
);
1108 else if ( m_precision
!= -1 && m_width
== -1)
1111 m_format
.Printf(wxT("%%.%d"), m_precision
);
1113 else if ( m_precision
!= -1 && m_width
!= -1 )
1115 m_format
.Printf(wxT("%%%d.%d"), m_width
, m_precision
);
1119 // default width/precision
1120 m_format
= wxT("%");
1123 bool isUpper
= (m_style
& wxGRID_FLOAT_FORMAT_UPPER
) != 0;
1124 if ( m_style
& wxGRID_FLOAT_FORMAT_SCIENTIFIC
)
1125 m_format
+= isUpper
? wxT('E') : wxT('e');
1126 else if ( m_style
& wxGRID_FLOAT_FORMAT_COMPACT
)
1127 m_format
+= isUpper
? wxT('G') : wxT('g');
1129 m_format
+= wxT('f');
1132 return wxString::Format(m_format
, m_value
);
1135 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1137 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1139 const int keycode
= event
.GetKeyCode();
1140 if ( wxIsascii(keycode
) )
1143 const wxString decimalPoint
=
1144 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1146 const wxString
decimalPoint(wxT('.'));
1149 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1150 if ( wxIsdigit(keycode
) ||
1151 tolower(keycode
) == 'e' ||
1152 keycode
== decimalPoint
||
1164 #endif // wxUSE_TEXTCTRL
1168 // ----------------------------------------------------------------------------
1169 // wxGridCellBoolEditor
1170 // ----------------------------------------------------------------------------
1172 // the default values for GetValue()
1173 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { wxT(""), wxT("1") };
1175 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1177 wxEvtHandler
* evtHandler
)
1179 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1180 wxDefaultPosition
, wxDefaultSize
,
1183 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1186 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1188 bool resize
= false;
1189 wxSize size
= m_control
->GetSize();
1190 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1192 // check if the checkbox is not too big/small for this cell
1193 wxSize sizeBest
= m_control
->GetBestSize();
1194 if ( !(size
== sizeBest
) )
1196 // reset to default size if it had been made smaller
1202 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1204 // leave 1 pixel margin
1205 size
.x
= size
.y
= minSize
- 2;
1212 m_control
->SetSize(size
);
1215 // position it in the centre of the rectangle (TODO: support alignment?)
1217 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1218 // the checkbox without label still has some space to the right in wxGTK,
1219 // so shift it to the right
1221 #elif defined(__WXMSW__)
1222 // here too, but in other way
1227 int hAlign
= wxALIGN_CENTRE
;
1228 int vAlign
= wxALIGN_CENTRE
;
1230 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1233 if (hAlign
== wxALIGN_LEFT
)
1241 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1243 else if (hAlign
== wxALIGN_RIGHT
)
1245 x
= r
.x
+ r
.width
- size
.x
- 2;
1246 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1248 else if (hAlign
== wxALIGN_CENTRE
)
1250 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1251 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1254 m_control
->Move(x
, y
);
1257 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1259 m_control
->Show(show
);
1263 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1264 CBox()->SetBackgroundColour(colBg
);
1268 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1270 wxASSERT_MSG(m_control
,
1271 wxT("The wxGridCellEditor must be created first!"));
1273 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1275 m_value
= grid
->GetTable()->GetValueAsBool(row
, col
);
1279 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1281 if ( cellval
== ms_stringValues
[false] )
1283 else if ( cellval
== ms_stringValues
[true] )
1287 // do not try to be smart here and convert it to true or false
1288 // because we'll still overwrite it with something different and
1289 // this risks to be very surprising for the user code, let them
1291 wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") );
1295 CBox()->SetValue(m_value
);
1299 bool wxGridCellBoolEditor::EndEdit(int WXUNUSED(row
),
1301 const wxGrid
* WXUNUSED(grid
),
1302 const wxString
& WXUNUSED(oldval
),
1305 bool value
= CBox()->GetValue();
1306 if ( value
== m_value
)
1312 *newval
= GetValue();
1317 void wxGridCellBoolEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1319 wxGridTableBase
* const table
= grid
->GetTable();
1320 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1321 table
->SetValueAsBool(row
, col
, m_value
);
1323 table
->SetValue(row
, col
, GetValue());
1326 void wxGridCellBoolEditor::Reset()
1328 wxASSERT_MSG(m_control
,
1329 wxT("The wxGridCellEditor must be created first!"));
1331 CBox()->SetValue(m_value
);
1334 void wxGridCellBoolEditor::StartingClick()
1336 CBox()->SetValue(!CBox()->GetValue());
1339 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1341 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1343 int keycode
= event
.GetKeyCode();
1356 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1358 int keycode
= event
.GetKeyCode();
1362 CBox()->SetValue(!CBox()->GetValue());
1366 CBox()->SetValue(true);
1370 CBox()->SetValue(false);
1375 wxString
wxGridCellBoolEditor::GetValue() const
1377 return ms_stringValues
[CBox()->GetValue()];
1381 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1382 const wxString
& valueFalse
)
1384 ms_stringValues
[false] = valueFalse
;
1385 ms_stringValues
[true] = valueTrue
;
1389 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1391 return value
== ms_stringValues
[true];
1394 #endif // wxUSE_CHECKBOX
1398 // ----------------------------------------------------------------------------
1399 // wxGridCellChoiceEditor
1400 // ----------------------------------------------------------------------------
1402 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1404 : m_choices(choices
),
1405 m_allowOthers(allowOthers
) { }
1407 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1408 const wxString choices
[],
1410 : m_allowOthers(allowOthers
)
1414 m_choices
.Alloc(count
);
1415 for ( size_t n
= 0; n
< count
; n
++ )
1417 m_choices
.Add(choices
[n
]);
1422 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1424 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1425 editor
->m_allowOthers
= m_allowOthers
;
1426 editor
->m_choices
= m_choices
;
1431 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1433 wxEvtHandler
* evtHandler
)
1435 int style
= wxTE_PROCESS_ENTER
|
1439 if ( !m_allowOthers
)
1440 style
|= wxCB_READONLY
;
1441 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1442 wxDefaultPosition
, wxDefaultSize
,
1446 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1449 void wxGridCellChoiceEditor::SetSize(const wxRect
& rect
)
1451 wxASSERT_MSG(m_control
,
1452 wxT("The wxGridCellChoiceEditor must be created first!"));
1454 // Check that the height is not too small to fit the combobox.
1455 wxRect rectTallEnough
= rect
;
1456 const wxSize bestSize
= m_control
->GetBestSize();
1457 const wxCoord diffY
= bestSize
.GetHeight() - rectTallEnough
.GetHeight();
1460 // Do make it tall enough.
1461 rectTallEnough
.height
+= diffY
;
1463 // Also centre the effective rectangle vertically with respect to the
1465 rectTallEnough
.y
-= diffY
/2;
1467 //else: The rectangle provided is already tall enough.
1469 wxGridCellEditor::SetSize(rectTallEnough
);
1472 void wxGridCellChoiceEditor::PaintBackground(wxDC
& dc
,
1473 const wxRect
& rectCell
,
1474 const wxGridCellAttr
& attr
)
1476 // as we fill the entire client area, don't do anything here to minimize
1479 // TODO: It doesn't actually fill the client area since the height of a
1480 // combo always defaults to the standard. Until someone has time to
1481 // figure out the right rectangle to paint, just do it the normal way.
1482 wxGridCellEditor::PaintBackground(dc
, rectCell
, attr
);
1485 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1487 wxASSERT_MSG(m_control
,
1488 wxT("The wxGridCellEditor must be created first!"));
1490 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1492 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1494 // Don't immediately end if we get a kill focus event within BeginEdit
1496 evtHandler
->SetInSetFocus(true);
1498 m_value
= grid
->GetTable()->GetValue(row
, col
);
1500 Reset(); // this updates combo box to correspond to m_value
1502 Combo()->SetFocus();
1504 #ifdef __WXOSX_COCOA__
1505 // This is a work around for the combobox being simply dismissed when a
1506 // choice is made in it under OS X. The bug is almost certainly due to a
1507 // problem in focus events generation logic but it's not obvious to fix and
1508 // for now this at least allows to use wxGrid.
1514 // When dropping down the menu, a kill focus event
1515 // happens after this point, so we can't reset the flag yet.
1516 #if !defined(__WXGTK20__)
1517 evtHandler
->SetInSetFocus(false);
1522 bool wxGridCellChoiceEditor::EndEdit(int WXUNUSED(row
),
1524 const wxGrid
* WXUNUSED(grid
),
1525 const wxString
& WXUNUSED(oldval
),
1528 const wxString value
= Combo()->GetValue();
1529 if ( value
== m_value
)
1540 void wxGridCellChoiceEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1542 grid
->GetTable()->SetValue(row
, col
, m_value
);
1545 void wxGridCellChoiceEditor::Reset()
1549 Combo()->SetValue(m_value
);
1550 Combo()->SetInsertionPointEnd();
1552 else // the combobox is read-only
1554 // find the right position, or default to the first if not found
1555 int pos
= Combo()->FindString(m_value
);
1556 if (pos
== wxNOT_FOUND
)
1558 Combo()->SetSelection(pos
);
1562 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1572 wxStringTokenizer
tk(params
, wxT(','));
1573 while ( tk
.HasMoreTokens() )
1575 m_choices
.Add(tk
.GetNextToken());
1579 // return the value in the text control
1580 wxString
wxGridCellChoiceEditor::GetValue() const
1582 return Combo()->GetValue();
1585 #endif // wxUSE_COMBOBOX
1589 // ----------------------------------------------------------------------------
1590 // wxGridCellEnumEditor
1591 // ----------------------------------------------------------------------------
1593 // A cell editor which displays an enum number as a textual equivalent. eg
1594 // data in cell is 0,1,2 ... n the cell could be displayed as
1595 // "John","Fred"..."Bob" in the combo choice box
1597 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
1598 :wxGridCellChoiceEditor()
1602 if (!choices
.empty())
1603 SetParameters(choices
);
1606 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
1608 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
1609 editor
->m_index
= m_index
;
1613 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1615 wxASSERT_MSG(m_control
,
1616 wxT("The wxGridCellEnumEditor must be Created first!"));
1618 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1620 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1622 // Don't immediately end if we get a kill focus event within BeginEdit
1624 evtHandler
->SetInSetFocus(true);
1626 wxGridTableBase
*table
= grid
->GetTable();
1628 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1630 m_index
= table
->GetValueAsLong(row
, col
);
1634 wxString startValue
= table
->GetValue(row
, col
);
1635 if (startValue
.IsNumber() && !startValue
.empty())
1637 startValue
.ToLong(&m_index
);
1645 Combo()->SetSelection(m_index
);
1646 Combo()->SetFocus();
1648 #ifdef __WXOSX_COCOA__
1649 // This is a work around for the combobox being simply dismissed when a
1650 // choice is made in it under OS X. The bug is almost certainly due to a
1651 // problem in focus events generation logic but it's not obvious to fix and
1652 // for now this at least allows to use wxGrid.
1658 // When dropping down the menu, a kill focus event
1659 // happens after this point, so we can't reset the flag yet.
1660 #if !defined(__WXGTK20__)
1661 evtHandler
->SetInSetFocus(false);
1666 bool wxGridCellEnumEditor::EndEdit(int WXUNUSED(row
),
1668 const wxGrid
* WXUNUSED(grid
),
1669 const wxString
& WXUNUSED(oldval
),
1672 long idx
= Combo()->GetSelection();
1673 if ( idx
== m_index
)
1679 newval
->Printf("%ld", m_index
);
1684 void wxGridCellEnumEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1686 wxGridTableBase
* const table
= grid
->GetTable();
1687 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1688 table
->SetValueAsLong(row
, col
, m_index
);
1690 table
->SetValue(row
, col
, wxString::Format("%ld", m_index
));
1693 #endif // wxUSE_COMBOBOX
1695 // ----------------------------------------------------------------------------
1696 // wxGridCellAutoWrapStringEditor
1697 // ----------------------------------------------------------------------------
1700 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
1702 wxEvtHandler
* evtHandler
)
1704 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
1705 wxTE_MULTILINE
| wxTE_RICH
);
1709 #endif // wxUSE_GRID