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"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
41 #include "wx/headerctrl.h"
43 #include "wx/generic/gridsel.h"
44 #include "wx/generic/grideditors.h"
45 #include "wx/generic/private/grid.h"
47 #if defined(__WXMOTIF__)
48 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
50 #define WXUNUSED_MOTIF(identifier) identifier
53 #if defined(__WXGTK__)
54 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
56 #define WXUNUSED_GTK(identifier) identifier
59 // Required for wxIs... functions
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxGridCellEditorEvtHandler
68 // ----------------------------------------------------------------------------
70 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
72 // Don't disable the cell if we're just starting to edit it
80 m_grid
->DisableCellEditControl();
82 // notice that we must not skip the event here because the call above may
83 // delete the control which received the kill focus event in the first
84 // place and if we pretend not having processed the event, the search for a
85 // handler for it will continue using the now deleted object resulting in a
89 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
91 switch ( event
.GetKeyCode() )
95 m_grid
->DisableCellEditControl();
99 m_grid
->GetEventHandler()->ProcessEvent( event
);
103 case WXK_NUMPAD_ENTER
:
104 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
105 m_editor
->HandleReturn(event
);
114 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
116 int row
= m_grid
->GetGridCursorRow();
117 int col
= m_grid
->GetGridCursorCol();
118 wxRect rect
= m_grid
->CellToRect( row
, col
);
120 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
122 // if cell width is smaller than grid client area, cell is wholly visible
123 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
125 switch ( event
.GetKeyCode() )
130 case WXK_NUMPAD_ENTER
:
135 if ( wholeCellVisible
)
137 // no special processing needed...
142 // do special processing for partly visible cell...
144 // get the widths of all cells previous to this one
146 for ( int i
= 0; i
< col
; i
++ )
148 colXPos
+= m_grid
->GetColSize(i
);
151 int xUnit
= 1, yUnit
= 1;
152 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
155 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
159 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
167 if ( wholeCellVisible
)
169 // no special processing needed...
174 // do special processing for partly visible cell...
177 wxString value
= m_grid
->GetCellValue(row
, col
);
178 if ( wxEmptyString
!= value
)
180 // get width of cell CONTENTS (text)
182 wxFont font
= m_grid
->GetCellFont(row
, col
);
183 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
185 // try to RIGHT align the text by scrolling
186 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
188 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
189 // otherwise the last part of the cell content might be hidden below the scroll bar
190 // FIXME: maybe there is a more suitable correction?
191 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
198 // get the widths of all cells previous to this one
200 for ( int i
= 0; i
< col
; i
++ )
202 colXPos
+= m_grid
->GetColSize(i
);
205 // and add the (modified) text width of the cell contents
206 // as we'd like to see the last part of the cell contents
207 colXPos
+= textWidth
;
209 int xUnit
= 1, yUnit
= 1;
210 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
211 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 wxGridCellEditor::wxGridCellEditor()
232 wxGridCellEditor::~wxGridCellEditor()
237 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
238 wxWindowID
WXUNUSED(id
),
239 wxEvtHandler
* evtHandler
)
242 m_control
->PushEventHandler(evtHandler
);
245 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
246 wxGridCellAttr
*attr
)
248 // erase the background because we might not fill the cell
249 wxClientDC
dc(m_control
->GetParent());
250 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
252 gridWindow
->GetOwner()->PrepareDC(dc
);
254 dc
.SetPen(*wxTRANSPARENT_PEN
);
255 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
256 dc
.DrawRectangle(rectCell
);
258 // redraw the control we just painted over
259 m_control
->Refresh();
262 void wxGridCellEditor::Destroy()
266 m_control
->PopEventHandler( true /* delete it*/ );
268 m_control
->Destroy();
273 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
275 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
277 m_control
->Show(show
);
281 // set the colours/fonts if we have any
284 m_colFgOld
= m_control
->GetForegroundColour();
285 m_control
->SetForegroundColour(attr
->GetTextColour());
287 m_colBgOld
= m_control
->GetBackgroundColour();
288 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
290 // Workaround for GTK+1 font setting problem on some platforms
291 #if !defined(__WXGTK__) || defined(__WXGTK20__)
292 m_fontOld
= m_control
->GetFont();
293 m_control
->SetFont(attr
->GetFont());
296 // can't do anything more in the base class version, the other
297 // attributes may only be used by the derived classes
302 // restore the standard colours fonts
303 if ( m_colFgOld
.Ok() )
305 m_control
->SetForegroundColour(m_colFgOld
);
306 m_colFgOld
= wxNullColour
;
309 if ( m_colBgOld
.Ok() )
311 m_control
->SetBackgroundColour(m_colBgOld
);
312 m_colBgOld
= wxNullColour
;
315 // Workaround for GTK+1 font setting problem on some platforms
316 #if !defined(__WXGTK__) || defined(__WXGTK20__)
317 if ( m_fontOld
.Ok() )
319 m_control
->SetFont(m_fontOld
);
320 m_fontOld
= wxNullFont
;
326 void wxGridCellEditor::SetSize(const wxRect
& rect
)
328 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
330 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
333 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
338 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
340 bool ctrl
= event
.ControlDown();
341 bool alt
= event
.AltDown();
344 // On the Mac the Alt key is more like shift and is used for entry of
345 // valid characters, so check for Ctrl and Meta instead.
346 alt
= event
.MetaDown();
349 // Assume it's not a valid char if ctrl or alt is down, but if both are
350 // down then it may be because of an AltGr key combination, so let them
351 // through in that case.
352 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
356 // if the unicode key code is not really a unicode character (it may
357 // be a function key or etc., the platforms appear to always give us a
358 // small value in this case) then fallback to the ASCII key code but
359 // don't do anything for function keys or etc.
360 if ( event
.GetUnicodeKey() > 127 && event
.GetKeyCode() > 127 )
363 if ( event
.GetKeyCode() > 255 )
370 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
375 void wxGridCellEditor::StartingClick()
381 // ----------------------------------------------------------------------------
382 // wxGridCellTextEditor
383 // ----------------------------------------------------------------------------
385 wxGridCellTextEditor::wxGridCellTextEditor()
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 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
405 wxDefaultPosition
, wxDefaultSize
,
408 // set max length allowed in the textctrl, if the parameter was set
409 if ( m_maxChars
!= 0 )
411 Text()->SetMaxLength(m_maxChars
);
414 wxGridCellEditor::Create(parent
, id
, evtHandler
);
417 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
418 wxGridCellAttr
* WXUNUSED(attr
))
420 // as we fill the entire client area,
421 // don't do anything here to minimize flicker
424 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
426 wxRect
rect(rectOrig
);
428 // Make the edit control large enough to allow for internal margins
430 // TODO: remove this if the text ctrl sizing is improved esp. for unix
432 #if defined(__WXGTK__)
440 #elif defined(__WXMSW__)
454 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
455 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
457 #if defined(__WXMOTIF__)
462 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
463 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
464 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
465 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
468 wxGridCellEditor::SetSize(rect
);
471 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
473 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
475 m_value
= grid
->GetTable()->GetValue(row
, col
);
477 DoBeginEdit(m_value
);
480 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
482 Text()->SetValue(startValue
);
483 Text()->SetInsertionPointEnd();
484 Text()->SetSelection(-1, -1);
488 bool wxGridCellTextEditor::EndEdit(const wxString
& WXUNUSED(oldval
),
491 wxCHECK_MSG( m_control
, false,
492 "wxGridCellTextEditor must be created first!" );
494 const wxString value
= Text()->GetValue();
495 if ( value
== m_value
)
506 void wxGridCellTextEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
508 grid
->GetTable()->SetValue(row
, col
, m_value
);
512 void wxGridCellTextEditor::Reset()
514 wxASSERT_MSG( m_control
, "wxGridCellTextEditor must be created first!" );
519 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
521 Text()->SetValue(startValue
);
522 Text()->SetInsertionPointEnd();
525 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
527 return wxGridCellEditor::IsAcceptedKey(event
);
530 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
532 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
533 // longer an appropriate way to get the character into the text control.
534 // Do it ourselves instead. We know that if we get this far that we have
535 // a valid character, so not a whole lot of testing needs to be done.
537 wxTextCtrl
* tc
= Text();
542 ch
= event
.GetUnicodeKey();
544 ch
= (wxChar
)event
.GetKeyCode();
546 ch
= (wxChar
)event
.GetKeyCode();
552 // delete the character at the cursor
553 pos
= tc
->GetInsertionPoint();
554 if (pos
< tc
->GetLastPosition())
555 tc
->Remove(pos
, pos
+ 1);
559 // delete the character before the cursor
560 pos
= tc
->GetInsertionPoint();
562 tc
->Remove(pos
- 1, pos
);
571 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
572 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
574 #if defined(__WXMOTIF__) || defined(__WXGTK__)
575 // wxMotif needs a little extra help...
576 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
577 wxString
s( Text()->GetValue() );
578 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
580 Text()->SetInsertionPoint( pos
);
582 // the other ports can handle a Return key press
588 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
598 if ( params
.ToLong(&tmp
) )
600 m_maxChars
= (size_t)tmp
;
604 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
609 // return the value in the text control
610 wxString
wxGridCellTextEditor::GetValue() const
612 return Text()->GetValue();
615 // ----------------------------------------------------------------------------
616 // wxGridCellNumberEditor
617 // ----------------------------------------------------------------------------
619 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
625 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
627 wxEvtHandler
* evtHandler
)
632 // create a spin ctrl
633 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
634 wxDefaultPosition
, wxDefaultSize
,
638 wxGridCellEditor::Create(parent
, id
, evtHandler
);
643 // just a text control
644 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
647 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
652 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
654 // first get the value
655 wxGridTableBase
*table
= grid
->GetTable();
656 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
658 m_value
= table
->GetValueAsLong(row
, col
);
663 wxString sValue
= table
->GetValue(row
, col
);
664 if (! sValue
.ToLong(&m_value
) && ! sValue
.empty())
666 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
674 Spin()->SetValue((int)m_value
);
680 DoBeginEdit(GetString());
684 bool wxGridCellNumberEditor::EndEdit(const wxString
& oldval
, wxString
*newval
)
692 value
= Spin()->GetValue();
693 if ( value
== m_value
)
696 text
.Printf(wxT("%ld"), value
);
698 else // using unconstrained input
699 #endif // wxUSE_SPINCTRL
701 text
= Text()->GetValue();
704 if ( oldval
.empty() )
707 else // non-empty text now (maybe 0)
709 if ( !text
.ToLong(&value
) )
712 // if value == m_value == 0 but old text was "" and new one is
713 // "0" something still did change
714 if ( value
== m_value
&& (value
|| !oldval
.empty()) )
727 void wxGridCellNumberEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
729 wxGridTableBase
* const table
= grid
->GetTable();
730 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
731 table
->SetValueAsLong(row
, col
, m_value
);
733 table
->SetValue(row
, col
, wxString::Format("%ld", m_value
));
736 void wxGridCellNumberEditor::Reset()
741 Spin()->SetValue((int)m_value
);
746 DoReset(GetString());
750 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
752 if ( wxGridCellEditor::IsAcceptedKey(event
) )
754 int keycode
= event
.GetKeyCode();
755 if ( (keycode
< 128) &&
756 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
765 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
767 int keycode
= event
.GetKeyCode();
770 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
772 wxGridCellTextEditor::StartingKey(event
);
781 if ( wxIsdigit(keycode
) )
783 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
784 spin
->SetValue(keycode
- '0');
785 spin
->SetSelection(1,1);
794 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
805 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
809 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
813 // skip the error message below
818 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
822 // return the value in the spin control if it is there (the text control otherwise)
823 wxString
wxGridCellNumberEditor::GetValue() const
830 long value
= Spin()->GetValue();
831 s
.Printf(wxT("%ld"), value
);
836 s
= Text()->GetValue();
842 // ----------------------------------------------------------------------------
843 // wxGridCellFloatEditor
844 // ----------------------------------------------------------------------------
846 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
849 m_precision
= precision
;
852 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
854 wxEvtHandler
* evtHandler
)
856 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
859 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
863 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
865 // first get the value
866 wxGridTableBase
* const table
= grid
->GetTable();
867 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
869 m_value
= table
->GetValueAsDouble(row
, col
);
875 const wxString value
= table
->GetValue(row
, col
);
876 if ( !value
.empty() )
878 if ( !value
.ToDouble(&m_value
) )
880 wxFAIL_MSG( _T("this cell doesn't have float value") );
886 DoBeginEdit(GetString());
889 bool wxGridCellFloatEditor::EndEdit(const wxString
& oldval
, wxString
*newval
)
891 const wxString
text(Text()->GetValue());
896 if ( !text
.ToDouble(&value
) )
899 else // new value is empty string
901 if ( oldval
.empty() )
902 return false; // nothing changed
907 // the test for empty strings ensures that we don't skip the value setting
908 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
909 if ( wxIsSameDouble(value
, m_value
) && !text
.empty() && !oldval
.empty() )
910 return false; // nothing changed
920 void wxGridCellFloatEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
922 wxGridTableBase
* const table
= grid
->GetTable();
924 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
925 table
->SetValueAsDouble(row
, col
, m_value
);
927 table
->SetValue(row
, col
, Text()->GetValue());
930 void wxGridCellFloatEditor::Reset()
932 DoReset(GetString());
935 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
937 int keycode
= event
.GetKeyCode();
939 tmpbuf
[0] = (char) keycode
;
941 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
944 bool is_decimal_point
= ( strbuf
==
945 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
947 bool is_decimal_point
= ( strbuf
== _T(".") );
950 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
951 || is_decimal_point
)
953 wxGridCellTextEditor::StartingKey(event
);
962 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
973 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
977 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
979 m_precision
= (int)tmp
;
981 // skip the error message below
986 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
990 wxString
wxGridCellFloatEditor::GetString() const
993 if ( m_precision
== -1 && m_width
!= -1)
996 fmt
.Printf(_T("%%%d.f"), m_width
);
998 else if ( m_precision
!= -1 && m_width
== -1)
1001 fmt
.Printf(_T("%%.%df"), m_precision
);
1003 else if ( m_precision
!= -1 && m_width
!= -1 )
1005 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1009 // default width/precision
1013 return wxString::Format(fmt
, m_value
);
1016 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1018 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1020 const int keycode
= event
.GetKeyCode();
1021 if ( isascii(keycode
) )
1024 tmpbuf
[0] = (char) keycode
;
1026 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1029 const wxString decimalPoint
=
1030 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1032 const wxString
decimalPoint(_T('.'));
1035 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1036 if ( wxIsdigit(keycode
) ||
1037 tolower(keycode
) == 'e' ||
1038 keycode
== decimalPoint
||
1050 #endif // wxUSE_TEXTCTRL
1054 // ----------------------------------------------------------------------------
1055 // wxGridCellBoolEditor
1056 // ----------------------------------------------------------------------------
1058 // the default values for GetValue()
1059 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1061 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1063 wxEvtHandler
* evtHandler
)
1065 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1066 wxDefaultPosition
, wxDefaultSize
,
1069 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1072 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1074 bool resize
= false;
1075 wxSize size
= m_control
->GetSize();
1076 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1078 // check if the checkbox is not too big/small for this cell
1079 wxSize sizeBest
= m_control
->GetBestSize();
1080 if ( !(size
== sizeBest
) )
1082 // reset to default size if it had been made smaller
1088 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1090 // leave 1 pixel margin
1091 size
.x
= size
.y
= minSize
- 2;
1098 m_control
->SetSize(size
);
1101 // position it in the centre of the rectangle (TODO: support alignment?)
1103 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1104 // the checkbox without label still has some space to the right in wxGTK,
1105 // so shift it to the right
1107 #elif defined(__WXMSW__)
1108 // here too, but in other way
1113 int hAlign
= wxALIGN_CENTRE
;
1114 int vAlign
= wxALIGN_CENTRE
;
1116 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1119 if (hAlign
== wxALIGN_LEFT
)
1127 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1129 else if (hAlign
== wxALIGN_RIGHT
)
1131 x
= r
.x
+ r
.width
- size
.x
- 2;
1132 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1134 else if (hAlign
== wxALIGN_CENTRE
)
1136 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1137 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1140 m_control
->Move(x
, y
);
1143 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1145 m_control
->Show(show
);
1149 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1150 CBox()->SetBackgroundColour(colBg
);
1154 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1156 wxASSERT_MSG(m_control
,
1157 wxT("The wxGridCellEditor must be created first!"));
1159 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1161 m_value
= grid
->GetTable()->GetValueAsBool(row
, col
);
1165 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1167 if ( cellval
== ms_stringValues
[false] )
1169 else if ( cellval
== ms_stringValues
[true] )
1173 // do not try to be smart here and convert it to true or false
1174 // because we'll still overwrite it with something different and
1175 // this risks to be very surprising for the user code, let them
1177 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1181 CBox()->SetValue(m_value
);
1185 bool wxGridCellBoolEditor::EndEdit(const wxString
& WXUNUSED(oldval
),
1188 bool value
= CBox()->GetValue();
1189 if ( value
== m_value
)
1195 *newval
= GetValue();
1200 void wxGridCellBoolEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1202 wxGridTableBase
* const table
= grid
->GetTable();
1203 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1204 table
->SetValueAsBool(row
, col
, m_value
);
1206 table
->SetValue(row
, col
, GetValue());
1209 void wxGridCellBoolEditor::Reset()
1211 wxASSERT_MSG(m_control
,
1212 wxT("The wxGridCellEditor must be created first!"));
1214 CBox()->SetValue(m_value
);
1217 void wxGridCellBoolEditor::StartingClick()
1219 CBox()->SetValue(!CBox()->GetValue());
1222 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1224 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1226 int keycode
= event
.GetKeyCode();
1239 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1241 int keycode
= event
.GetKeyCode();
1245 CBox()->SetValue(!CBox()->GetValue());
1249 CBox()->SetValue(true);
1253 CBox()->SetValue(false);
1258 wxString
wxGridCellBoolEditor::GetValue() const
1260 return ms_stringValues
[CBox()->GetValue()];
1264 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1265 const wxString
& valueFalse
)
1267 ms_stringValues
[false] = valueFalse
;
1268 ms_stringValues
[true] = valueTrue
;
1272 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1274 return value
== ms_stringValues
[true];
1277 #endif // wxUSE_CHECKBOX
1281 // ----------------------------------------------------------------------------
1282 // wxGridCellChoiceEditor
1283 // ----------------------------------------------------------------------------
1285 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1287 : m_choices(choices
),
1288 m_allowOthers(allowOthers
) { }
1290 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1291 const wxString choices
[],
1293 : m_allowOthers(allowOthers
)
1297 m_choices
.Alloc(count
);
1298 for ( size_t n
= 0; n
< count
; n
++ )
1300 m_choices
.Add(choices
[n
]);
1305 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1307 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1308 editor
->m_allowOthers
= m_allowOthers
;
1309 editor
->m_choices
= m_choices
;
1314 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1316 wxEvtHandler
* evtHandler
)
1318 int style
= wxTE_PROCESS_ENTER
|
1322 if ( !m_allowOthers
)
1323 style
|= wxCB_READONLY
;
1324 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1325 wxDefaultPosition
, wxDefaultSize
,
1329 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1332 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1333 wxGridCellAttr
* attr
)
1335 // as we fill the entire client area, don't do anything here to minimize
1338 // TODO: It doesn't actually fill the client area since the height of a
1339 // combo always defaults to the standard. Until someone has time to
1340 // figure out the right rectangle to paint, just do it the normal way.
1341 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1344 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1346 wxASSERT_MSG(m_control
,
1347 wxT("The wxGridCellEditor must be created first!"));
1349 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1351 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1353 // Don't immediately end if we get a kill focus event within BeginEdit
1355 evtHandler
->SetInSetFocus(true);
1357 m_value
= grid
->GetTable()->GetValue(row
, col
);
1359 Reset(); // this updates combo box to correspond to m_value
1361 Combo()->SetFocus();
1365 // When dropping down the menu, a kill focus event
1366 // happens after this point, so we can't reset the flag yet.
1367 #if !defined(__WXGTK20__)
1368 evtHandler
->SetInSetFocus(false);
1373 bool wxGridCellChoiceEditor::EndEdit(const wxString
& WXUNUSED(oldval
),
1376 const wxString value
= Combo()->GetValue();
1377 if ( value
== m_value
)
1388 void wxGridCellChoiceEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1390 grid
->GetTable()->SetValue(row
, col
, m_value
);
1393 void wxGridCellChoiceEditor::Reset()
1397 Combo()->SetValue(m_value
);
1398 Combo()->SetInsertionPointEnd();
1400 else // the combobox is read-only
1402 // find the right position, or default to the first if not found
1403 int pos
= Combo()->FindString(m_value
);
1404 if (pos
== wxNOT_FOUND
)
1406 Combo()->SetSelection(pos
);
1410 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1420 wxStringTokenizer
tk(params
, _T(','));
1421 while ( tk
.HasMoreTokens() )
1423 m_choices
.Add(tk
.GetNextToken());
1427 // return the value in the text control
1428 wxString
wxGridCellChoiceEditor::GetValue() const
1430 return Combo()->GetValue();
1433 #endif // wxUSE_COMBOBOX
1437 // ----------------------------------------------------------------------------
1438 // wxGridCellEnumEditor
1439 // ----------------------------------------------------------------------------
1441 // A cell editor which displays an enum number as a textual equivalent. eg
1442 // data in cell is 0,1,2 ... n the cell could be displayed as
1443 // "John","Fred"..."Bob" in the combo choice box
1445 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
1446 :wxGridCellChoiceEditor()
1450 if (!choices
.empty())
1451 SetParameters(choices
);
1454 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
1456 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
1457 editor
->m_index
= m_index
;
1461 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1463 wxASSERT_MSG(m_control
,
1464 wxT("The wxGridCellEnumEditor must be Created first!"));
1466 wxGridTableBase
*table
= grid
->GetTable();
1468 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1470 m_index
= table
->GetValueAsLong(row
, col
);
1474 wxString startValue
= table
->GetValue(row
, col
);
1475 if (startValue
.IsNumber() && !startValue
.empty())
1477 startValue
.ToLong(&m_index
);
1485 Combo()->SetSelection(m_index
);
1486 Combo()->SetInsertionPointEnd();
1487 Combo()->SetFocus();
1491 bool wxGridCellEnumEditor::EndEdit(const wxString
& WXUNUSED(oldval
),
1494 long idx
= Combo()->GetSelection();
1495 if ( idx
== m_index
)
1501 newval
->Printf("%ld", m_index
);
1506 void wxGridCellEnumEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1508 wxGridTableBase
* const table
= grid
->GetTable();
1509 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1510 table
->SetValueAsLong(row
, col
, m_index
);
1512 table
->SetValue(row
, col
, wxString::Format("%ld", m_index
));
1515 #endif // wxUSE_COMBOBOX
1517 // ----------------------------------------------------------------------------
1518 // wxGridCellAutoWrapStringEditor
1519 // ----------------------------------------------------------------------------
1522 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
1524 wxEvtHandler
* evtHandler
)
1526 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
1527 wxTE_MULTILINE
| wxTE_RICH
);
1531 #endif // wxUSE_GRID