KiCad PCB EDA Suite
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 <dick@softplc.com>
6  * Copyright (C) 1992-2021 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 <dialog_helpers.h>
27 #include <eda_draw_frame.h>
28 #include <kicad_string.h>
29 #include <macros.h>
30 
31 
32 // wxWidgets spends *far* too long calcuating 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 static int DEFAULT_COL_WIDTHS[] = { 200, 600 };
36 
37 
38 
39 EDA_LIST_DIALOG::EDA_LIST_DIALOG( EDA_DRAW_FRAME* aParent, const wxString& aTitle,
40  const wxArrayString& aItemHeaders,
41  const std::vector<wxArrayString>& aItemList,
42  const wxString& aSelection ) :
43  EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle )
44 {
45  m_itemsList = &aItemList;
46 
47  m_filterBox->SetHint( _( "Filter" ) );
48 
49  initDialog( aItemHeaders, aSelection );
50 
51  // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
52  // because so many dialogs share this same class, with different numbers of
53  // columns, different column names, and column widths.
54  m_hash_key = TO_UTF8( aTitle );
55 
56  m_sdbSizerOK->SetDefault();
57 
58  // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
59  // and works fine on all systems
60  GetSizer()->Fit( this );
61 
62  Centre();
63 }
64 
65 
66 void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders, const wxString& aSelection )
67 {
68  for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
69  {
70  m_listBox->InsertColumn( i, aItemHeaders.Item( i ),
71  wxLIST_FORMAT_LEFT, DEFAULT_COL_WIDTHS[ i ] );
72  }
73 
74  InsertItems( *m_itemsList, 0 );
75 
76  if( !aSelection.IsEmpty() )
77  {
78  long sel = m_listBox->FindItem( -1, aSelection );
79 
80  if( sel != wxNOT_FOUND )
81  {
82  m_listBox->SetItemState( sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
83 
84  // Set to a small size so EnsureVisible() won't be foiled by later additions.
85  // ListBox will expand to fit later.
86  m_listBox->SetSize( m_listBox->GetSize().GetX(), 100 );
87  m_listBox->EnsureVisible( sel );
88  }
89  }
90 }
91 
92 
93 void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
94 {
95  m_listLabel->SetLabel( aLabel );
96  m_listBox->SetSingleStyle( wxLC_NO_HEADER, true );
97 }
98 
99 
100 void EDA_LIST_DIALOG::SetOKLabel( const wxString& aLabel )
101 {
102  m_sdbSizerOK->SetLabel( aLabel );
103 }
104 
105 
106 void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
107 {
108  wxString filter;
109  wxString itemName;
110 
111  filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
112 
113  m_listBox->DeleteAllItems();
114 
115  for( const wxArrayString& row : *m_itemsList )
116  {
117  itemName = row.Item( 0 );
118 
119  if( itemName.MakeLower().Matches( filter ) )
120  Append( row );
121  }
122 
123  sortList();
124 }
125 
126 
127 wxString EDA_LIST_DIALOG::GetTextSelection( int aColumn )
128 {
129  wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
130  wxT( "Invalid list control column." ) );
131 
132  long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
133 
134  if( item >= 0 ) // if something is selected.
135  {
136  wxListItem info;
137 
138  info.m_mask = wxLIST_MASK_TEXT;
139  info.m_itemId = item;
140  info.m_col = aColumn;
141 
142  if( m_listBox->GetItem( info ) )
143  return info.m_text;
144  }
145 
146  return wxEmptyString;
147 }
148 
149 
150 void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
151 {
152  long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
153 
154  m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
155 
156  // Adding the next columns content
157  for( unsigned i = 1; i < itemList.size(); i++ )
158  m_listBox->SetItem( itemIndex, i, itemList[i] );
159 }
160 
161 
162 void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
163 {
164  for( unsigned row = 0; row < itemList.size(); row++ )
165  {
166  wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
167 
168  for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
169  {
170  wxListItem info;
171  info.m_itemId = row + position;
172  info.m_col = col;
173  info.m_text = itemList[row].Item( col );
174  info.m_width = DEFAULT_COL_WIDTHS[ col ];
175  info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
176 
177  if( col == 0 )
178  {
179  info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
180  info.m_mask |= wxLIST_MASK_DATA;
181 
182  m_listBox->InsertItem( info );
183  }
184  else
185  {
186  m_listBox->SetItem( info );
187  }
188  }
189  }
190 
191  sortList();
192 }
193 
194 
195 void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
196 {
197  EndModal( wxID_OK );
198 }
199 
200 
201 /* Sort alphabetically, case insensitive.
202  */
203 static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2,
204  wxIntPtr WXUNUSED( aSortData ) )
205 {
206  wxString* component1Name = (wxString*) aItem1;
207  wxString* component2Name = (wxString*) aItem2;
208 
209  return StrNumCmp( *component1Name, *component2Name, true );
210 }
211 
212 
214 {
215  m_listBox->SortItems( myCompareFunction, 0 );
216 }
void initDialog(const wxArrayString &aItemHeaders, const wxString &aSelection)
wxString GetTextSelection(int aColumn=0)
Return the selected text from aColumn in the wxListCtrl in the dialog.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Definition: string.cpp:440
void textChangeInFilterBox(wxCommandEvent &event) override
std::string m_hash_key
Definition: dialog_shim.h:200
void SetOKLabel(const wxString &aLabel)
EDA_LIST_DIALOG(EDA_DRAW_FRAME *aParent, const wxString &aTitle, const wxArrayString &aItemHeaders, const std::vector< wxArrayString > &aItemList, const wxString &aRefText)
void Append(const wxArrayString &aItemStr)
The base class for create windows for drawing purpose.
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
static int wxCALLBACK myCompareFunction(wxIntPtr aItem1, wxIntPtr aItem2, wxIntPtr WXUNUSED(aSortData))
Helper dialog and control classes.
Class EDA_LIST_DIALOG_BASE.
const std::vector< wxArrayString > * m_itemsList
#define _(s)
Definition: 3d_actions.cpp:33
static int DEFAULT_COL_WIDTHS[]
void InsertItems(const std::vector< wxArrayString > &aItemList, int aPosition=0)
void onListItemActivated(wxListEvent &event) override
void SetListLabel(const wxString &aLabel)