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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <eda_list_dialog.h>
23#include <eda_draw_frame.h>
24#include <string_utils.h>
25#include <macros.h>
26#include <wx/checkbox.h>
28
29// wxWidgets spends *far* too long calculating column widths (most of it, believe it or
30// not, in repeatedly creating/destroying a wxDC to do the measurement in).
31// Use default column widths instead.
32// Note, these are "standard DPI" widths
33static int DEFAULT_SINGLE_COL_WIDTH = 260;
34static int DEFAULT_COL_WIDTHS[] = { 200, 300 };
35
36
37EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle, const wxArrayString& aItemHeaders,
38 const std::vector<wxArrayString>& aItemList, const wxString& aPreselectText,
39 bool aSortList ) :
40 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
41 m_sortList( aSortList )
42{
43 // correct for wxfb not supporting FromDIP
44 m_listBox->SetMinSize( FromDIP( m_listBox->GetMinSize() ) );
45 m_filterBox->SetHint( _( "Filter" ) );
47
48 initDialog( aItemHeaders, aItemList, aPreselectText );
49
50 if( aItemList.size() > 16 )
51 m_listBox->SetMinSize( wxSize( m_listBox->GetMinWidth(), KiROUND( m_listBox->GetMinHeight() * 1.66 ) ) );
52
53 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
54 // because so many dialogs share this same class, with different numbers of
55 // columns, different column names, and column widths.
56 m_hash_key = TO_UTF8( aTitle );
57
59
60 Layout();
61 GetSizer()->Fit( this );
62
63 Centre();
64}
65
66
67EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aSortList ) :
68 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
69 m_sortList( aSortList )
70{
71 m_filterBox->SetHint( _( "Filter" ) );
73}
74
75
76void EDA_LIST_DIALOG::AddExtraCheckbox( const wxString& aLabel, bool* aValuePtr )
77{
78 wxCHECK2_MSG( aValuePtr, return, wxT( "Null pointer for checkbox value." ) );
79
80 int flags = wxBOTTOM;
81
82 if( m_ExtrasSizer->GetItemCount() > 0 )
83 flags |= wxTOP;
84
85 wxCheckBox* cb = new wxCheckBox( this, wxID_ANY, aLabel );
86 cb->SetValue( *aValuePtr );
87 m_ExtrasSizer->Add( cb, 0, flags, 5 );
88 m_extraCheckboxMap[cb] = aValuePtr;
89}
90
91
92bool EDA_LIST_DIALOG::Show( bool show )
93{
94 bool retVal = DIALOG_SHIM::Show( show );
95
96 if( show )
97 {
98 wxSizeEvent dummy;
99 onSize( dummy );
100 }
101
102 return retVal;
103}
104
105
106void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders, const std::vector<wxArrayString>& aItemList,
107 const wxString& aPreselectText )
108{
109 if( aItemHeaders.Count() == 1 )
110 {
111 m_listBox->InsertColumn( 0, aItemHeaders.Item( 0 ), wxLIST_FORMAT_LEFT,
112 FromDIP( DEFAULT_SINGLE_COL_WIDTH ) );
113
114 m_listBox->SetMinClientSize( FromDIP( wxSize( DEFAULT_SINGLE_COL_WIDTH, 200 ) ) );
115 SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0], 220 ) ) );
116 }
117 else if( aItemHeaders.Count() == 2 )
118 {
119 for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
120 {
121 m_listBox->InsertColumn( i, aItemHeaders.Item( i ), wxLIST_FORMAT_LEFT,
122 FromDIP( DEFAULT_COL_WIDTHS[ i ] ) );
123 }
124
125 m_listBox->SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0] * 3, 200 ) ) );
126 SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0] * 2, 220 ) ) );
127 }
128
129 m_itemsList = aItemList;
131
132 if( !aPreselectText.IsEmpty() )
133 {
134 // If a preselect is specified, disable loading of previously-saved state
135 OptOut( m_listBox );
136
137 long sel = m_listBox->FindItem( -1, aPreselectText );
138
139 if( sel != wxNOT_FOUND )
140 {
141 m_listBox->SetItemState( sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
142
143 // Set to a small size so EnsureVisible() won't be foiled by later additions.
144 // ListBox will expand to fit later.
145 m_listBox->SetSize( m_listBox->GetSize().GetX(), FromDIP( 100 ) );
146 m_listBox->EnsureVisible( sel );
147 }
148 }
149}
150
151
152void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
153{
154 m_listLabel->SetLabel( aLabel );
155 m_listBox->SetSingleStyle( wxLC_NO_HEADER, true );
156}
157
158
159void EDA_LIST_DIALOG::SetOKLabel( const wxString& aLabel )
160{
161 m_sdbSizerOK->SetLabel( aLabel );
162}
163
164
166{
167 m_filterBox->Hide();
168}
169
170
171void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
172{
173 wxString filter;
174 wxString itemName;
175
176 filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
177
178 m_listBox->DeleteAllItems();
179
180 for( const wxArrayString& row : m_itemsList )
181 {
182 itemName = row.Item( 0 );
183
184 if( itemName.MakeLower().Matches( filter ) )
185 Append( row );
186 }
187
188 sortList();
189}
190
191
193{
194 return m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
195}
196
197
199{
200 wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
201 wxT( "Invalid list control column." ) );
202
203 long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
204 wxString text;
205
206 if( item >= 0 ) // if something is selected.
207 {
208 wxListItem info;
209
210 info.m_mask = wxLIST_MASK_TEXT;
211 info.m_itemId = item;
212 info.m_col = aColumn;
213
214 if( m_listBox->GetItem( info ) )
215 text = info.m_text;
216
219 }
220
221 return text;
222}
223
224
226{
227 for( const auto& [checkbox, valuePtr] : m_extraCheckboxMap )
228 *valuePtr = checkbox->GetValue();
229}
230
231
232void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
233{
234 long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
235
236 m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
237
238 // Adding the next columns content
239 for( unsigned i = 1; i < itemList.size(); i++ )
240 m_listBox->SetItem( itemIndex, i, itemList[i] );
241}
242
243
244void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
245{
246 for( unsigned row = 0; row < itemList.size(); row++ )
247 {
248 wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
249
250 for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
251 {
252 wxListItem info;
253 info.m_itemId = row + position;
254 info.m_col = col;
255 info.m_text = itemList[row].Item( col );
256 info.m_width = FromDIP( DEFAULT_COL_WIDTHS[ col ] );
257 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
258
259 if( col == 0 )
260 {
261 info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
262 info.m_mask |= wxLIST_MASK_DATA;
263
264 m_listBox->InsertItem( info );
265 }
266 else
267 {
268 m_listBox->SetItem( info );
269 }
270 }
271 }
272
273 sortList();
274}
275
276
277void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
278{
279 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
280}
281
282
283void EDA_LIST_DIALOG::onSize( wxSizeEvent& event )
284{
285 if( m_listBox->GetColumnCount() == 1 )
286 {
287 m_listBox->SetColumnWidth( 0, m_listBox->GetClientSize().x );
288 }
289 else if( m_listBox->GetColumnCount() == 2 )
290 {
291 int first = KiROUND( m_listBox->GetClientSize().x * 0.42 );
292 m_listBox->SetColumnWidth( 0, first );
293 m_listBox->SetColumnWidth( 1, m_listBox->GetClientSize().x - first );
294 }
295
296 event.Skip();
297}
298
299
300/*
301 * Sort alphabetically, case insensitive.
302 */
303static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2, wxIntPtr WXUNUSED( aSortData ) )
304{
305 wxString* component1Name = (wxString*) aItem1;
306 wxString* component2Name = (wxString*) aItem2;
307
308 return StrNumCmp( *component1Name, *component2Name, true );
309}
310
311
313{
314 if( m_sortList )
315 m_listBox->SortItems( myCompareFunction, 0 );
316}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:995
bool Show(bool show) override
void OptOut(wxWindow *aWindow)
Opt out of control state saving.
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
EDA_LIST_DIALOG_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
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)
std::map< wxCheckBox *, bool * > m_extraCheckboxMap
void onSize(wxSizeEvent &event) override
std::vector< wxArrayString > m_itemsList
void Append(const wxArrayString &aItemStr)
void AddExtraCheckbox(const wxString &aLabel, bool *aValuePtr)
Add a checkbox value to the dialog.
void GetExtraCheckboxValues()
Fills in the value pointers from the checkboxes after the dialog has run.
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.