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(int WXUNUSED(row
),
490 const wxGrid
* WXUNUSED(grid
),
491 const wxString
& WXUNUSED(oldval
),
494 wxCHECK_MSG( m_control
, false,
495 "wxGridCellTextEditor must be created first!" );
497 const wxString value
= Text()->GetValue();
498 if ( value
== m_value
)
509 void wxGridCellTextEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
511 grid
->GetTable()->SetValue(row
, col
, m_value
);
515 void wxGridCellTextEditor::Reset()
517 wxASSERT_MSG( m_control
, "wxGridCellTextEditor must be created first!" );
522 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
524 Text()->SetValue(startValue
);
525 Text()->SetInsertionPointEnd();
528 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
530 return wxGridCellEditor::IsAcceptedKey(event
);
533 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
535 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
536 // longer an appropriate way to get the character into the text control.
537 // Do it ourselves instead. We know that if we get this far that we have
538 // a valid character, so not a whole lot of testing needs to be done.
540 wxTextCtrl
* tc
= Text();
545 ch
= event
.GetUnicodeKey();
547 ch
= (wxChar
)event
.GetKeyCode();
549 ch
= (wxChar
)event
.GetKeyCode();
555 // delete the character at the cursor
556 pos
= tc
->GetInsertionPoint();
557 if (pos
< tc
->GetLastPosition())
558 tc
->Remove(pos
, pos
+ 1);
562 // delete the character before the cursor
563 pos
= tc
->GetInsertionPoint();
565 tc
->Remove(pos
- 1, pos
);
574 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
575 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
577 #if defined(__WXMOTIF__) || defined(__WXGTK__)
578 // wxMotif needs a little extra help...
579 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
580 wxString
s( Text()->GetValue() );
581 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
583 Text()->SetInsertionPoint( pos
);
585 // the other ports can handle a Return key press
591 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
601 if ( params
.ToLong(&tmp
) )
603 m_maxChars
= (size_t)tmp
;
607 wxLogDebug( wxT("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
612 // return the value in the text control
613 wxString
wxGridCellTextEditor::GetValue() const
615 return Text()->GetValue();
618 // ----------------------------------------------------------------------------
619 // wxGridCellNumberEditor
620 // ----------------------------------------------------------------------------
622 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
628 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
630 wxEvtHandler
* evtHandler
)
635 // create a spin ctrl
636 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
637 wxDefaultPosition
, wxDefaultSize
,
641 wxGridCellEditor::Create(parent
, id
, evtHandler
);
646 // just a text control
647 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
650 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
655 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
657 // first get the value
658 wxGridTableBase
*table
= grid
->GetTable();
659 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
661 m_value
= table
->GetValueAsLong(row
, col
);
666 wxString sValue
= table
->GetValue(row
, col
);
667 if (! sValue
.ToLong(&m_value
) && ! sValue
.empty())
669 wxFAIL_MSG( wxT("this cell doesn't have numeric value") );
677 Spin()->SetValue((int)m_value
);
683 DoBeginEdit(GetString());
687 bool wxGridCellNumberEditor::EndEdit(int WXUNUSED(row
),
689 const wxGrid
* WXUNUSED(grid
),
690 const wxString
& oldval
, wxString
*newval
)
698 value
= Spin()->GetValue();
699 if ( value
== m_value
)
702 text
.Printf(wxT("%ld"), value
);
704 else // using unconstrained input
705 #endif // wxUSE_SPINCTRL
707 text
= Text()->GetValue();
710 if ( oldval
.empty() )
713 else // non-empty text now (maybe 0)
715 if ( !text
.ToLong(&value
) )
718 // if value == m_value == 0 but old text was "" and new one is
719 // "0" something still did change
720 if ( value
== m_value
&& (value
|| !oldval
.empty()) )
733 void wxGridCellNumberEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
735 wxGridTableBase
* const table
= grid
->GetTable();
736 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
737 table
->SetValueAsLong(row
, col
, m_value
);
739 table
->SetValue(row
, col
, wxString::Format("%ld", m_value
));
742 void wxGridCellNumberEditor::Reset()
747 Spin()->SetValue((int)m_value
);
752 DoReset(GetString());
756 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
758 if ( wxGridCellEditor::IsAcceptedKey(event
) )
760 int keycode
= event
.GetKeyCode();
761 if ( (keycode
< 128) &&
762 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
771 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
773 int keycode
= event
.GetKeyCode();
776 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
778 wxGridCellTextEditor::StartingKey(event
);
787 if ( wxIsdigit(keycode
) )
789 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
790 spin
->SetValue(keycode
- '0');
791 spin
->SetSelection(1,1);
800 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
811 if ( params
.BeforeFirst(wxT(',')).ToLong(&tmp
) )
815 if ( params
.AfterFirst(wxT(',')).ToLong(&tmp
) )
819 // skip the error message below
824 wxLogDebug(wxT("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
828 // return the value in the spin control if it is there (the text control otherwise)
829 wxString
wxGridCellNumberEditor::GetValue() const
836 long value
= Spin()->GetValue();
837 s
.Printf(wxT("%ld"), value
);
842 s
= Text()->GetValue();
848 // ----------------------------------------------------------------------------
849 // wxGridCellFloatEditor
850 // ----------------------------------------------------------------------------
852 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
855 m_precision
= precision
;
858 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
860 wxEvtHandler
* evtHandler
)
862 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
865 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
869 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
871 // first get the value
872 wxGridTableBase
* const table
= grid
->GetTable();
873 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
875 m_value
= table
->GetValueAsDouble(row
, col
);
881 const wxString value
= table
->GetValue(row
, col
);
882 if ( !value
.empty() )
884 if ( !value
.ToDouble(&m_value
) )
886 wxFAIL_MSG( wxT("this cell doesn't have float value") );
892 DoBeginEdit(GetString());
895 bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row
),
897 const wxGrid
* WXUNUSED(grid
),
898 const wxString
& oldval
, wxString
*newval
)
900 const wxString
text(Text()->GetValue());
905 if ( !text
.ToDouble(&value
) )
908 else // new value is empty string
910 if ( oldval
.empty() )
911 return false; // nothing changed
916 // the test for empty strings ensures that we don't skip the value setting
917 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
918 if ( wxIsSameDouble(value
, m_value
) && !text
.empty() && !oldval
.empty() )
919 return false; // nothing changed
929 void wxGridCellFloatEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
931 wxGridTableBase
* const table
= grid
->GetTable();
933 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
934 table
->SetValueAsDouble(row
, col
, m_value
);
936 table
->SetValue(row
, col
, Text()->GetValue());
939 void wxGridCellFloatEditor::Reset()
941 DoReset(GetString());
944 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
946 int keycode
= event
.GetKeyCode();
948 tmpbuf
[0] = (char) keycode
;
950 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
953 bool is_decimal_point
= ( strbuf
==
954 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
956 bool is_decimal_point
= ( strbuf
== wxT(".") );
959 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
960 || is_decimal_point
)
962 wxGridCellTextEditor::StartingKey(event
);
971 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
982 if ( params
.BeforeFirst(wxT(',')).ToLong(&tmp
) )
986 if ( params
.AfterFirst(wxT(',')).ToLong(&tmp
) )
988 m_precision
= (int)tmp
;
990 // skip the error message below
995 wxLogDebug(wxT("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
999 wxString
wxGridCellFloatEditor::GetString() const
1002 if ( m_precision
== -1 && m_width
!= -1)
1004 // default precision
1005 fmt
.Printf(wxT("%%%d.f"), m_width
);
1007 else if ( m_precision
!= -1 && m_width
== -1)
1010 fmt
.Printf(wxT("%%.%df"), m_precision
);
1012 else if ( m_precision
!= -1 && m_width
!= -1 )
1014 fmt
.Printf(wxT("%%%d.%df"), m_width
, m_precision
);
1018 // default width/precision
1022 return wxString::Format(fmt
, m_value
);
1025 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1027 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1029 const int keycode
= event
.GetKeyCode();
1030 if ( wxIsascii(keycode
) )
1033 const wxString decimalPoint
=
1034 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1036 const wxString
decimalPoint(wxT('.'));
1039 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1040 if ( wxIsdigit(keycode
) ||
1041 tolower(keycode
) == 'e' ||
1042 keycode
== decimalPoint
||
1054 #endif // wxUSE_TEXTCTRL
1058 // ----------------------------------------------------------------------------
1059 // wxGridCellBoolEditor
1060 // ----------------------------------------------------------------------------
1062 // the default values for GetValue()
1063 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { wxT(""), wxT("1") };
1065 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1067 wxEvtHandler
* evtHandler
)
1069 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1070 wxDefaultPosition
, wxDefaultSize
,
1073 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1076 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1078 bool resize
= false;
1079 wxSize size
= m_control
->GetSize();
1080 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1082 // check if the checkbox is not too big/small for this cell
1083 wxSize sizeBest
= m_control
->GetBestSize();
1084 if ( !(size
== sizeBest
) )
1086 // reset to default size if it had been made smaller
1092 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1094 // leave 1 pixel margin
1095 size
.x
= size
.y
= minSize
- 2;
1102 m_control
->SetSize(size
);
1105 // position it in the centre of the rectangle (TODO: support alignment?)
1107 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1108 // the checkbox without label still has some space to the right in wxGTK,
1109 // so shift it to the right
1111 #elif defined(__WXMSW__)
1112 // here too, but in other way
1117 int hAlign
= wxALIGN_CENTRE
;
1118 int vAlign
= wxALIGN_CENTRE
;
1120 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1123 if (hAlign
== wxALIGN_LEFT
)
1131 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1133 else if (hAlign
== wxALIGN_RIGHT
)
1135 x
= r
.x
+ r
.width
- size
.x
- 2;
1136 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1138 else if (hAlign
== wxALIGN_CENTRE
)
1140 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1141 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1144 m_control
->Move(x
, y
);
1147 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1149 m_control
->Show(show
);
1153 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1154 CBox()->SetBackgroundColour(colBg
);
1158 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1160 wxASSERT_MSG(m_control
,
1161 wxT("The wxGridCellEditor must be created first!"));
1163 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1165 m_value
= grid
->GetTable()->GetValueAsBool(row
, col
);
1169 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1171 if ( cellval
== ms_stringValues
[false] )
1173 else if ( cellval
== ms_stringValues
[true] )
1177 // do not try to be smart here and convert it to true or false
1178 // because we'll still overwrite it with something different and
1179 // this risks to be very surprising for the user code, let them
1181 wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") );
1185 CBox()->SetValue(m_value
);
1189 bool wxGridCellBoolEditor::EndEdit(int WXUNUSED(row
),
1191 const wxGrid
* WXUNUSED(grid
),
1192 const wxString
& WXUNUSED(oldval
),
1195 bool value
= CBox()->GetValue();
1196 if ( value
== m_value
)
1202 *newval
= GetValue();
1207 void wxGridCellBoolEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1209 wxGridTableBase
* const table
= grid
->GetTable();
1210 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1211 table
->SetValueAsBool(row
, col
, m_value
);
1213 table
->SetValue(row
, col
, GetValue());
1216 void wxGridCellBoolEditor::Reset()
1218 wxASSERT_MSG(m_control
,
1219 wxT("The wxGridCellEditor must be created first!"));
1221 CBox()->SetValue(m_value
);
1224 void wxGridCellBoolEditor::StartingClick()
1226 CBox()->SetValue(!CBox()->GetValue());
1229 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1231 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1233 int keycode
= event
.GetKeyCode();
1246 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1248 int keycode
= event
.GetKeyCode();
1252 CBox()->SetValue(!CBox()->GetValue());
1256 CBox()->SetValue(true);
1260 CBox()->SetValue(false);
1265 wxString
wxGridCellBoolEditor::GetValue() const
1267 return ms_stringValues
[CBox()->GetValue()];
1271 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1272 const wxString
& valueFalse
)
1274 ms_stringValues
[false] = valueFalse
;
1275 ms_stringValues
[true] = valueTrue
;
1279 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1281 return value
== ms_stringValues
[true];
1284 #endif // wxUSE_CHECKBOX
1288 // ----------------------------------------------------------------------------
1289 // wxGridCellChoiceEditor
1290 // ----------------------------------------------------------------------------
1292 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1294 : m_choices(choices
),
1295 m_allowOthers(allowOthers
) { }
1297 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1298 const wxString choices
[],
1300 : m_allowOthers(allowOthers
)
1304 m_choices
.Alloc(count
);
1305 for ( size_t n
= 0; n
< count
; n
++ )
1307 m_choices
.Add(choices
[n
]);
1312 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1314 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1315 editor
->m_allowOthers
= m_allowOthers
;
1316 editor
->m_choices
= m_choices
;
1321 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1323 wxEvtHandler
* evtHandler
)
1325 int style
= wxTE_PROCESS_ENTER
|
1329 if ( !m_allowOthers
)
1330 style
|= wxCB_READONLY
;
1331 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1332 wxDefaultPosition
, wxDefaultSize
,
1336 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1339 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1340 wxGridCellAttr
* attr
)
1342 // as we fill the entire client area, don't do anything here to minimize
1345 // TODO: It doesn't actually fill the client area since the height of a
1346 // combo always defaults to the standard. Until someone has time to
1347 // figure out the right rectangle to paint, just do it the normal way.
1348 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1351 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1353 wxASSERT_MSG(m_control
,
1354 wxT("The wxGridCellEditor must be created first!"));
1356 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1358 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1360 // Don't immediately end if we get a kill focus event within BeginEdit
1362 evtHandler
->SetInSetFocus(true);
1364 m_value
= grid
->GetTable()->GetValue(row
, col
);
1366 Reset(); // this updates combo box to correspond to m_value
1368 Combo()->SetFocus();
1372 // When dropping down the menu, a kill focus event
1373 // happens after this point, so we can't reset the flag yet.
1374 #if !defined(__WXGTK20__)
1375 evtHandler
->SetInSetFocus(false);
1380 bool wxGridCellChoiceEditor::EndEdit(int WXUNUSED(row
),
1382 const wxGrid
* WXUNUSED(grid
),
1383 const wxString
& WXUNUSED(oldval
),
1386 const wxString value
= Combo()->GetValue();
1387 if ( value
== m_value
)
1398 void wxGridCellChoiceEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1400 grid
->GetTable()->SetValue(row
, col
, m_value
);
1403 void wxGridCellChoiceEditor::Reset()
1407 Combo()->SetValue(m_value
);
1408 Combo()->SetInsertionPointEnd();
1410 else // the combobox is read-only
1412 // find the right position, or default to the first if not found
1413 int pos
= Combo()->FindString(m_value
);
1414 if (pos
== wxNOT_FOUND
)
1416 Combo()->SetSelection(pos
);
1420 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1430 wxStringTokenizer
tk(params
, wxT(','));
1431 while ( tk
.HasMoreTokens() )
1433 m_choices
.Add(tk
.GetNextToken());
1437 // return the value in the text control
1438 wxString
wxGridCellChoiceEditor::GetValue() const
1440 return Combo()->GetValue();
1443 #endif // wxUSE_COMBOBOX
1447 // ----------------------------------------------------------------------------
1448 // wxGridCellEnumEditor
1449 // ----------------------------------------------------------------------------
1451 // A cell editor which displays an enum number as a textual equivalent. eg
1452 // data in cell is 0,1,2 ... n the cell could be displayed as
1453 // "John","Fred"..."Bob" in the combo choice box
1455 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString
& choices
)
1456 :wxGridCellChoiceEditor()
1460 if (!choices
.empty())
1461 SetParameters(choices
);
1464 wxGridCellEditor
*wxGridCellEnumEditor::Clone() const
1466 wxGridCellEnumEditor
*editor
= new wxGridCellEnumEditor();
1467 editor
->m_index
= m_index
;
1471 void wxGridCellEnumEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1473 wxASSERT_MSG(m_control
,
1474 wxT("The wxGridCellEnumEditor must be Created first!"));
1476 wxGridTableBase
*table
= grid
->GetTable();
1478 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1480 m_index
= table
->GetValueAsLong(row
, col
);
1484 wxString startValue
= table
->GetValue(row
, col
);
1485 if (startValue
.IsNumber() && !startValue
.empty())
1487 startValue
.ToLong(&m_index
);
1495 Combo()->SetSelection(m_index
);
1496 Combo()->SetInsertionPointEnd();
1497 Combo()->SetFocus();
1501 bool wxGridCellEnumEditor::EndEdit(int WXUNUSED(row
),
1503 const wxGrid
* WXUNUSED(grid
),
1504 const wxString
& WXUNUSED(oldval
),
1507 long idx
= Combo()->GetSelection();
1508 if ( idx
== m_index
)
1514 newval
->Printf("%ld", m_index
);
1519 void wxGridCellEnumEditor::ApplyEdit(int row
, int col
, wxGrid
* grid
)
1521 wxGridTableBase
* const table
= grid
->GetTable();
1522 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1523 table
->SetValueAsLong(row
, col
, m_index
);
1525 table
->SetValue(row
, col
, wxString::Format("%ld", m_index
));
1528 #endif // wxUSE_COMBOBOX
1530 // ----------------------------------------------------------------------------
1531 // wxGridCellAutoWrapStringEditor
1532 // ----------------------------------------------------------------------------
1535 wxGridCellAutoWrapStringEditor::Create(wxWindow
* parent
,
1537 wxEvtHandler
* evtHandler
)
1539 wxGridCellTextEditor::DoCreate(parent
, id
, evtHandler
,
1540 wxTE_MULTILINE
| wxTE_RICH
);
1544 #endif // wxUSE_GRID