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#include <algorithm>
30#include <iterator>
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// Note, these are "standard DPI" widths
36static int DEFAULT_SINGLE_COL_WIDTH = 260;
37static int DEFAULT_COL_WIDTHS[] = { 200, 300 };
38
39
40// Columns past the defaults take the last one's width
41static int defaultColWidth( unsigned aCol )
42{
43 return DEFAULT_COL_WIDTHS[std::min<size_t>( aCol, std::size( DEFAULT_COL_WIDTHS ) - 1 )];
44}
45
46
47EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle, const wxArrayString& aItemHeaders,
48 const std::vector<wxArrayString>& aItemList, const wxString& aPreselectText,
49 bool aSortList ) :
50 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
51 m_sortList( aSortList )
52{
53 // correct for wxfb not supporting FromDIP
54 m_listBox->SetMinSize( FromDIP( m_listBox->GetMinSize() ) );
55 m_filterBox->SetHint( _( "Filter" ) );
57
58 initDialog( aItemHeaders, aItemList, aPreselectText );
59
60 if( aItemList.size() > 16 )
61 m_listBox->SetMinSize( wxSize( m_listBox->GetMinWidth(), KiROUND( m_listBox->GetMinHeight() * 1.66 ) ) );
62
63 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
64 // because so many dialogs share this same class, with different numbers of
65 // columns, different column names, and column widths.
66 m_hash_key = TO_UTF8( aTitle );
67
69
70 Layout();
71 GetSizer()->Fit( this );
72
73 Centre();
74}
75
76
77EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aSortList ) :
78 EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
79 m_sortList( aSortList )
80{
81 m_filterBox->SetHint( _( "Filter" ) );
83}
84
85
86void EDA_LIST_DIALOG::AddExtraCheckbox( const wxString& aLabel, bool* aValuePtr )
87{
88 wxCHECK2_MSG( aValuePtr, return, wxT( "Null pointer for checkbox value." ) );
89
90 int flags = wxBOTTOM;
91
92 if( m_ExtrasSizer->GetItemCount() > 0 )
93 flags |= wxTOP;
94
95 wxCheckBox* cb = new wxCheckBox( this, wxID_ANY, aLabel );
96 cb->SetValue( *aValuePtr );
97 m_ExtrasSizer->Add( cb, 0, flags, 5 );
98 m_extraCheckboxMap[cb] = aValuePtr;
99}
100
101
102bool EDA_LIST_DIALOG::Show( bool show )
103{
104 bool retVal = DIALOG_SHIM::Show( show );
105
106 if( show )
107 {
108 wxSizeEvent dummy;
109 onSize( dummy );
110 }
111
112 return retVal;
113}
114
115
116void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders, const std::vector<wxArrayString>& aItemList,
117 const wxString& aPreselectText )
118{
119 if( aItemHeaders.Count() == 1 )
120 {
121 m_listBox->InsertColumn( 0, aItemHeaders.Item( 0 ), wxLIST_FORMAT_LEFT,
122 FromDIP( DEFAULT_SINGLE_COL_WIDTH ) );
123
124 m_listBox->SetMinClientSize( FromDIP( wxSize( DEFAULT_SINGLE_COL_WIDTH, 200 ) ) );
125 SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0], 220 ) ) );
126 }
127 else if( aItemHeaders.Count() >= 2 )
128 {
129 for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
130 {
131 m_listBox->InsertColumn( i, aItemHeaders.Item( i ), wxLIST_FORMAT_LEFT,
132 FromDIP( defaultColWidth( i ) ) );
133 }
134
135 int minWidth = DEFAULT_COL_WIDTHS[0] * std::max( 3, static_cast<int>( aItemHeaders.Count() ) );
136
137 m_listBox->SetMinClientSize( FromDIP( wxSize( minWidth, 200 ) ) );
138 SetMinClientSize( FromDIP( wxSize( DEFAULT_COL_WIDTHS[0] * 2, 220 ) ) );
139 }
140
141 m_itemsList = aItemList;
143
144 if( !aPreselectText.IsEmpty() )
145 {
146 // If a preselect is specified, disable loading of previously-saved state
147 OptOut( m_listBox );
148
149 long sel = m_listBox->FindItem( -1, aPreselectText );
150
151 if( sel != wxNOT_FOUND )
152 {
153 m_listBox->SetItemState( sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
154
155 // Set to a small size so EnsureVisible() won't be foiled by later additions.
156 // ListBox will expand to fit later.
157 m_listBox->SetSize( m_listBox->GetSize().GetX(), FromDIP( 100 ) );
158 m_listBox->EnsureVisible( sel );
159 }
160 }
161}
162
163
164void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
165{
166 m_listLabel->SetLabel( aLabel );
167 m_listBox->SetSingleStyle( wxLC_NO_HEADER, true );
168}
169
170
171void EDA_LIST_DIALOG::SetOKLabel( const wxString& aLabel )
172{
173 m_sdbSizerOK->SetLabel( aLabel );
174}
175
176
178{
179 m_filterBox->Hide();
180}
181
182
183void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
184{
185 wxString filter;
186 wxString itemName;
187
188 filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
189
190 m_listBox->DeleteAllItems();
191
192 for( const wxArrayString& row : m_itemsList )
193 {
194 itemName = row.Item( 0 );
195
196 if( itemName.MakeLower().Matches( filter ) )
197 Append( row );
198 }
199
200 sortList();
201}
202
203
205{
206 return m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
207}
208
209
211{
212 wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
213 wxT( "Invalid list control column." ) );
214
215 long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
216 wxString text;
217
218 if( item >= 0 ) // if something is selected.
219 {
220 wxListItem info;
221
222 info.m_mask = wxLIST_MASK_TEXT;
223 info.m_itemId = item;
224 info.m_col = aColumn;
225
226 if( m_listBox->GetItem( info ) )
227 text = info.m_text;
228
231 }
232
233 return text;
234}
235
236
238{
239 for( const auto& [checkbox, valuePtr] : m_extraCheckboxMap )
240 *valuePtr = checkbox->GetValue();
241}
242
243
244void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
245{
246 long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
247
248 m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
249
250 // Adding the next columns content
251 for( unsigned i = 1; i < itemList.size(); i++ )
252 m_listBox->SetItem( itemIndex, i, itemList[i] );
253}
254
255
256void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
257{
258 for( unsigned row = 0; row < itemList.size(); row++ )
259 {
260 wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
261
262 for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
263 {
264 wxListItem info;
265 info.m_itemId = row + position;
266 info.m_col = col;
267 info.m_text = itemList[row].Item( col );
268 info.m_width = FromDIP( defaultColWidth( col ) );
269 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
270
271 if( col == 0 )
272 {
273 info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
274 info.m_mask |= wxLIST_MASK_DATA;
275
276 m_listBox->InsertItem( info );
277 }
278 else
279 {
280 m_listBox->SetItem( info );
281 }
282 }
283 }
284
285 sortList();
286}
287
288
289void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
290{
291 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
292}
293
294
295void EDA_LIST_DIALOG::onSize( wxSizeEvent& event )
296{
297 if( m_listBox->GetColumnCount() == 1 )
298 {
299 m_listBox->SetColumnWidth( 0, m_listBox->GetClientSize().x );
300 }
301 else if( m_listBox->GetColumnCount() == 2 )
302 {
303 int first = KiROUND( m_listBox->GetClientSize().x * 0.42 );
304 m_listBox->SetColumnWidth( 0, first );
305 m_listBox->SetColumnWidth( 1, m_listBox->GetClientSize().x - first );
306 }
307 else if( m_listBox->GetColumnCount() > 2 )
308 {
309 int width = m_listBox->GetClientSize().x / m_listBox->GetColumnCount();
310
311 for( int col = 0; col < m_listBox->GetColumnCount(); col++ )
312 m_listBox->SetColumnWidth( col, width );
313 }
314
315 event.Skip();
316}
317
318
319/*
320 * Sort alphabetically, case insensitive.
321 */
322static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2, wxIntPtr WXUNUSED( aSortData ) )
323{
324 wxString* component1Name = (wxString*) aItem1;
325 wxString* component2Name = (wxString*) aItem2;
326
327 return StrNumCmp( *component1Name, *component2Name, CMP_T::IGNORE_CASE );
328}
329
330
332{
333 if( m_sortList )
334 m_listBox->SortItems( myCompareFunction, 0 );
335}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
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 defaultColWidth(unsigned aCol)
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, CMP_T aCaseSensitivity)
Compare two strings with alphanumerical content.
@ IGNORE_CASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.