KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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( const 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 wxASSERT( board );
237
238 if( board )
239 board->Add( newnet );
240
241 rebuildList();
242
243 if( newnet->GetNetCode() > 0 )
244 {
245 m_selectedNetcode = newnet->GetNetCode();
246 GetComboCtrl()->SetValue( UnescapeString( remainingName ) );
247 }
248 else
249 {
250 // This indicates that the NETINFO_ITEM was not successfully appended to the
251 // list for unknown reasons
252 if( board )
253 board->Remove( newnet );
254
255 delete newnet;
256 }
257 }
258 else
259 {
260 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( escapedNetName );
261
262 if( netInfo == nullptr || netInfo->GetNetCode() == 0 )
263 {
265 GetComboCtrl()->SetValue( NO_NET );
266 }
267 else
268 {
269 m_selectedNetcode = netInfo->GetNetCode();
270 GetComboCtrl()->SetValue( UnescapeString( escapedNetName ) );
271 }
272 }
273
274 wxCommandEvent changeEvent( NET_SELECTED );
275 wxPostEvent( GetComboCtrl(), changeEvent );
276 }
277
278protected:
279 wxSize updateSize()
280 {
281 int listTop = m_listBox->GetRect().y;
282 int itemHeight = KIUI::GetTextSize( wxT( "Xy" ), this ).y + LIST_ITEM_PADDING;
283 int listHeight = m_listBox->GetCount() * itemHeight + LIST_PADDING;
284
285 if( listTop + listHeight >= m_maxPopupHeight )
286 listHeight = m_maxPopupHeight - listTop - 1;
287
288 int listWidth = m_minPopupWidth;
289
290 for( size_t i = 0; i < m_listBox->GetCount(); ++i )
291 {
292 int itemWidth = KIUI::GetTextSize( m_listBox->GetString( i ), m_listBox ).x;
293 listWidth = std::max( listWidth, itemWidth + LIST_PADDING * 3 );
294 }
295
296 wxSize listSize( listWidth, listHeight );
297 wxSize popupSize( listWidth, listTop + listHeight );
298
299 SetSize( popupSize ); // us
300 GetParent()->SetSize( popupSize ); // the window that wxComboCtrl put us in
301
302 m_listBox->SetMinSize( listSize );
303 m_listBox->SetSize( listSize );
304
305 return popupSize;
306 }
307
309 {
310 wxArrayString netNames;
311 wxString netstring = m_filterCtrl->GetValue().Trim().Trim( false );
312 wxString filter = netstring.Lower();
313
314 m_unescapedNetNameMap.clear();
315
316 if( !filter.IsEmpty() )
317 filter = wxT( "*" ) + filter + wxT( "*" );
318
319 for( NETINFO_ITEM* netinfo : *m_netinfoList )
320 {
321 if( netinfo->GetNetCode() > 0 && netinfo->IsCurrent() )
322 {
323 wxString netname = UnescapeString( netinfo->GetNetname() );
324
325 if( filter.IsEmpty() || wxString( netname ).MakeLower().Matches( filter ) )
326 {
327 netNames.push_back( netname );
328 m_unescapedNetNameMap[ netname ] = netinfo->GetNetname();
329 }
330 }
331 }
332
333 std::sort( netNames.begin(), netNames.end(),
334 []( const wxString& lhs, const wxString& rhs )
335 {
336 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
337 } );
338
339
340 // Special handling for <no net>
341 if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
342 netNames.insert( netNames.begin(), NO_NET );
343
344 if( !filter.IsEmpty() && !m_netinfoList->GetNetItem( netstring ) )
345 {
346 wxString newnet = wxString::Format( "%s: %s", CREATE_NET, netstring );
347 netNames.insert( netNames.end(), newnet );
348 }
349
350 if( !m_indeterminateLabel.IsEmpty() )
351 netNames.push_back( m_indeterminateLabel );
352
353 m_listBox->Set( netNames );
354 }
355
356 void onIdle( wxIdleEvent& aEvent )
357 {
358 // Generate synthetic (but reliable) MouseMoved events
359 static wxPoint lastPos;
360 wxPoint screenPos = KIPLATFORM::UI::GetMousePosition();
361
362 if( screenPos != lastPos )
363 {
364 lastPos = screenPos;
365 onMouseMoved( screenPos );
366 }
367
368 if( m_focusHandler )
369 {
370 m_filterCtrl->PushEventHandler( m_focusHandler );
371 m_focusHandler = nullptr;
372 }
373 }
374
375 // Hot-track the mouse (for focus and listbox selection)
376 void onMouseMoved( const wxPoint aScreenPos )
377 {
378 if( m_listBox->GetScreenRect().Contains( aScreenPos ) )
379 {
381
382 wxPoint relativePos = m_listBox->ScreenToClient( aScreenPos );
383 int item = m_listBox->HitTest( relativePos );
384
385 if( item >= 0 )
386 m_listBox->SetSelection( item );
387 }
388 else if( m_filterCtrl->GetScreenRect().Contains( aScreenPos ) )
389 {
391 }
392 }
393
394 void onMouseClick( wxMouseEvent& aEvent )
395 {
396 // Accept a click event from anywhere. Different platform implementations have
397 // different foibles with regard to transient popups and their children.
398
399 if( aEvent.GetEventObject() == m_listBox )
400 {
401 m_listBox->SetSelection( m_listBox->HitTest( aEvent.GetPosition() ) );
402 Accept();
403 return;
404 }
405
406 wxWindow* window = dynamic_cast<wxWindow*>( aEvent.GetEventObject() );
407
408 if( window )
409 {
410 wxPoint screenPos = window->ClientToScreen( aEvent.GetPosition() );
411
412 if( m_listBox->GetScreenRect().Contains( screenPos ) )
413 {
414 wxPoint localPos = m_listBox->ScreenToClient( screenPos );
415
416 m_listBox->SetSelection( m_listBox->HitTest( localPos ) );
417 Accept();
418 }
419 }
420 }
421
422 void onKeyDown( wxKeyEvent& aEvent )
423 {
424 switch( aEvent.GetKeyCode() )
425 {
426 // Control keys go to the parent combobox
427 case WXK_TAB:
428 Dismiss();
429
430 m_parent->NavigateIn( ( aEvent.ShiftDown() ? 0 : wxNavigationKeyEvent::IsForward ) |
431 ( aEvent.ControlDown() ? wxNavigationKeyEvent::WinChange : 0 ) );
432 break;
433
434 case WXK_ESCAPE:
435 Dismiss();
436 break;
437
438 case WXK_RETURN:
439 case WXK_NUMPAD_ENTER:
440 Accept();
441 break;
442
443 // Arrows go to the list box
444 case WXK_DOWN:
445 case WXK_NUMPAD_DOWN:
447 m_listBox->SetSelection( std::min( m_listBox->GetSelection() + 1, (int) m_listBox->GetCount() - 1 ) );
448 break;
449
450 case WXK_UP:
451 case WXK_NUMPAD_UP:
453 m_listBox->SetSelection( std::max( m_listBox->GetSelection() - 1, 0 ) );
454 break;
455
456 // Everything else goes to the filter textbox
457 default:
458 if( !m_filterCtrl->HasFocus() )
459 {
461
462 // Because we didn't have focus we missed our chance to have the native widget
463 // handle the keystroke. We'll have to do the first character ourselves.
464 doStartingKey( aEvent );
465 }
466 else
467 {
468 // On some platforms a wxComboFocusHandler will have been pushed which
469 // unhelpfully gives the event right back to the popup. Make sure the filter
470 // control is going to get the event.
471 if( m_filterCtrl->GetEventHandler() != m_filterCtrl )
472 m_focusHandler = m_filterCtrl->PopEventHandler();
473
474 aEvent.Skip();
475 }
476 break;
477 }
478 }
479
480 void onEnter( wxCommandEvent& aEvent )
481 {
482 Accept();
483 }
484
485 void onFilterEdit( wxCommandEvent& aEvent )
486 {
487 rebuildList();
488 updateSize();
489
490 if( m_listBox->GetCount() > 0 )
491 m_listBox->SetSelection( 0 );
492 }
493
494 void doStartingKey( wxKeyEvent& aEvent )
495 {
496 if( aEvent.GetKeyCode() == WXK_BACK )
497 {
498 const long pos = m_filterCtrl->GetLastPosition();
499 m_filterCtrl->Remove( pos - 1, pos );
500 }
501 else
502 {
503 bool isPrintable;
504 int ch = aEvent.GetUnicodeKey();
505
506 if( ch != WXK_NONE )
507 isPrintable = true;
508 else
509 {
510 ch = aEvent.GetKeyCode();
511 isPrintable = ch > WXK_SPACE && ch < WXK_START;
512 }
513
514 if( isPrintable )
515 {
516 wxString text( static_cast<wxChar>( ch ) );
517
518 // wxCHAR_HOOK chars have been converted to uppercase.
519 if( !aEvent.ShiftDown() )
520 text.MakeLower();
521
522 m_filterCtrl->AppendText( text );
523 }
524 }
525 }
526
527 void doSetFocus( wxWindow* aWindow )
528 {
530 }
531
532protected:
533 wxTextValidator* m_filterValidator;
534 wxTextCtrl* m_filterCtrl;
535 wxListBox* m_listBox;
538
542
544
545 std::map<wxString, wxString> m_unescapedNetNameMap;
546
547 wxEvtHandler* m_focusHandler;
548};
549
550
551NET_SELECTOR::NET_SELECTOR( wxWindow *parent, wxWindowID id, const wxPoint &pos,
552 const wxSize &size, long style ) :
553 wxComboCtrl( parent, id, wxEmptyString, pos, size, style|wxCB_READONLY|wxTE_PROCESS_ENTER )
554{
555 UseAltPopupWindow();
556
558 SetPopupControl( m_netSelectorPopup );
559
560 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR::onKeyDown ), nullptr, this );
561}
562
563
565{
566 Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR::onKeyDown ), nullptr, this );
567}
568
569
570void NET_SELECTOR::onKeyDown( wxKeyEvent& aEvt )
571{
572 int key = aEvt.GetKeyCode();
573
574 if( IsPopupShown() )
575 {
576 // If the popup is shown then it's CHAR_HOOK should be eating these before they
577 // even get to us. But just to be safe, we go ahead and skip.
578 aEvt.Skip();
579 }
580
581 // Shift-return accepts dialog
582 else if( ( key == WXK_RETURN || key == WXK_NUMPAD_ENTER ) && aEvt.ShiftDown() )
583 {
584 wxPostEvent( m_parent, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
585 }
586
587 // Return, arrow-down and space-bar all open popup
588 else if( key == WXK_RETURN || key == WXK_NUMPAD_ENTER || key == WXK_DOWN
589 || key == WXK_NUMPAD_DOWN || key == WXK_SPACE )
590 {
591 Popup();
592 }
593
594 // Non-control characters go to filterbox in popup
595 else if( key > WXK_SPACE && key < WXK_START )
596 {
597 Popup();
599 }
600
601 else
602 {
603 aEvt.Skip();
604 }
605}
606
607
608void NET_SELECTOR::SetNetInfo( const NETINFO_LIST* aNetInfoList )
609{
610 m_netSelectorPopup->SetNetInfo( aNetInfoList );
611}
612
613
614void NET_SELECTOR::SetIndeterminateString( const wxString& aString )
615{
616 m_indeterminateString = aString;
618}
619
620
622{
623 m_netSelectorPopup->SetBoard( aBoard );
624}
625
626
628{
631}
632
633
634void NET_SELECTOR::SetSelectedNet( const wxString& aNetname )
635{
638}
639
640
642{
644}
645
646
648{
650 SetValue( m_indeterminateString );
651}
652
653
655{
657}
658
659
661{
663}
664
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:276
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:855
void Remove(BOARD_ITEM *aBoardItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL) override
Removes an item from the container.
Definition: board.cpp:970
Handle the data for a net.
Definition: netinfo.h:56
const wxString & GetNetname() const
Definition: netinfo.h:114
int GetNetCode() const
Definition: netinfo.h:108
Container for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:337
BOARD * GetParent() const
Definition: netinfo.h:452
NETINFO_ITEM * GetNetItem(int aNetCode) const
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
const NETINFO_LIST * m_netinfoList
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 SetNetInfo(const NETINFO_LIST *aNetInfoList)
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
void SetNetInfo(const NETINFO_LIST *aNetInfoList)
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 SetSelectedNet(const wxString &aNetname)
~NET_SELECTOR() override
wxString GetSelectedNetname()
#define _(s)
wxPoint GetMousePosition()
Returns the mouse position in screen coordinates.
Definition: gtk/ui.cpp:588
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition: gtk/ui.cpp:67
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:74
wxDEFINE_EVENT(NET_SELECTED, wxCommandEvent)
#define LIST_ITEM_PADDING
#define LIST_PADDING
#define CREATE_NET
#define NO_NET
wxString UnescapeString(const wxString &aSource)
Functions to provide common constants and other functions to assist in making a consistent UI.