KiCad PCB EDA Suite
displlst.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-2013 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 
30 #include <fctsys.h>
31 #include <macros.h>
32 #include <draw_frame.h>
33 #include <kicad_string.h>
34 #include <dialog_helpers.h>
35 
36 EDA_LIST_DIALOG::EDA_LIST_DIALOG( EDA_DRAW_FRAME* aParent, const wxString& aTitle,
37  const wxArrayString& aItemHeaders,
38  const std::vector<wxArrayString>& aItemList,
39  const wxString& aSelection,
40  void( *aCallBackFunction )( wxString&, void* ),
41  void* aCallBackFunctionData,
42  bool aSortList ) :
43  EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle )
44 {
45  m_sortList = aSortList;
46  m_cb_func = aCallBackFunction;
47  m_cb_data = aCallBackFunctionData;
48  m_itemsListCp = &aItemList;
49 
50  initDialog( aItemHeaders, aSelection );
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_filterBox->SetFocus();
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,
68  const wxString& aSelection)
69 {
70 
71  for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
72  {
73  wxListItem column;
74 
75  column.SetId( i );
76  column.SetText( aItemHeaders.Item( i ) );
77 
78  m_listBox->InsertColumn( i, column );
79  }
80 
82 
83  if( m_cb_func == NULL )
84  {
85  m_messages->Show( false );
86  m_staticTextMsg->Show( false );
87  }
88 
89  for( unsigned col = 0; col < aItemHeaders.Count(); ++col )
90  {
91  m_listBox->SetColumnWidth( col, wxLIST_AUTOSIZE );
92  int columnwidth = m_listBox->GetColumnWidth( col );
93  m_listBox->SetColumnWidth( col, wxLIST_AUTOSIZE_USEHEADER );
94  int headerwidth = m_listBox->GetColumnWidth( col );
95  m_listBox->SetColumnWidth( col, std::max( columnwidth, headerwidth ) );
96  }
97 
98  if( !!aSelection )
99  {
100  for( unsigned row = 0; row < m_itemsListCp->size(); ++row )
101  {
102  if( (*m_itemsListCp)[row][0] == aSelection )
103  {
104  m_listBox->SetItemState( row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
105  m_listBox->EnsureVisible( row );
106  break;
107  }
108  }
109  }
110 }
111 
112 
113 void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
114 {
115  wxString filter;
116  wxString itemName;
117 
118  filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
119 
120  m_listBox->DeleteAllItems();
121 
122  for( unsigned i = 0; i < m_itemsListCp->size(); i++ )
123  {
124  itemName = (*m_itemsListCp)[i].Item( 0 );
125 
126  if( itemName.MakeLower().Matches( filter ) )
127  {
128  Append( (*m_itemsListCp)[i] );
129  }
130  }
131 
132  if( m_sortList )
133  sortList();
134 }
135 
136 
137 wxString EDA_LIST_DIALOG::GetTextSelection( int aColumn )
138 {
139  wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
140  wxT( "Invalid list control column." ) );
141 
142  long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
143 
144  if( item >= 0 ) // if something is selected.
145  {
146  wxListItem info;
147 
148  info.m_mask = wxLIST_MASK_TEXT;
149  info.m_itemId = item;
150  info.m_col = aColumn;
151 
152  if( m_listBox->GetItem( info ) )
153  return info.m_text;
154  }
155 
156  return wxEmptyString;
157 }
158 
159 
160 void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
161 {
162  long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
163 
164  m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
165 
166  // Adding the next columns content
167  for( unsigned i = 1; i < itemList.size(); i++ )
168  {
169  m_listBox->SetItem( itemIndex, i, itemList[i] );
170  }
171 }
172 
173 
174 void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
175 {
176  for( unsigned row = 0; row < itemList.size(); row++ )
177  {
178  wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
179 
180  long itemIndex = 0;
181  for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
182  {
183 
184  if( col == 0 )
185  {
186  itemIndex = m_listBox->InsertItem( row+position, itemList[row].Item( col ) );
187  m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[row].Item( col ) ) );
188  }
189  else
190  {
191  m_listBox->SetItem( itemIndex, col, itemList[row].Item( col ) );
192  }
193  }
194  }
195 
196  if( m_sortList )
197  sortList();
198 }
199 
200 
201 void EDA_LIST_DIALOG::onCancelClick( wxCommandEvent& event )
202 {
203  EndModal( wxID_CANCEL );
204 }
205 
206 
207 void EDA_LIST_DIALOG::onListItemSelected( wxListEvent& event )
208 {
209  if( m_cb_func )
210  {
211  m_messages->Clear();
212  wxString text = GetTextSelection();
213  m_cb_func( text, m_cb_data );
214  m_messages->WriteText( text );
215  }
216 }
217 
218 
219 void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
220 {
221  EndModal( wxID_OK );
222 }
223 
224 
225 void EDA_LIST_DIALOG::onOkClick( wxCommandEvent& event )
226 {
227  EndModal( wxID_OK );
228 }
229 
230 
231 void EDA_LIST_DIALOG::onClose( wxCloseEvent& event )
232 {
233  EndModal( wxID_CANCEL );
234 }
235 
236 
237 /* Sort alphabetically, case insensitive.
238  */
239 static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2,
240  wxIntPtr WXUNUSED( aSortData ) )
241 {
242  wxString* component1Name = (wxString*) aItem1;
243  wxString* component2Name = (wxString*) aItem2;
244 
245  return StrNumCmp( *component1Name, *component2Name, INT_MAX, true );
246 }
247 
248 
250 {
251  m_listBox->SortItems( myCompareFunction, 0 );
252 }
void onCancelClick(wxCommandEvent &event) override
Definition: displlst.cpp:201
void initDialog(const wxArrayString &aItemHeaders, const wxString &aSelection)
Definition: displlst.cpp:67
void onOkClick(wxCommandEvent &event) override
Definition: displlst.cpp:225
wxString GetTextSelection(int aColumn=0)
Function GetTextSelection return the selected text from aColumn in the wxListCtrl in the dialog...
Definition: displlst.cpp:137
void textChangeInFilterBox(wxCommandEvent &event) override
Definition: displlst.cpp:113
std::string m_hash_key
Definition: dialog_shim.h:128
EDA_LIST_DIALOG(EDA_DRAW_FRAME *aParent, const wxString &aTitle, const wxArrayString &aItemHeaders, const std::vector< wxArrayString > &aItemList, const wxString &aRefText, void(*aCallBackFunction)(wxString &text, void *data)=NULL, void *aCallBackFunctionData=NULL, bool aSortList=false)
Constructor:
Definition: displlst.cpp:36
void Append(const wxArrayString &aItemStr)
Definition: displlst.cpp:160
const std::vector< wxArrayString > * m_itemsListCp
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:53
static int wxCALLBACK myCompareFunction(wxIntPtr aItem1, wxIntPtr aItem2, wxIntPtr WXUNUSED(aSortData))
Definition: displlst.cpp:239
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
Helper dialog and control classes.
void onClose(wxCloseEvent &event) override
Definition: displlst.cpp:231
Class EDA_LIST_DIALOG_BASE.
void onListItemSelected(wxListEvent &event) override
Definition: displlst.cpp:207
#define max(a, b)
Definition: auxiliary.h:86
int StrNumCmp(const wxString &aString1, const wxString &aString2, int aLength, bool aIgnoreCase)
Function StrLenNumCmp is a routine compatible with qsort() to sort by alphabetical order...
Definition: string.cpp:238
void InsertItems(const std::vector< wxArrayString > &aItemList, int aPosition=0)
Definition: displlst.cpp:174
void(* m_cb_func)(wxString &text, void *data)
void onListItemActivated(wxListEvent &event) override
Definition: displlst.cpp:219