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 The 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>
30#include <wx/checkbox.h>
32
33// wxWidgets spends *far* too long calculating column widths (most of it, believe it or
34// not, in repeatedly creating/destroying a wxDC to do the measurement in).
35// Use default column widths instead.
36// Note, these are "standard DPI" widths
37static int DEFAULT_SINGLE_COL_WIDTH = 260;
38static int DEFAULT_COL_WIDTHS[] = { 200, 300 };
39
40
41EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle,
42 const wxArrayString& aItemHeaders,
43 const std::vector<wxArrayString>& aItemList,
44 const wxString& aPreselectText, bool aSortList,
45 const std::vector<std::pair<wxString, bool*>>& aExtraCheckboxes ) :
46 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
47 m_sortList( aSortList )
48{
49 // correct for wxfb not supporting FromDIP
50 m_listBox->SetMinSize( FromDIP( m_listBox->GetMinSize() ) );
51 m_filterBox->SetHint( _( "Filter" ) );
52
53 initDialog( aItemHeaders, aItemList, aPreselectText );
54
55 if( aItemList.size() > 16 )
56 {
57 m_listBox->SetMinSize( wxSize( m_listBox->GetMinWidth(),
58 KiROUND( m_listBox->GetMinHeight() * 1.66 ) ) );
59 }
60
61 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
62 // because so many dialogs share this same class, with different numbers of
63 // columns, different column names, and column widths.
64 m_hash_key = TO_UTF8( aTitle );
65
66 if( !aExtraCheckboxes.empty() )
67 {
68 m_ExtrasSizer->AddSpacer( 5 );
69
70 for( const auto& [label, valuePtr] : aExtraCheckboxes )
71 {
72 wxCheckBox* cb = new wxCheckBox( this, wxID_ANY, label );
73 cb->SetValue( *valuePtr );
74 m_ExtrasSizer->Add( cb, 0, wxBOTTOM, 5 );
75 m_extraCheckboxMap[cb] = valuePtr;
76 }
77 }
78
80
81 Layout();
82 GetSizer()->Fit( this );
83
84 Centre();
85}
86
87
88EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aSortList ) :
89 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
90 m_sortList( aSortList )
91{
92 m_filterBox->SetHint( _( "Filter" ) );
93}
94
95
96bool EDA_LIST_DIALOG::Show( bool show )
97{
98 bool retVal = DIALOG_SHIM::Show( show );
99
100 if( show )
101 {
102 wxSizeEvent dummy;
103 onSize( dummy );
104 }
105
106 return retVal;
107}
108
109
110void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders,
111 const std::vector<wxArrayString>& aItemList,
112 const wxString& aPreselectText )
113{
114 if( aItemHeaders.Count() == 1 )
115 {
116 m_listBox->InsertColumn( 0, aItemHeaders.Item( 0 ), wxLIST_FORMAT_LEFT,
117 FromDIP( DEFAULT_SINGLE_COL_WIDTH ) );
118
119 m_listBox->SetMinClientSize( FromDIP( wxSize( DEFAULT_SINGLE_COL_WIDTH, 200 ) ) );
120 SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0], 220 ) ) );
121 }
122 else if( aItemHeaders.Count() == 2 )
123 {
124 for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
125 {
126 m_listBox->InsertColumn( i, aItemHeaders.Item( i ), wxLIST_FORMAT_LEFT,
127 FromDIP( DEFAULT_COL_WIDTHS[ i ] ) );
128 }
129
130 m_listBox->SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0] * 3, 200 ) ) );
131 SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0] * 2, 220 ) ) );
132 }
133
134 m_itemsList = aItemList;
136
137 if( !aPreselectText.IsEmpty() )
138 {
139 long sel = m_listBox->FindItem( -1, aPreselectText );
140
141 if( sel != wxNOT_FOUND )
142 {
143 m_listBox->SetItemState( sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
144
145 // Set to a small size so EnsureVisible() won't be foiled by later additions.
146 // ListBox will expand to fit later.
147 m_listBox->SetSize( m_listBox->GetSize().GetX(), FromDIP( 100 ) );
148 m_listBox->EnsureVisible( sel );
149 }
150 }
151}
152
153
154void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
155{
156 m_listLabel->SetLabel( aLabel );
157 m_listBox->SetSingleStyle( wxLC_NO_HEADER, true );
158}
159
160
161void EDA_LIST_DIALOG::SetOKLabel( const wxString& aLabel )
162{
163 m_sdbSizerOK->SetLabel( aLabel );
164}
165
166
168{
169 m_filterBox->Hide();
170}
171
172
173void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
174{
175 wxString filter;
176 wxString itemName;
177
178 filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
179
180 m_listBox->DeleteAllItems();
181
182 for( const wxArrayString& row : m_itemsList )
183 {
184 itemName = row.Item( 0 );
185
186 if( itemName.MakeLower().Matches( filter ) )
187 Append( row );
188 }
189
190 sortList();
191}
192
193
195{
196 return m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
197}
198
199
201{
202 wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
203 wxT( "Invalid list control column." ) );
204
205 long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
206 wxString text;
207
208 if( item >= 0 ) // if something is selected.
209 {
210 wxListItem info;
211
212 info.m_mask = wxLIST_MASK_TEXT;
213 info.m_itemId = item;
214 info.m_col = aColumn;
215
216 if( m_listBox->GetItem( info ) )
217 text = info.m_text;
218
221 }
222
223 return text;
224}
225
226
228{
229 for( const auto& [checkbox, valuePtr] : m_extraCheckboxMap )
230 *valuePtr = checkbox->GetValue();
231}
232
233
234void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
235{
236 long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
237
238 m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
239
240 // Adding the next columns content
241 for( unsigned i = 1; i < itemList.size(); i++ )
242 m_listBox->SetItem( itemIndex, i, itemList[i] );
243}
244
245
246void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
247{
248 for( unsigned row = 0; row < itemList.size(); row++ )
249 {
250 wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
251
252 for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
253 {
254 wxListItem info;
255 info.m_itemId = row + position;
256 info.m_col = col;
257 info.m_text = itemList[row].Item( col );
258 info.m_width = FromDIP( DEFAULT_COL_WIDTHS[ col ] );
259 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
260
261 if( col == 0 )
262 {
263 info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
264 info.m_mask |= wxLIST_MASK_DATA;
265
266 m_listBox->InsertItem( info );
267 }
268 else
269 {
270 m_listBox->SetItem( info );
271 }
272 }
273 }
274
275 sortList();
276}
277
278
279void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
280{
281 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
282}
283
284
285void EDA_LIST_DIALOG::onSize( wxSizeEvent& event )
286{
287 if( m_listBox->GetColumnCount() == 1 )
288 {
289 m_listBox->SetColumnWidth( 0, m_listBox->GetClientSize().x );
290 }
291 else if( m_listBox->GetColumnCount() == 2 )
292 {
293 int first = KiROUND( m_listBox->GetClientSize().x * 0.42 );
294 m_listBox->SetColumnWidth( 0, first );
295 m_listBox->SetColumnWidth( 1, m_listBox->GetClientSize().x - first );
296 }
297
298 event.Skip();
299}
300
301
302/*
303 * Sort alphabetically, case insensitive.
304 */
305static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2,
306 wxIntPtr WXUNUSED( aSortData ) )
307{
308 wxString* component1Name = (wxString*) aItem1;
309 wxString* component2Name = (wxString*) aItem2;
310
311 return StrNumCmp( *component1Name, *component2Name, true );
312}
313
314
316{
317 if( m_sortList )
318 m_listBox->SortItems( myCompareFunction, 0 );
319}
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:236
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)
std::map< wxCheckBox *, bool * > m_extraCheckboxMap
virtual void onSize(wxSizeEvent &event) override
std::vector< wxArrayString > m_itemsList
void Append(const wxArrayString &aItemStr)
void GetExtraCheckboxValues()
Fills in the value pointers from the checkboxes after the dialog has run.
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, const std::vector< std::pair< wxString, bool * > > &aExtraCheckboxes={})
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:429