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 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 static int DEFAULT_SINGLE_COL_WIDTH = 260;
36 static int DEFAULT_COL_WIDTHS[] = { 200, 600 };
37 
38 
39 
40 EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle,
41  const wxArrayString& aItemHeaders,
42  const std::vector<wxArrayString>& aItemList,
43  const wxString& aPreselectText ) :
44  EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle )
45 {
46  m_itemsList = &aItemList;
47 
48  m_filterBox->SetHint( _( "Filter" ) );
49 
50  initDialog( aItemHeaders, aPreselectText );
51 
52  // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
53  // because so many dialogs share this same class, with different numbers of
54  // columns, different column names, and column widths.
55  m_hash_key = TO_UTF8( aTitle );
56 
57  m_sdbSizerOK->SetDefault();
58 
59  // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
60  // and works fine on all systems
61  GetSizer()->Fit( this );
62 
63  Centre();
64 }
65 
66 
67 void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders, const wxString& aSelection )
68 {
69  if( aItemHeaders.Count() == 1 )
70  {
71  m_listBox->InsertColumn( 0, aItemHeaders.Item( 0 ), wxLIST_FORMAT_LEFT,
73  }
74  else
75  {
76  for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
77  {
78  m_listBox->InsertColumn( i, aItemHeaders.Item( i ), wxLIST_FORMAT_LEFT,
79  DEFAULT_COL_WIDTHS[ i ] );
80  }
81  }
82 
83  InsertItems( *m_itemsList, 0 );
84 
85  if( !aSelection.IsEmpty() )
86  {
87  long sel = m_listBox->FindItem( -1, aSelection );
88 
89  if( sel != wxNOT_FOUND )
90  {
91  m_listBox->SetItemState( sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
92 
93  // Set to a small size so EnsureVisible() won't be foiled by later additions.
94  // ListBox will expand to fit later.
95  m_listBox->SetSize( m_listBox->GetSize().GetX(), 100 );
96  m_listBox->EnsureVisible( sel );
97  }
98  }
99 }
100 
101 
102 void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
103 {
104  m_listLabel->SetLabel( aLabel );
105  m_listBox->SetSingleStyle( wxLC_NO_HEADER, true );
106 }
107 
108 
109 void EDA_LIST_DIALOG::SetOKLabel( const wxString& aLabel )
110 {
111  m_sdbSizerOK->SetLabel( aLabel );
112 }
113 
114 
116 {
117  m_filterBox->Hide();
118 }
119 
120 
121 void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
122 {
123  wxString filter;
124  wxString itemName;
125 
126  filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
127 
128  m_listBox->DeleteAllItems();
129 
130  for( const wxArrayString& row : *m_itemsList )
131  {
132  itemName = row.Item( 0 );
133 
134  if( itemName.MakeLower().Matches( filter ) )
135  Append( row );
136  }
137 
138  sortList();
139 }
140 
141 
143 {
144  return m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
145 }
146 
147 
148 wxString EDA_LIST_DIALOG::GetTextSelection( int aColumn )
149 {
150  wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
151  wxT( "Invalid list control column." ) );
152 
153  long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
154 
155  if( item >= 0 ) // if something is selected.
156  {
157  wxListItem info;
158 
159  info.m_mask = wxLIST_MASK_TEXT;
160  info.m_itemId = item;
161  info.m_col = aColumn;
162 
163  if( m_listBox->GetItem( info ) )
164  return info.m_text;
165  }
166 
167  return wxEmptyString;
168 }
169 
170 
171 void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
172 {
173  long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
174 
175  m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
176 
177  // Adding the next columns content
178  for( unsigned i = 1; i < itemList.size(); i++ )
179  m_listBox->SetItem( itemIndex, i, itemList[i] );
180 }
181 
182 
183 void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
184 {
185  for( unsigned row = 0; row < itemList.size(); row++ )
186  {
187  wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
188 
189  for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
190  {
191  wxListItem info;
192  info.m_itemId = row + position;
193  info.m_col = col;
194  info.m_text = itemList[row].Item( col );
195  info.m_width = DEFAULT_COL_WIDTHS[ col ];
196  info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
197 
198  if( col == 0 )
199  {
200  info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
201  info.m_mask |= wxLIST_MASK_DATA;
202 
203  m_listBox->InsertItem( info );
204  }
205  else
206  {
207  m_listBox->SetItem( info );
208  }
209  }
210  }
211 
212  sortList();
213 }
214 
215 
216 void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
217 {
218  wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
219 }
220 
221 
222 /*
223  * Sort alphabetically, case insensitive.
224  */
225 static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2,
226  wxIntPtr WXUNUSED( aSortData ) )
227 {
228  wxString* component1Name = (wxString*) aItem1;
229  wxString* component2Name = (wxString*) aItem2;
230 
231  return StrNumCmp( *component1Name, *component2Name, true );
232 }
233 
234 
236 {
237  m_listBox->SortItems( myCompareFunction, 0 );
238 }
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:518
void textChangeInFilterBox(wxCommandEvent &event) override
EDA_LIST_DIALOG(wxWindow *aParent, const wxString &aTitle, const wxArrayString &aItemHeaders, const std::vector< wxArrayString > &aItemList, const wxString &aPreselectText=wxEmptyString)
std::string m_hash_key
Definition: dialog_shim.h:199
void SetOKLabel(const wxString &aLabel)
void Append(const wxArrayString &aItemStr)
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.
#define _(s)
Class EDA_LIST_DIALOG_BASE.
const std::vector< wxArrayString > * m_itemsList
static int DEFAULT_SINGLE_COL_WIDTH
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)