KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_list_dialog.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) 2007 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <eda_list_dialog.h>
27#include <eda_draw_frame.h>
28#include <string_utils.h>
29#include <macros.h>
31
32// wxWidgets spends *far* too long calculating column widths (most of it, believe it or
33// not, in repeatedly creating/destroying a wxDC to do the measurement in).
34// Use default column widths instead.
35static int DEFAULT_SINGLE_COL_WIDTH = 260;
36static int DEFAULT_COL_WIDTHS[] = { 200, 300 };
37
38
39EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle,
40 const wxArrayString& aItemHeaders,
41 const std::vector<wxArrayString>& aItemList,
42 const wxString& aPreselectText, bool aSortList ) :
43 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
44 m_sortList( aSortList )
45{
46 m_filterBox->SetHint( _( "Filter" ) );
47
48 initDialog( aItemHeaders, aItemList, aPreselectText );
49
50 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
51 // because so many dialogs share this same class, with different numbers of
52 // columns, different column names, and column widths.
53 m_hash_key = TO_UTF8( aTitle );
54
56
57 Layout();
58 GetSizer()->Fit( this );
59
60 Centre();
61}
62
63
64EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aSortList ) :
65 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
66 m_sortList( aSortList )
67{
68 m_filterBox->SetHint( _( "Filter" ) );
69}
70
71
72bool EDA_LIST_DIALOG::Show( bool show )
73{
74 bool retVal = DIALOG_SHIM::Show( show );
75
76 if( show )
77 {
78 wxSizeEvent dummy;
79 onSize( dummy );
80 }
81
82 return retVal;
83}
84
85
86void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders,
87 const std::vector<wxArrayString>& aItemList,
88 const wxString& aPreselectText )
89{
90 if( aItemHeaders.Count() == 1 )
91 {
92 m_listBox->InsertColumn( 0, aItemHeaders.Item( 0 ), wxLIST_FORMAT_LEFT,
94
95 m_listBox->SetMinClientSize( wxSize( DEFAULT_SINGLE_COL_WIDTH, 200 ) );
96 SetMinClientSize( wxSize( DEFAULT_COL_WIDTHS[0], 220 ) );
97 }
98 else if( aItemHeaders.Count() == 2 )
99 {
100 for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
101 {
102 m_listBox->InsertColumn( i, aItemHeaders.Item( i ), wxLIST_FORMAT_LEFT,
103 DEFAULT_COL_WIDTHS[ i ] );
104 }
105
106 m_listBox->SetMinClientSize( wxSize( DEFAULT_COL_WIDTHS[0] * 3, 200 ) );
107 SetMinClientSize( wxSize( DEFAULT_COL_WIDTHS[0] * 2, 220 ) );
108 }
109
110 m_itemsList = aItemList;
112
113 if( !aPreselectText.IsEmpty() )
114 {
115 long sel = m_listBox->FindItem( -1, aPreselectText );
116
117 if( sel != wxNOT_FOUND )
118 {
119 m_listBox->SetItemState( sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
120
121 // Set to a small size so EnsureVisible() won't be foiled by later additions.
122 // ListBox will expand to fit later.
123 m_listBox->SetSize( m_listBox->GetSize().GetX(), 100 );
124 m_listBox->EnsureVisible( sel );
125 }
126 }
127}
128
129
130void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
131{
132 m_listLabel->SetLabel( aLabel );
133 m_listBox->SetSingleStyle( wxLC_NO_HEADER, true );
134}
135
136
137void EDA_LIST_DIALOG::SetOKLabel( const wxString& aLabel )
138{
139 m_sdbSizerOK->SetLabel( aLabel );
140}
141
142
144{
145 m_filterBox->Hide();
146}
147
148
149void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
150{
151 wxString filter;
152 wxString itemName;
153
154 filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
155
156 m_listBox->DeleteAllItems();
157
158 for( const wxArrayString& row : m_itemsList )
159 {
160 itemName = row.Item( 0 );
161
162 if( itemName.MakeLower().Matches( filter ) )
163 Append( row );
164 }
165
166 sortList();
167}
168
169
171{
172 return m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
173}
174
175
177{
178 wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
179 wxT( "Invalid list control column." ) );
180
181 long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
182 wxString text;
183
184 if( item >= 0 ) // if something is selected.
185 {
186 wxListItem info;
187
188 info.m_mask = wxLIST_MASK_TEXT;
189 info.m_itemId = item;
190 info.m_col = aColumn;
191
192 if( m_listBox->GetItem( info ) )
193 text = info.m_text;
194
197 }
198
199 return text;
200}
201
202
203void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
204{
205 long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
206
207 m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
208
209 // Adding the next columns content
210 for( unsigned i = 1; i < itemList.size(); i++ )
211 m_listBox->SetItem( itemIndex, i, itemList[i] );
212}
213
214
215void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
216{
217 for( unsigned row = 0; row < itemList.size(); row++ )
218 {
219 wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
220
221 for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
222 {
223 wxListItem info;
224 info.m_itemId = row + position;
225 info.m_col = col;
226 info.m_text = itemList[row].Item( col );
227 info.m_width = DEFAULT_COL_WIDTHS[ col ];
228 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
229
230 if( col == 0 )
231 {
232 info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
233 info.m_mask |= wxLIST_MASK_DATA;
234
235 m_listBox->InsertItem( info );
236 }
237 else
238 {
239 m_listBox->SetItem( info );
240 }
241 }
242 }
243
244 sortList();
245}
246
247
248void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
249{
250 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
251}
252
253
254void EDA_LIST_DIALOG::onSize( wxSizeEvent& event )
255{
256 if( m_listBox->GetColumnCount() == 1 )
257 {
258 m_listBox->SetColumnWidth( 0, m_listBox->GetClientSize().x );
259 }
260 else if( m_listBox->GetColumnCount() == 2 )
261 {
262 int first = KiROUND( m_listBox->GetClientSize().x * 0.42 );
263 m_listBox->SetColumnWidth( 0, first );
264 m_listBox->SetColumnWidth( 1, m_listBox->GetClientSize().x - first );
265 }
266
267 event.Skip();
268}
269
270
271/*
272 * Sort alphabetically, case insensitive.
273 */
274static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2,
275 wxIntPtr WXUNUSED( aSortData ) )
276{
277 wxString* component1Name = (wxString*) aItem1;
278 wxString* component2Name = (wxString*) aItem2;
279
280 return StrNumCmp( *component1Name, *component2Name, true );
281}
282
283
285{
286 if( m_sortList )
287 m_listBox->SortItems( myCompareFunction, 0 );
288}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
bool Show(bool show) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
Definition: dialog_shim.h:213
Class EDA_LIST_DIALOG_BASE.
void textChangeInFilterBox(wxCommandEvent &event) override
bool Show(bool show) override
void SetOKLabel(const wxString &aLabel)
void initDialog(const wxArrayString &aItemHeaders, const std::vector< wxArrayString > &aItemList, const wxString &aPreselectText)
wxString GetTextSelection(int aColumn=0)
Return the selected text from aColumn in the wxListCtrl in the dialog.
void onListItemActivated(wxListEvent &event) override
void SetListLabel(const wxString &aLabel)
virtual void onSize(wxSizeEvent &event) override
std::vector< wxArrayString > m_itemsList
void Append(const wxArrayString &aItemStr)
void InsertItems(const std::vector< wxArrayString > &aItemList, int aPosition=0)
EDA_LIST_DIALOG(wxWindow *aParent, const wxString &aTitle, const wxArrayString &aItemHeaders, const std::vector< wxArrayString > &aItemList, const wxString &aPreselectText=wxEmptyString, bool aSortList=true)
static const wxString GetPinningSymbol()
#define _(s)
static int wxCALLBACK myCompareFunction(wxIntPtr aItem1, wxIntPtr aItem2, wxIntPtr WXUNUSED(aSortData))
static int DEFAULT_SINGLE_COL_WIDTH
static int DEFAULT_COL_WIDTHS[]
This file contains miscellaneous commonly used macros and functions.
std::vector< FAB_LAYER_COLOR > dummy
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398