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, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <algorithm>
22#include <wx/dc.h>
23#include <wx/grid.h>
24#include <wx/renderer.h>
25
26
27//-------- Renderer ---------------------------------------------------------------------
28
29wxGridCellRenderer* GRID_CELL_COMBOBOX_RENDERER::Clone() const
30{
31 return new GRID_CELL_COMBOBOX_RENDERER( *this );
32}
33
34
35void GRID_CELL_COMBOBOX_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC, const wxRect& aRect, int aRow,
36 int aCol, bool isSelected )
37{
38 int arrowWidth = aRect.GetHeight();
39
40 wxRect textRect = aRect;
41 textRect.width = std::max( 0, textRect.width - arrowWidth );
42
43 wxGridCellStringRenderer::Draw( aGrid, aAttr, aDC, textRect, aRow, aCol, isSelected );
44
45 wxRect arrowRect = aRect;
46 arrowRect.x += textRect.width;
47 arrowRect.width = arrowWidth;
48
49 aDC.DrawRectangle( arrowRect ); // needed at least on wxMSW to draw the background with current brush
50 // DrawDropArrow uses a transparent brush to draw the background
51 wxRendererNative::Get().DrawDropArrow( &aGrid, aDC, arrowRect );
52}
53
54
55wxSize GRID_CELL_COMBOBOX_RENDERER::GetBestSize( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC, int aRow, int aCol )
56{
57 wxSize size = wxGridCellStringRenderer::GetBestSize( aGrid, aAttr, aDC, aRow, aCol );
58
59 size.x += size.y; // Reserve a square area for the always-visible dropdown arrow.
60
61 return size;
62}
63
64
65//-------- Editor -----------------------------------------------------------------------
66//
67// Note: this implementation is an adaptation of wxGridCellChoiceEditor
68
69
70GRID_CELL_COMBOBOX::GRID_CELL_COMBOBOX( const wxArrayString& names ) :
71 m_names( names )
72{
73}
74
75
77
78
79wxGridCellEditor* GRID_CELL_COMBOBOX::Clone() const
80{
81 return new GRID_CELL_COMBOBOX( m_names );
82}
83
84
85void GRID_CELL_COMBOBOX::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler )
86{
87 m_control = new wxComboBox( aParent, wxID_ANY, wxEmptyString,
88 wxDefaultPosition, wxDefaultSize, m_names );
89
90 wxGridCellEditor::Create(aParent, aId, aEventHandler);
91}
92
93
95{
96 return Combo()->GetValue();
97}
98
99
100void GRID_CELL_COMBOBOX::SetSize( const wxRect& aRect )
101{
102 wxRect rect( aRect );
103 rect.Inflate( -1 );
104
105#if defined( __WXMAC__ )
106 rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
107#endif
108
109 Combo()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
110}
111
112
113void GRID_CELL_COMBOBOX::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
114{
115 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
116
117 // Don't immediately end if we get a kill focus event within BeginEdit
118 evtHandler->SetInSetFocus( true );
119
120 // These event handlers are needed to properly dismiss the editor when the popup is closed
121 m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_COMBOBOX::onComboDropDown, this);
122 m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_COMBOBOX::onComboCloseUp, this);
123
124 m_value = aGrid->GetTable()->GetValue( aRow, aCol );
125
126 Combo()->SetFocus();
127
128 // Work around a wxWidgets bug where the drop-down is the wrong width on the first drop.
129 Combo()->Set( Combo()->GetStrings() );
130
131 Combo()->ChangeValue( m_value );
132 Combo()->SelectAll();
133
134#ifdef __WXOSX_COCOA__
135 // This is a work around for the combobox being simply dismissed when a
136 // choice is made in it under OS X. The bug is almost certainly due to a
137 // problem in focus events generation logic but it's not obvious to fix and
138 // for now this at least allows one to use wxGrid.
139 Combo()->Popup();
140#endif
141
142 // When dropping down the menu, a kill focus event
143 // happens after this point, so we can't reset the flag yet.
144#if !defined(__WXGTK__)
145 evtHandler->SetInSetFocus( false );
146#endif
147}
148
149
150bool GRID_CELL_COMBOBOX::EndEdit( int , int , const wxGrid* , const wxString& , wxString *aNewVal )
151{
152 const wxString value = Combo()->GetValue();
153
154 if( value == m_value )
155 return false;
156
157 m_value = value;
158
159 if( aNewVal )
160 *aNewVal = value;
161
162 return true;
163}
164
165
166void GRID_CELL_COMBOBOX::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
167{
168 aGrid->GetTable()->SetValue( aRow, aCol, m_value );
169}
170
171
175
176
177void GRID_CELL_COMBOBOX::onComboDropDown( wxCommandEvent& aEvent )
178{
179 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
180
181 // Once the combobox is dropped, reset the flag to allow the focus-loss handler
182 // to function and close the editor.
183 evtHandler->SetInSetFocus( false );
184}
185
186
187void GRID_CELL_COMBOBOX::onComboCloseUp( wxCommandEvent& aEvent )
188{
189 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
190
191 // Forward the combobox close up event to the cell event handler as a focus kill event
192 // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
193 // dropdown arrow visible in the cell.
194 wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
195 event.SetEventObject( m_control );
196 evtHandler->ProcessEvent( event );
197}
Renders a combobox cell's text with an always-visible dropdown arrow, so it reads as a dropdown even ...
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
wxSize GetBestSize(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, int aRow, int aCol) override
wxGridCellRenderer * Clone() const override
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