KiCad PCB EDA Suite
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 (C) 2018 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 
24 #include <widgets/grid_combobox.h>
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 
37 GRID_CELL_COMBOBOX::GRID_CELL_COMBOBOX( const wxArrayString& names ) :
38  m_names( names )
39 {
40 }
41 
42 
43 wxGridCellEditor* GRID_CELL_COMBOBOX::Clone() const
44 {
45  return new GRID_CELL_COMBOBOX( m_names );
46 }
47 
48 
49 void GRID_CELL_COMBOBOX::Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler )
50 {
51  m_control = new wxComboBox( aParent, wxID_ANY, wxEmptyString,
52  wxDefaultPosition, wxDefaultSize, m_names );
53 
54  wxGridCellEditor::Create(aParent, aId, aEventHandler);
55 }
56 
58 {
59  return Combo()->GetValue();
60 }
61 
62 void GRID_CELL_COMBOBOX::SetSize( const wxRect& aRect )
63 {
64  wxRect rect( aRect );
65  rect.Inflate( -1 );
66 
67 #if defined( __WXMAC__ )
68  rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
69 #endif
70 
71  Combo()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
72 }
73 
74 
75 void GRID_CELL_COMBOBOX::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
76 {
77  auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
78 
79  // Don't immediately end if we get a kill focus event within BeginEdit
80  evtHandler->SetInSetFocus( true );
81 
82  // These event handlers are needed to properly dismiss the editor when the popup is closed
83  m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_COMBOBOX::onComboDropDown, this);
84  m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_COMBOBOX::onComboCloseUp, this);
85 
86  m_value = aGrid->GetTable()->GetValue( aRow, aCol );
87 
88  Combo()->SetFocus();
89 
90 #ifdef __WXOSX_COCOA__
91  // This is a work around for the combobox being simply dismissed when a
92  // choice is made in it under OS X. The bug is almost certainly due to a
93  // problem in focus events generation logic but it's not obvious to fix and
94  // for now this at least allows to use wxGrid.
95  Combo()->Popup();
96 #endif
97 
98  // When dropping down the menu, a kill focus event
99  // happens after this point, so we can't reset the flag yet.
100 #if !defined(__WXGTK20__)
101  evtHandler->SetInSetFocus( false );
102 #endif
103 }
104 
105 
106 bool GRID_CELL_COMBOBOX::EndEdit( int , int , const wxGrid* , const wxString& , wxString *aNewVal )
107 {
108  const wxString value = Combo()->GetValue();
109 
110  if( value == m_value )
111  return false;
112 
113  m_value = value;
114 
115  if( aNewVal )
116  *aNewVal = value;
117 
118  return true;
119 }
120 
121 
122 void GRID_CELL_COMBOBOX::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
123 {
124  aGrid->GetTable()->SetValue( aRow, aCol, m_value );
125 }
126 
127 
129 {
130 }
131 
132 
133 void GRID_CELL_COMBOBOX::onComboDropDown( wxCommandEvent& aEvent )
134 {
135  auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
136 
137  // Once the combobox is dropped, reset the flag to allow the focus-loss handler
138  // to function and close the editor.
139  evtHandler->SetInSetFocus( false );
140 }
141 
142 
143 void GRID_CELL_COMBOBOX::onComboCloseUp( wxCommandEvent& aEvent )
144 {
145  auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
146 
147  // Forward the combobox close up event to the cell event handler as a focus kill event
148  // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
149  // dropdown arrow visible in the cell.
150  wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
151  event.SetEventObject( m_control );
152  evtHandler->ProcessEvent( event );
153 }
void Reset() override
void onComboDropDown(wxCommandEvent &aEvent)
wxString GetValue() const override
wxArrayString m_names
Definition: grid_combobox.h:58
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
void SetSize(const wxRect &aRect) override
void onComboCloseUp(wxCommandEvent &aEvent)
GRID_CELL_COMBOBOX(const wxArrayString &names)
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
wxGridCellEditor * Clone() const override
wxComboBox * Combo() const
Definition: grid_combobox.h:53
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *aNewVal) override