KiCad PCB EDA Suite
Loading...
Searching...
No Matches
filter_combobox.h
Go to the documentation of this file.
1
2/*
3 * This program source code file is part of KiCad, a free EDA CAD application.
4 *
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <memory>
24#include <optional>
25
26#include <wx/panel.h>
27#include <wx/combo.h>
28#include <wx/vlbox.h>
29
30#include <font/font.h>
31
32
33class wxTextValidator;
34class wxTextCtrl;
35class wxListBox;
36
37
38class FILTER_COMBOPOPUP_LISTBOX : public wxVListBox
39{
40public:
41 FILTER_COMBOPOPUP_LISTBOX( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos, const wxSize& aSize,
42 int aFlags ) :
43 wxVListBox( aParent, aId, aPos, aSize, aFlags | wxLB_OWNERDRAW )
44 {
46 []( const wxString& aItem ) -> int
47 {
48 return 0;
49 };
50 }
51
52 void Set( const wxArrayString& aChoices )
53 {
54 m_choices = aChoices;
55 SetItemCount( m_choices.size() );
56
57 RefreshAll();
58 }
59
60 void SetDisplayStyleCallback( const std::function<int( const wxString& aItem )>& aCallback );
61
62 size_t GetCount() const
63 {
64 return m_choices.size();
65 }
66
67 wxString GetString( size_t aIdx ) const
68 {
69 if( aIdx < m_choices.size() )
70 return m_choices[aIdx];
71 else
72 return wxEmptyString;
73 }
74
75 void SetStringSelection( const wxString& aString )
76 {
77 for( int ii = 0; ii < (int) m_choices.size(); ++ii )
78 {
79 if( m_choices[ii] == aString )
80 {
81 SetSelection( ii );
82 return;
83 }
84 }
85
86 SetSelection( -1 );
87 }
88
89 int HitTest( const wxPoint& aPoint ) const;
90
91protected:
92 wxCoord OnMeasureItem( size_t aItem ) const override;
93
94 void OnDrawItem( wxDC& aDC, const wxRect& aRect, size_t aItem ) const override;
95
96 void OnDrawBackground( wxDC& aDC, const wxRect& aRect, size_t aItem ) const override;
97
98private:
99 wxArrayString m_choices;
100 std::function<int( const wxString& aItem )> m_displayStyleCallback;
101};
102
103
104class FILTER_COMBOPOPUP : public wxPanel, public wxComboPopup
105{
106public:
108
109 bool Create( wxWindow* aParent ) override;
110
111 wxWindow* GetControl() override { return this; }
112
113 void SetStringList( const wxArrayString& aStringList );
114
115 wxString GetStringValue() const override;
116 void SetStringValue( const wxString& aNetName ) override;
117
118 void SetSelectedString( const wxString& aString );
119
120 void SetDisplayStyleCallback( const std::function<int( const wxString& aItem )>& aCallback );
121
122 void OnPopup() override;
123
124 void OnStartingKey( wxKeyEvent& aEvent );
125
126 wxSize GetAdjustedSize( int aMinWidth, int aPrefHeight, int aMaxHeight ) override;
127
128 virtual void Accept();
129
130protected:
134 std::optional<wxString> getSelectedValue() const;
135
139 wxString getFilterValue() const;
140
144 virtual void getListContent( wxArrayString& aStringList );
145
149 void rebuildList();
150
151private:
152 wxSize updateSize();
153
154 void onIdle( wxIdleEvent& aEvent );
155
156 // Hot-track the mouse (for focus and listbox selection)
157 void onMouseMoved( const wxPoint aScreenPos );
158 void onMouseClick( wxMouseEvent& aEvent );
159 void onKeyDown( wxKeyEvent& aEvent );
160 void onEnter( wxCommandEvent& aEvent );
161 void onFilterEdit( wxCommandEvent& aEvent );
162 void doStartingKey( wxKeyEvent& aEvent );
163 void doSetFocus( wxWindow* aWindow );
164
165protected:
166 wxTextValidator* m_filterValidator;
167 wxTextCtrl* m_filterCtrl;
171
172 wxEvtHandler* m_focusHandler;
173
175 wxArrayString m_stringList;
176
177 std::function<int( const wxString& aItem )> m_displayStyleCallback;
178};
179
180
181wxDECLARE_EVENT( FILTERED_ITEM_SELECTED, wxCommandEvent );
182
189class FILTER_COMBOBOX : public wxComboCtrl
190{
191public:
192 // C'tor matching wxFormBuilder's Custom Control
193 FILTER_COMBOBOX( wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
194 const wxSize& size = wxDefaultSize, long style = 0 );
195
196 // C'tor matching wxFormBuilder's ComboxBox.
197 FILTER_COMBOBOX( wxWindow* parent, wxWindowID id, const wxString& value,
198 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
199 int count = 0, wxString strings[] = nullptr, long style = 0 );
200
202
203 virtual void SetStringList( const wxArrayString& aStringList );
204
205 void SetDisplayStyleCallback( const std::function<int( const wxString& aItem )>& aCallback );
206
207 virtual void SetSelectedString( const wxString& aString );
208
209protected:
210 void setFilterPopup( FILTER_COMBOPOPUP* aPopup );
211
212 void onKeyDown( wxKeyEvent& aEvt );
213
214protected:
216};
virtual void SetSelectedString(const wxString &aString)
FILTER_COMBOPOPUP * m_filterPopup
void setFilterPopup(FILTER_COMBOPOPUP *aPopup)
virtual void SetStringList(const wxArrayString &aStringList)
FILTER_COMBOBOX(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0)
void onKeyDown(wxKeyEvent &aEvt)
void SetDisplayStyleCallback(const std::function< int(const wxString &aItem)> &aCallback)
FILTER_COMBOPOPUP_LISTBOX(wxWindow *aParent, wxWindowID aId, const wxPoint &aPos, const wxSize &aSize, int aFlags)
void OnDrawBackground(wxDC &aDC, const wxRect &aRect, size_t aItem) const override
wxString GetString(size_t aIdx) const
void SetStringSelection(const wxString &aString)
std::function< int(const wxString &aItem)> m_displayStyleCallback
wxCoord OnMeasureItem(size_t aItem) const override
int HitTest(const wxPoint &aPoint) const
void Set(const wxArrayString &aChoices)
void OnDrawItem(wxDC &aDC, const wxRect &aRect, size_t aItem) const override
void SetDisplayStyleCallback(const std::function< int(const wxString &aItem)> &aCallback)
void SetSelectedString(const wxString &aString)
void onEnter(wxCommandEvent &aEvent)
wxTextValidator * m_filterValidator
void onFilterEdit(wxCommandEvent &aEvent)
void SetStringValue(const wxString &aNetName) override
virtual void getListContent(wxArrayString &aStringList)
Fill the combobox list.
void onMouseClick(wxMouseEvent &aEvent)
void doStartingKey(wxKeyEvent &aEvent)
void rebuildList()
Call this to rebuild the list from the getListContent() method.
wxString getFilterValue() const
Get the current value of the filter control.
virtual void Accept()
void OnStartingKey(wxKeyEvent &aEvent)
void SetDisplayStyleCallback(const std::function< int(const wxString &aItem)> &aCallback)
std::optional< wxString > getSelectedValue() const
Get the currently selected value in the list, or std::nullopt.
void onIdle(wxIdleEvent &aEvent)
wxString GetStringValue() const override
wxArrayString m_stringList
wxSize GetAdjustedSize(int aMinWidth, int aPrefHeight, int aMaxHeight) override
std::function< int(const wxString &aItem)> m_displayStyleCallback
wxEvtHandler * m_focusHandler
wxWindow * GetControl() override
FILTER_COMBOPOPUP_LISTBOX * m_listBox
bool Create(wxWindow *aParent) override
void OnPopup() override
void doSetFocus(wxWindow *aWindow)
void SetStringList(const wxArrayString &aStringList)
void onKeyDown(wxKeyEvent &aEvent)
void onMouseMoved(const wxPoint aScreenPos)
wxTextCtrl * m_filterCtrl
wxDECLARE_EVENT(FILTERED_ITEM_SELECTED, wxCommandEvent)