KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_combobox.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26
27//-------- Renderer ---------------------------------------------------------------------
28// None required; just render as normal text.
29
30
31
32//-------- Editor -----------------------------------------------------------------------
33//
34// Note: this implementation is an adaptation of wxGridCellChoiceEditor
35
36
37GRID_CELL_COMBOBOX::GRID_CELL_COMBOBOX( const wxArrayString& names ) :
38 m_names( names )
39{
40}
41
42
44
45
46wxGridCellEditor* GRID_CELL_COMBOBOX::Clone() const
47{
48 return new GRID_CELL_COMBOBOX( m_names );
49}
50
51
52void GRID_CELL_COMBOBOX::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler )
53{
54 m_control = new wxComboBox( aParent, wxID_ANY, wxEmptyString,
55 wxDefaultPosition, wxDefaultSize, m_names );
56
57 wxGridCellEditor::Create(aParent, aId, aEventHandler);
58}
59
60
62{
63 return Combo()->GetValue();
64}
65
66
67void GRID_CELL_COMBOBOX::SetSize( const wxRect& aRect )
68{
69 wxRect rect( aRect );
70 rect.Inflate( -1 );
71
72#if defined( __WXMAC__ )
73 rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
74#endif
75
76 Combo()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
77}
78
79
80void GRID_CELL_COMBOBOX::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
81{
82 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
83
84 // Don't immediately end if we get a kill focus event within BeginEdit
85 evtHandler->SetInSetFocus( true );
86
87 // These event handlers are needed to properly dismiss the editor when the popup is closed
88 m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_COMBOBOX::onComboDropDown, this);
89 m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_COMBOBOX::onComboCloseUp, this);
90
91 m_value = aGrid->GetTable()->GetValue( aRow, aCol );
92
93 Combo()->SetFocus();
94
95 // Work around a wxWidgets bug where the drop-down is the wrong width on the first drop.
96 Combo()->Set( Combo()->GetStrings() );
97
98 Combo()->ChangeValue( m_value );
99 Combo()->SelectAll();
100
101#ifdef __WXOSX_COCOA__
102 // This is a work around for the combobox being simply dismissed when a
103 // choice is made in it under OS X. The bug is almost certainly due to a
104 // problem in focus events generation logic but it's not obvious to fix and
105 // for now this at least allows one to use wxGrid.
106 Combo()->Popup();
107#endif
108
109 // When dropping down the menu, a kill focus event
110 // happens after this point, so we can't reset the flag yet.
111#if !defined(__WXGTK__)
112 evtHandler->SetInSetFocus( false );
113#endif
114}
115
116
117bool GRID_CELL_COMBOBOX::EndEdit( int , int , const wxGrid* , const wxString& , wxString *aNewVal )
118{
119 const wxString value = Combo()->GetValue();
120
121 if( value == m_value )
122 return false;
123
124 m_value = value;
125
126 if( aNewVal )
127 *aNewVal = value;
128
129 return true;
130}
131
132
133void GRID_CELL_COMBOBOX::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
134{
135 aGrid->GetTable()->SetValue( aRow, aCol, m_value );
136}
137
138
142
143
144void GRID_CELL_COMBOBOX::onComboDropDown( wxCommandEvent& aEvent )
145{
146 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
147
148 // Once the combobox is dropped, reset the flag to allow the focus-loss handler
149 // to function and close the editor.
150 evtHandler->SetInSetFocus( false );
151}
152
153
154void GRID_CELL_COMBOBOX::onComboCloseUp( wxCommandEvent& aEvent )
155{
156 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
157
158 // Forward the combobox close up event to the cell event handler as a focus kill event
159 // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
160 // dropdown arrow visible in the cell.
161 wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
162 event.SetEventObject( m_control );
163 evtHandler->ProcessEvent( event );
164}
bool EndEdit(int aRow, int aCol, const wxGrid *, const wxString &, wxString *aNewVal) override
void onComboDropDown(wxCommandEvent &aEvent)
GRID_CELL_COMBOBOX(const wxArrayString &names)
~GRID_CELL_COMBOBOX() override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
void onComboCloseUp(wxCommandEvent &aEvent)
wxString GetValue() const override
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
wxArrayString m_names
wxComboBox * Combo() const
void SetSize(const wxRect &aRect) override
void Reset() override
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
wxGridCellEditor * Clone() const override