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 
37 // wxWidgets spends *far* too long calcuating column widths (most of it, believe it or
38 // not, in repeatedly creating/destorying a wxDC to do the measurement in).
39 // Use default column widths instead. (Note that these will be scaled down proportionally
40 // to fit the available space when the dialog is instantiated.)
41 static int DEFAULT_COL_WIDTHS[] = { 400, 200 };
42 
43 
44 
45 EDA_LIST_DIALOG::EDA_LIST_DIALOG( EDA_DRAW_FRAME* aParent, const wxString& aTitle,
46  const wxArrayString& aItemHeaders,
47  const std::vector<wxArrayString>& aItemList,
48  const wxString& aSelection,
49  void( *aCallBackFunction )( wxString&, void* ),
50  void* aCallBackFunctionData,
51  bool aSortList ) :
52  EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle )
53 {
54  m_sortList = aSortList;
55  m_cb_func = aCallBackFunction;
56  m_cb_data = aCallBackFunctionData;
57  m_itemsListCp = &aItemList;
58 
59  initDialog( aItemHeaders, aSelection );
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  m_filterBox->SetFocus();
67 
68  m_sdbSizerOK->SetDefault();
69 
70  // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
71  // and works fine on all systems
72  GetSizer()->Fit( this );
73 
74  Centre();
75 }
76 
77 
78 void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders, const wxString& aSelection)
79 {
80  for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
81  m_listBox->InsertColumn( i, aItemHeaders.Item( i ),
82  wxLIST_FORMAT_LEFT, DEFAULT_COL_WIDTHS[ i ] );
83 
85 
86  if( m_cb_func == NULL )
87  {
88  m_messages->Show( false );
89  m_staticTextMsg->Show( false );
90  }
91 
92  if( !aSelection.IsEmpty() )
93  {
94  for( unsigned row = 0; row < m_itemsListCp->size(); ++row )
95  {
96  if( (*m_itemsListCp)[row][0] == aSelection )
97  {
98  m_listBox->SetItemState( row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
99 
100  // Set to a small size so EnsureVisible() won't be foiled by later additions.
101  // ListBox will expand to fit later.
102  m_listBox->SetSize( m_listBox->GetSize().GetX(), 100 );
103  m_listBox->EnsureVisible( row );
104 
105  break;
106  }
107  }
108  }
109 }
110 
111 
112 void EDA_LIST_DIALOG::SetFilterLabel( const wxString& aLabel )
113 {
114  m_filterLabel->SetLabel( aLabel );
115 }
116 
117 
118 void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
119 {
120  m_listLabel->SetLabel( aLabel );
121 }
122 
123 
124 void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
125 {
126  wxString filter;
127  wxString itemName;
128 
129  filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
130 
131  m_listBox->DeleteAllItems();
132 
133  for( unsigned i = 0; i < m_itemsListCp->size(); i++ )
134  {
135  itemName = (*m_itemsListCp)[i].Item( 0 );
136 
137  if( itemName.MakeLower().Matches( filter ) )
138  {
139  Append( (*m_itemsListCp)[i] );
140  }
141  }
142 
143  if( m_sortList )
144  sortList();
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  {
180  m_listBox->SetItem( itemIndex, i, itemList[i] );
181  }
182 }
183 
184 
185 void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
186 {
187  for( unsigned row = 0; row < itemList.size(); row++ )
188  {
189  wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
190 
191  for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
192  {
193  wxListItem info;
194  info.m_itemId = row + position;
195  info.m_col = col;
196  info.m_text = itemList[row].Item( col );
197  info.m_width = DEFAULT_COL_WIDTHS[ col ];
198  info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
199 
200  if( col == 0 )
201  {
202  info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
203  info.m_mask |= wxLIST_MASK_DATA;
204 
205  m_listBox->InsertItem( info );
206  }
207  else
208  {
209  m_listBox->SetItem( info );
210  }
211  }
212  }
213 
214  if( m_sortList )
215  sortList();
216 }
217 
218 
219 void EDA_LIST_DIALOG::onCancelClick( wxCommandEvent& event )
220 {
221  EndModal( wxID_CANCEL );
222 }
223 
224 
225 void EDA_LIST_DIALOG::onListItemSelected( wxListEvent& event )
226 {
227  if( m_cb_func )
228  {
229  m_messages->Clear();
230  wxString text = GetTextSelection();
231  m_cb_func( text, m_cb_data );
232  m_messages->WriteText( text );
233  }
234 }
235 
236 
237 void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
238 {
239  EndModal( wxID_OK );
240 }
241 
242 
243 void EDA_LIST_DIALOG::onOkClick( wxCommandEvent& event )
244 {
245  EndModal( wxID_OK );
246 }
247 
248 
249 void EDA_LIST_DIALOG::onClose( wxCloseEvent& event )
250 {
251  EndModal( wxID_CANCEL );
252 }
253 
254 
255 /* Sort alphabetically, case insensitive.
256  */
257 static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2,
258  wxIntPtr WXUNUSED( aSortData ) )
259 {
260  wxString* component1Name = (wxString*) aItem1;
261  wxString* component2Name = (wxString*) aItem2;
262 
263  return StrNumCmp( *component1Name, *component2Name, INT_MAX, true );
264 }
265 
266 
268 {
269  m_listBox->SortItems( myCompareFunction, 0 );
270 }
void onCancelClick(wxCommandEvent &event) override
Definition: displlst.cpp:219
void initDialog(const wxArrayString &aItemHeaders, const wxString &aSelection)
Definition: displlst.cpp:78
void onOkClick(wxCommandEvent &event) override
Definition: displlst.cpp:243
wxString GetTextSelection(int aColumn=0)
Function GetTextSelection return the selected text from aColumn in the wxListCtrl in the dialog...
Definition: displlst.cpp:148
static int DEFAULT_COL_WIDTHS[]
Definition: displlst.cpp:41
void textChangeInFilterBox(wxCommandEvent &event) override
Definition: displlst.cpp:124
std::string m_hash_key
Definition: dialog_shim.h:151
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:45
void Append(const wxArrayString &aItemStr)
Definition: displlst.cpp:171
const std::vector< wxArrayString > * m_itemsListCp
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:63
static int wxCALLBACK myCompareFunction(wxIntPtr aItem1, wxIntPtr aItem2, wxIntPtr WXUNUSED(aSortData))
Definition: displlst.cpp:257
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
void SetFilterLabel(const wxString &aLabel)
Definition: displlst.cpp:112
Helper dialog and control classes.
void onClose(wxCloseEvent &event) override
Definition: displlst.cpp:249
Class EDA_LIST_DIALOG_BASE.
void onListItemSelected(wxListEvent &event) override
Definition: displlst.cpp:225
size_t i
Definition: json11.cpp:597
int StrNumCmp(const wxString &aString1, const wxString &aString2, int aLength, bool aIgnoreCase)
Function StrNumCmp 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:185
void(* m_cb_func)(wxString &text, void *data)
void onListItemActivated(wxListEvent &event) override
Definition: displlst.cpp:237
void SetListLabel(const wxString &aLabel)
Definition: displlst.cpp:118