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