KiCad PCB EDA Suite
net_selector.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) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <string_utils.h>
25#include <kiplatform/ui.h>
26
27#include <widgets/ui_common.h>
29
30#include <board.h>
31#include <netinfo.h>
32#include <wx/arrstr.h>
33#include <wx/display.h>
34#include <wx/valtext.h>
35#include <wx/listbox.h>
36#include <wx/stattext.h>
37#include <wx/sizer.h>
38#include <wx/textctrl.h>
39#include <wx/panel.h>
40
41
42wxDEFINE_EVENT( NET_SELECTED, wxCommandEvent );
43
44#if defined( __WXOSX_MAC__ )
45 #define POPUP_PADDING 2
46 #define LIST_ITEM_PADDING 5
47 #define LIST_PADDING 5
48#elif defined( __WXMSW__ )
49 #define POPUP_PADDING 0
50 #define LIST_ITEM_PADDING 2
51 #define LIST_PADDING 5
52#else
53 #define POPUP_PADDING 0
54 #define LIST_ITEM_PADDING 6
55 #define LIST_PADDING 5
56#endif
57
58#define NO_NET _( "<no net>" )
59#define CREATE_NET _( "<create net>" )
60
61
62class NET_SELECTOR_COMBOPOPUP : public wxPanel, public wxComboPopup
63{
64public:
66 m_filterValidator( nullptr ),
67 m_filterCtrl( nullptr ),
68 m_listBox( nullptr ),
69 m_minPopupWidth( -1 ),
70 m_maxPopupHeight( 1000 ),
71 m_netinfoList( nullptr ),
72 m_board( nullptr ),
74 m_focusHandler( nullptr )
75 { }
76
77 bool Create(wxWindow* aParent) override
78 {
79 wxPanel::Create( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER );
80
81 wxBoxSizer* mainSizer;
82 mainSizer = new wxBoxSizer( wxVERTICAL );
83
84 wxStaticText* filterLabel = new wxStaticText( this, wxID_ANY, _( "Filter:" ) );
85 mainSizer->Add( filterLabel, 0, wxEXPAND, 0 );
86
87 m_filterCtrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
88 wxDefaultSize, wxTE_PROCESS_ENTER );
89 m_filterValidator = new wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST );
90 m_filterValidator->SetCharExcludes( " " );
91 m_filterCtrl->SetValidator( *m_filterValidator );
92 mainSizer->Add( m_filterCtrl, 0, wxEXPAND, 0 );
93
94 m_listBox = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, nullptr,
95 wxLB_SINGLE|wxLB_NEEDED_SB );
96 mainSizer->Add( m_listBox, 0, wxEXPAND|wxTOP, 2 );
97
98 SetSizer( mainSizer );
99 Layout();
100
101 Connect( wxEVT_IDLE, wxIdleEventHandler( NET_SELECTOR_COMBOPOPUP::onIdle ), nullptr, this );
102 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR_COMBOPOPUP::onKeyDown ), nullptr, this );
103 Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( NET_SELECTOR_COMBOPOPUP::onMouseClick ), nullptr, this );
104 m_listBox->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( NET_SELECTOR_COMBOPOPUP::onMouseClick ), nullptr, this );
105 m_filterCtrl->Connect( wxEVT_TEXT, wxCommandEventHandler( NET_SELECTOR_COMBOPOPUP::onFilterEdit ), nullptr, this );
106 m_filterCtrl->Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( NET_SELECTOR_COMBOPOPUP::onEnter ), nullptr, this );
107
108 // <enter> in a ListBox comes in as a double-click on GTK
109 m_listBox->Connect( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEventHandler( NET_SELECTOR_COMBOPOPUP::onEnter ), nullptr, this );
110
111 return true;
112 }
113
114 wxWindow *GetControl() override { return this; }
115
116 void SetStringValue( const wxString& aNetName ) override
117 {
118 // shouldn't be here (combo is read-only)
119 }
120
121 wxString GetStringValue() const override
122 {
123 if( m_selectedNetcode == -1 )
125
127
128 if( netInfo && netInfo->GetNetCode() > 0 )
129 return netInfo->GetNetname();
130
131 return NO_NET;
132 }
133
134 void SetNetInfo( NETINFO_LIST* aNetInfoList )
135 {
136 m_netinfoList = aNetInfoList;
137 rebuildList();
138 }
139
140 void SetIndeterminateLabel( const wxString& aIndeterminateLabel )
141 {
142 m_indeterminateLabel = aIndeterminateLabel;
143 rebuildList();
144 }
145
146 void SetBoard( BOARD* aBoard )
147 {
148 m_board = aBoard;
149 }
150
152 bool IsIndeterminate() { return m_selectedNetcode == -1; }
153
154 void SetSelectedNetcode( int aNetcode ) { m_selectedNetcode = aNetcode; }
156
157 void SetSelectedNet( const wxString& aNetname )
158 {
159 if( m_netinfoList && m_netinfoList->GetNetItem( aNetname ) )
161 }
162
164 {
167 else
168 return wxEmptyString;
169 }
170
171 wxSize GetAdjustedSize( int aMinWidth, int aPrefHeight, int aMaxHeight ) override
172 {
173 // Called when the popup is first shown. Stash the minWidth and maxHeight so we
174 // can use them later when refreshing the sizes after filter changes.
175 m_minPopupWidth = aMinWidth;
176 m_maxPopupHeight = aMaxHeight;
177
178 return updateSize();
179 }
180
181 void OnPopup() override
182 {
183 // While it can sometimes be useful to keep the filter, it's always unexpected.
184 // Better to clear it.
185 m_filterCtrl->Clear();
186
187 // The updateSize() call in GetAdjustedSize() leaves the height off-by-one for
188 // some reason, so do it again.
189 updateSize();
190 }
191
192 void OnStartingKey( wxKeyEvent& aEvent )
193 {
195 doStartingKey( aEvent );
196 }
197
198 void Accept()
199 {
200 wxString selectedNetName;
201 wxString escapedNetName;
202 wxString remainingName;
203 int selection = m_listBox->GetSelection();
204
205 if( selection >= 0 )
206 selectedNetName = m_listBox->GetString( (unsigned) selection );
207
208 auto it = m_unescapedNetNameMap.find( selectedNetName );
209
210 if( it != m_unescapedNetNameMap.end() )
211 escapedNetName = it->second;
212 else // shouldn't happen....
213 escapedNetName = selectedNetName;
214
215 Dismiss();
216
217 if( escapedNetName.IsEmpty() || escapedNetName == m_indeterminateLabel )
218 {
220 GetComboCtrl()->SetValue( m_indeterminateLabel );
221 }
222 else if( escapedNetName == NO_NET )
223 {
225 GetComboCtrl()->SetValue( NO_NET );
226 }
227 else if( escapedNetName.StartsWith( CREATE_NET, &remainingName ) &&
228 !remainingName.IsEmpty() )
229 {
230 // Remove the first character ':' and all whitespace
231 remainingName = remainingName.Mid( 1 ).Trim().Trim( false );
232
233 BOARD* board = m_netinfoList->GetParent();
234 NETINFO_ITEM *newnet = new NETINFO_ITEM( m_board, remainingName, 0 );
235
236 // add the new netinfo through the board's function so that
237 // board listeners get notified and things stay in sync.
238 if( board != nullptr )
239 board->Add( newnet );
240 else
241 m_netinfoList->AppendNet( newnet );
242
243 rebuildList();
244
245 if( newnet->GetNetCode() > 0 )
246 {
247 m_selectedNetcode = newnet->GetNetCode();
248 GetComboCtrl()->SetValue( UnescapeString( remainingName ) );
249 }
250 else
251 {
252 // This indicates that the NETINFO_ITEM was not successfully appended
253 // to the list for unknown reasons
254 if( board != nullptr )
255 board->Remove( newnet );
256 else
257 m_netinfoList->RemoveNet( newnet );
258
259 delete newnet;
260 }
261 }
262 else
263 {
264 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( escapedNetName );
265
266 if( netInfo == nullptr || netInfo->GetNetCode() == 0 )
267 {
269 GetComboCtrl()->SetValue( NO_NET );
270 }
271 else
272 {
273 m_selectedNetcode = netInfo->GetNetCode();
274 GetComboCtrl()->SetValue( UnescapeString( escapedNetName ) );
275 }
276 }
277
278 wxCommandEvent changeEvent( NET_SELECTED );
279 wxPostEvent( GetComboCtrl(), changeEvent );
280 }
281
282protected:
283 wxSize updateSize()
284 {
285 int listTop = m_listBox->GetRect().y;
286 int itemHeight = KIUI::GetTextSize( wxT( "Xy" ), this ).y + LIST_ITEM_PADDING;
287 int listHeight = m_listBox->GetCount() * itemHeight + LIST_PADDING;
288
289 if( listTop + listHeight >= m_maxPopupHeight )
290 listHeight = m_maxPopupHeight - listTop - 1;
291
292 int listWidth = m_minPopupWidth;
293
294 for( size_t i = 0; i < m_listBox->GetCount(); ++i )
295 {
296 int itemWidth = KIUI::GetTextSize( m_listBox->GetString( i ), m_listBox ).x;
297 listWidth = std::max( listWidth, itemWidth + LIST_PADDING * 3 );
298 }
299
300 wxSize listSize( listWidth, listHeight );
301 wxSize popupSize( listWidth, listTop + listHeight );
302
303 SetSize( popupSize ); // us
304 GetParent()->SetSize( popupSize ); // the window that wxComboCtrl put us in
305
306 m_listBox->SetMinSize( listSize );
307 m_listBox->SetSize( listSize );
308
309 return popupSize;
310 }
311
313 {
314 wxArrayString netNames;
315 wxString netstring = m_filterCtrl->GetValue().Trim().Trim( false );
316 wxString filter = netstring.Lower();
317
318 m_unescapedNetNameMap.clear();
319
320 if( !filter.IsEmpty() )
321 filter = wxT( "*" ) + filter + wxT( "*" );
322
323 for( NETINFO_ITEM* netinfo : *m_netinfoList )
324 {
325 if( netinfo->GetNetCode() > 0 && netinfo->IsCurrent() )
326 {
327 wxString netname = UnescapeString( netinfo->GetNetname() );
328
329 if( filter.IsEmpty() || wxString( netname ).MakeLower().Matches( filter ) )
330 {
331 netNames.push_back( netname );
332 m_unescapedNetNameMap[ netname ] = netinfo->GetNetname();
333 }
334 }
335 }
336
337 std::sort( netNames.begin(), netNames.end(),
338 []( const wxString& lhs, const wxString& rhs )
339 {
340 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
341 } );
342
343
344 // Special handling for <no net>
345 if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
346 netNames.insert( netNames.begin(), NO_NET );
347
348 if( !filter.IsEmpty() && !m_netinfoList->GetNetItem( netstring ) )
349 {
350 wxString newnet = wxString::Format( "%s: %s", CREATE_NET, netstring );
351 netNames.insert( netNames.end(), newnet );
352 }
353
354 if( !m_indeterminateLabel.IsEmpty() )
355 netNames.push_back( m_indeterminateLabel );
356
357 m_listBox->Set( netNames );
358 }
359
360 void onIdle( wxIdleEvent& aEvent )
361 {
362 // Generate synthetic (but reliable) MouseMoved events
363 static wxPoint lastPos;
364 wxPoint screenPos = wxGetMousePosition();
365
366 if( screenPos != lastPos )
367 {
368 lastPos = screenPos;
369 onMouseMoved( screenPos );
370 }
371
372 if( m_focusHandler )
373 {
374 m_filterCtrl->PushEventHandler( m_focusHandler );
375 m_focusHandler = nullptr;
376 }
377 }
378
379 // Hot-track the mouse (for focus and listbox selection)
380 void onMouseMoved( const wxPoint aScreenPos )
381 {
382 if( m_listBox->GetScreenRect().Contains( aScreenPos ) )
383 {
385
386 wxPoint relativePos = m_listBox->ScreenToClient( aScreenPos );
387 int item = m_listBox->HitTest( relativePos );
388
389 if( item >= 0 )
390 m_listBox->SetSelection( item );
391 }
392 else if( m_filterCtrl->GetScreenRect().Contains( aScreenPos ) )
393 {
395 }
396 }
397
398 void onMouseClick( wxMouseEvent& aEvent )
399 {
400 // Accept a click event from anywhere. Different platform implementations have
401 // different foibles with regard to transient popups and their children.
402
403 if( aEvent.GetEventObject() == m_listBox )
404 {
405 m_listBox->SetSelection( m_listBox->HitTest( aEvent.GetPosition() ) );
406 Accept();
407 return;
408 }
409
410 wxWindow* window = dynamic_cast<wxWindow*>( aEvent.GetEventObject() );
411
412 if( window )
413 {
414 wxPoint screenPos = window->ClientToScreen( aEvent.GetPosition() );
415
416 if( m_listBox->GetScreenRect().Contains( screenPos ) )
417 {
418 wxPoint localPos = m_listBox->ScreenToClient( screenPos );
419
420 m_listBox->SetSelection( m_listBox->HitTest( localPos ) );
421 Accept();
422 }
423 }
424 }
425
426 void onKeyDown( wxKeyEvent& aEvent )
427 {
428 switch( aEvent.GetKeyCode() )
429 {
430 // Control keys go to the parent combobox
431 case WXK_TAB:
432 Dismiss();
433
434 m_parent->NavigateIn( ( aEvent.ShiftDown() ? 0 : wxNavigationKeyEvent::IsForward ) |
435 ( aEvent.ControlDown() ? wxNavigationKeyEvent::WinChange : 0 ) );
436 break;
437
438 case WXK_ESCAPE:
439 Dismiss();
440 break;
441
442 case WXK_RETURN:
443 Accept();
444 break;
445
446 // Arrows go to the list box
447 case WXK_DOWN:
448 case WXK_NUMPAD_DOWN:
450 m_listBox->SetSelection( std::min( m_listBox->GetSelection() + 1, (int) m_listBox->GetCount() - 1 ) );
451 break;
452
453 case WXK_UP:
454 case WXK_NUMPAD_UP:
456 m_listBox->SetSelection( std::max( m_listBox->GetSelection() - 1, 0 ) );
457 break;
458
459 // Everything else goes to the filter textbox
460 default:
461 if( !m_filterCtrl->HasFocus() )
462 {
464
465 // Because we didn't have focus we missed our chance to have the native widget
466 // handle the keystroke. We'll have to do the first character ourselves.
467 doStartingKey( aEvent );
468 }
469 else
470 {
471 // On some platforms a wxComboFocusHandler will have been pushed which
472 // unhelpfully gives the event right back to the popup. Make sure the filter
473 // control is going to get the event.
474 if( m_filterCtrl->GetEventHandler() != m_filterCtrl )
475 m_focusHandler = m_filterCtrl->PopEventHandler();
476
477 aEvent.Skip();
478 }
479 break;
480 }
481 }
482
483 void onEnter( wxCommandEvent& aEvent )
484 {
485 Accept();
486 }
487
488 void onFilterEdit( wxCommandEvent& aEvent )
489 {
490 rebuildList();
491 updateSize();
492
493 if( m_listBox->GetCount() > 0 )
494 m_listBox->SetSelection( 0 );
495 }
496
497 void doStartingKey( wxKeyEvent& aEvent )
498 {
499 if( aEvent.GetKeyCode() == WXK_BACK )
500 {
501 const long pos = m_filterCtrl->GetLastPosition();
502 m_filterCtrl->Remove( pos - 1, pos );
503 }
504 else
505 {
506 bool isPrintable;
507 int ch = aEvent.GetUnicodeKey();
508
509 if( ch != WXK_NONE )
510 isPrintable = true;
511 else
512 {
513 ch = aEvent.GetKeyCode();
514 isPrintable = ch > WXK_SPACE && ch < WXK_START;
515 }
516
517 if( isPrintable )
518 {
519 wxString text( static_cast<wxChar>( ch ) );
520
521 // wxCHAR_HOOK chars have been converted to uppercase.
522 if( !aEvent.ShiftDown() )
523 text.MakeLower();
524
525 m_filterCtrl->AppendText( text );
526 }
527 }
528 }
529
530 void doSetFocus( wxWindow* aWindow )
531 {
533 }
534
535protected:
536 wxTextValidator* m_filterValidator;
537 wxTextCtrl* m_filterCtrl;
538 wxListBox* m_listBox;
541
545
547
548 std::map<wxString, wxString> m_unescapedNetNameMap;
549
550 wxEvtHandler* m_focusHandler;
551};
552
553
554NET_SELECTOR::NET_SELECTOR( wxWindow *parent, wxWindowID id, const wxPoint &pos,
555 const wxSize &size, long style ) :
556 wxComboCtrl( parent, id, wxEmptyString, pos, size, style|wxCB_READONLY|wxTE_PROCESS_ENTER )
557{
558 UseAltPopupWindow();
559
561 SetPopupControl( m_netSelectorPopup );
562
563 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR::onKeyDown ), nullptr, this );
564}
565
566
568{
569 Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR::onKeyDown ), nullptr, this );
570}
571
572
573void NET_SELECTOR::onKeyDown( wxKeyEvent& aEvt )
574{
575 int key = aEvt.GetKeyCode();
576
577 if( IsPopupShown() )
578 {
579 // If the popup is shown then it's CHAR_HOOK should be eating these before they
580 // even get to us. But just to be safe, we go ahead and skip.
581 aEvt.Skip();
582 }
583
584 // Shift-return accepts dialog
585 else if( key == WXK_RETURN && aEvt.ShiftDown() )
586 {
587 wxPostEvent( m_parent, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
588 }
589
590 // Return, arrow-down and space-bar all open popup
591 else if( key == WXK_RETURN || key == WXK_DOWN || key == WXK_NUMPAD_DOWN || key == WXK_SPACE )
592 {
593 Popup();
594 }
595
596 // Non-control characters go to filterbox in popup
597 else if( key > WXK_SPACE && key < WXK_START )
598 {
599 Popup();
601 }
602
603 else
604 {
605 aEvt.Skip();
606 }
607}
608
609
611{
612 m_netSelectorPopup->SetNetInfo( aNetInfoList );
613}
614
615
616void NET_SELECTOR::SetIndeterminateString( const wxString& aString )
617{
618 m_indeterminateString = aString;
620}
621
622
624{
625 m_netSelectorPopup->SetBoard( aBoard );
626}
627
628
630{
633}
634
635
636void NET_SELECTOR::SetSelectedNet( const wxString& aNetname )
637{
640}
641
642
644{
646}
647
648
650{
652 SetValue( m_indeterminateString );
653}
654
655
657{
659}
660
661
663{
665}
666
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:686
void Remove(BOARD_ITEM *aBoardItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL) override
Removes an item from the container.
Definition: board.cpp:795
Handle the data for a net.
Definition: netinfo.h:66
const wxString & GetNetname() const
Definition: netinfo.h:119
int GetNetCode() const
Definition: netinfo.h:113
Container for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:324
BOARD * GetParent() const
Definition: netinfo.h:455
void RemoveNet(NETINFO_ITEM *aNet)
Remove a net from the net list.
NETINFO_ITEM * GetNetItem(int aNetCode) const
void AppendNet(NETINFO_ITEM *aNewElement)
Add aNewElement to the end of the net list.
void onEnter(wxCommandEvent &aEvent)
void SetStringValue(const wxString &aNetName) override
void doSetFocus(wxWindow *aWindow)
void SetSelectedNet(const wxString &aNetname)
void onMouseMoved(const wxPoint aScreenPos)
void onMouseClick(wxMouseEvent &aEvent)
wxEvtHandler * m_focusHandler
wxWindow * GetControl() override
void SetNetInfo(NETINFO_LIST *aNetInfoList)
std::map< wxString, wxString > m_unescapedNetNameMap
void SetIndeterminateLabel(const wxString &aIndeterminateLabel)
wxString GetStringValue() const override
void onKeyDown(wxKeyEvent &aEvent)
void SetSelectedNetcode(int aNetcode)
bool Create(wxWindow *aParent) override
void onFilterEdit(wxCommandEvent &aEvent)
void OnPopup() override
wxTextValidator * m_filterValidator
void SetBoard(BOARD *aBoard)
void onIdle(wxIdleEvent &aEvent)
void OnStartingKey(wxKeyEvent &aEvent)
void doStartingKey(wxKeyEvent &aEvent)
wxSize GetAdjustedSize(int aMinWidth, int aPrefHeight, int aMaxHeight) override
NETINFO_LIST * m_netinfoList
wxString m_indeterminateString
Definition: net_selector.h:68
NET_SELECTOR_COMBOPOPUP * m_netSelectorPopup
Definition: net_selector.h:67
NET_SELECTOR(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0)
bool IsIndeterminate()
void SetBoard(BOARD *aBoard)
int GetSelectedNetcode()
void SetIndeterminateString(const wxString &aString)
void SetSelectedNetcode(int aNetcode)
void onKeyDown(wxKeyEvent &aEvt)
void SetIndeterminate()
void SetNetInfo(NETINFO_LIST *aNetInfoList)
void SetSelectedNet(const wxString &aNetname)
~NET_SELECTOR() override
wxString GetSelectedNetname()
#define _(s)
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition: gtk/ui.cpp:44
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: ui_common.cpp:70
wxDEFINE_EVENT(NET_SELECTED, wxCommandEvent)
#define LIST_ITEM_PADDING
#define LIST_PADDING
#define CREATE_NET
#define NO_NET
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxString UnescapeString(const wxString &aSource)
Functions to provide common constants and other functions to assist in making a consistent UI.