Improve visiblity of net selector popup.

This commit is contained in:
Jeff Young 2018-09-28 22:39:08 +01:00
parent 31c217426f
commit 3f6819e592
1 changed files with 63 additions and 62 deletions

View File

@ -81,7 +81,7 @@ public:
{ {
// A first mouse-up inside the popup represents a drag-style menu selection // A first mouse-up inside the popup represents a drag-style menu selection
if( m_popup->GetScreenRect().Contains( wxGetMousePosition() ) ) if( m_popup->GetScreenRect().Contains( wxGetMousePosition() ) )
m_popup->EndModal( wxID_APPLY ); m_popup->EndModal( wxID_OK );
// Otherwise the first mouse-up is sent back to the combox button // Otherwise the first mouse-up is sent back to the combox button
else if( m_combobox->GetButton() ) else if( m_combobox->GetButton() )
@ -108,42 +108,48 @@ public:
NET_SELECTOR_POPUP( wxComboCtrl* aParent, const wxPoint& aPos, const wxSize& aSize, NET_SELECTOR_POPUP( wxComboCtrl* aParent, const wxPoint& aPos, const wxSize& aSize,
NETINFO_LIST* aNetInfoList ) : NETINFO_LIST* aNetInfoList ) :
wxDialog( aParent->GetParent(), wxID_ANY, wxEmptyString, aPos, aSize, wxDialog( aParent->GetParent(), wxID_ANY, wxEmptyString, aPos, aSize,
wxSIMPLE_BORDER|wxWANTS_CHARS ), wxWANTS_CHARS ),
m_parentCombobox( aParent ), m_parentCombobox( aParent ),
m_popupWidth( -1 ), m_minPopupWidth( -1 ),
m_maxPopupHeight( 1000 ), m_maxPopupHeight( 1000 ),
m_netinfoList( aNetInfoList ), m_netinfoList( aNetInfoList ),
m_filterCtrl( nullptr ), m_filterCtrl( nullptr ),
m_netListBox( nullptr ), m_listBox( nullptr ),
m_initialized( false ), m_initialized( false ),
m_selectedNetcode( 0 ), m_selectedNetcode( 0 ),
m_retCode( 0 ) m_retCode( 0 )
{ {
SetExtraStyle( wxWS_EX_BLOCK_EVENTS|wxWS_EX_PROCESS_IDLE ); SetExtraStyle( wxWS_EX_BLOCK_EVENTS|wxWS_EX_PROCESS_IDLE );
m_popupWidth = aSize.x; m_minPopupWidth = aSize.x;
m_maxPopupHeight = aSize.y; m_maxPopupHeight = aSize.y;
wxBoxSizer* mainSizer; auto mainSizer = new wxBoxSizer( wxVERTICAL );
mainSizer = new wxBoxSizer( wxVERTICAL ); auto panelSizer = new wxBoxSizer( wxVERTICAL );
wxStaticText* title = new wxStaticText( this, wxID_ANY, _( "Filter:" ) ); auto panel = new wxPanel( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
mainSizer->Add( title, 0, wxLEFT|wxRIGHT, 2 ); wxSIMPLE_BORDER );
mainSizer->Add( panel, 1, wxEXPAND, 0 );
m_filterCtrl = new wxTextCtrl( this, wxID_ANY ); wxStaticText* title = new wxStaticText( panel, wxID_ANY, _( "Filter:" ) );
mainSizer->Add( m_filterCtrl, 0, wxEXPAND|wxLEFT|wxRIGHT, 2 ); panelSizer->Add( title, 0, wxEXPAND, 0 );
m_netListBox = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, 0, wxLB_NEEDED_SB ); m_filterCtrl = new wxTextCtrl( panel, wxID_ANY );
mainSizer->Add( m_netListBox, 0, wxALL|wxEXPAND, 2 ); panelSizer->Add( m_filterCtrl, 0, wxEXPAND, 0 );
SetSizer( mainSizer ); m_listBox = new wxListBox( panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, 0,
wxLB_NEEDED_SB );
panelSizer->Add( m_listBox, 0, wxEXPAND|wxTOP, 2 );
panel->SetSizer( panelSizer );
this->SetSizer( mainSizer );
Layout(); Layout();
Connect( wxEVT_IDLE, wxIdleEventHandler( NET_SELECTOR_POPUP::onIdle ), NULL, this ); Connect( wxEVT_IDLE, wxIdleEventHandler( NET_SELECTOR_POPUP::onIdle ), NULL, this );
Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this ); Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this );
Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this ); Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this );
m_netListBox->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( NET_SELECTOR_POPUP::onListBoxMouseClick ), NULL, this ); m_listBox->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( NET_SELECTOR_POPUP::onListBoxMouseClick ), NULL, this );
m_netListBox->Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this ); m_listBox->Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this );
m_filterCtrl->Connect( wxEVT_TEXT, wxCommandEventHandler( NET_SELECTOR_POPUP::onFilterEdit ), NULL, this ); m_filterCtrl->Connect( wxEVT_TEXT, wxCommandEventHandler( NET_SELECTOR_POPUP::onFilterEdit ), NULL, this );
rebuildList(); rebuildList();
@ -154,15 +160,15 @@ public:
Disconnect( wxEVT_IDLE, wxIdleEventHandler( NET_SELECTOR_POPUP::onIdle ), NULL, this ); Disconnect( wxEVT_IDLE, wxIdleEventHandler( NET_SELECTOR_POPUP::onIdle ), NULL, this );
Disconnect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this ); Disconnect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this );
Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this ); Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this );
m_netListBox->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( NET_SELECTOR_POPUP::onListBoxMouseClick ), NULL, this ); m_listBox->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( NET_SELECTOR_POPUP::onListBoxMouseClick ), NULL, this );
m_netListBox->Disconnect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this ); m_listBox->Disconnect( wxEVT_KEY_DOWN, wxKeyEventHandler( NET_SELECTOR_POPUP::onKeyDown ), NULL, this );
m_filterCtrl->Disconnect( wxEVT_TEXT, wxCommandEventHandler( NET_SELECTOR_POPUP::onFilterEdit ), NULL, this ); m_filterCtrl->Disconnect( wxEVT_TEXT, wxCommandEventHandler( NET_SELECTOR_POPUP::onFilterEdit ), NULL, this );
} }
void SetSelectedNetcode( int aNetcode ) void SetSelectedNetcode( int aNetcode )
{ {
m_selectedNetcode = aNetcode; m_selectedNetcode = aNetcode;
m_netListBox->SetFocus(); m_listBox->SetFocus();
} }
int GetSelectedNetcode() int GetSelectedNetcode()
@ -177,7 +183,7 @@ public:
POPUP_EVENTFILTER filter( this, m_parentCombobox ); POPUP_EVENTFILTER filter( this, m_parentCombobox );
Show( true ); Show( true );
doSetFocus( m_netListBox ); doSetFocus( m_listBox );
while( !m_retCode ) while( !m_retCode )
wxYield(); wxYield();
@ -192,53 +198,48 @@ public:
if( !m_retCode ) if( !m_retCode )
{ {
if( aReason == wxID_APPLY ) if( aReason == wxID_OK )
{ {
wxString selectedNetName; wxString selectedNetName;
int selection = m_netListBox->GetSelection(); int selection = m_listBox->GetSelection();
if( selection >= 0 ) if( selection >= 0 )
m_netListBox->GetString( (unsigned) selection ); selectedNetName = m_listBox->GetString( (unsigned) selection );
if( selectedNetName.IsEmpty() ) if( selectedNetName.IsEmpty() )
{ aReason = wxID_CANCEL;
m_selectedNetcode = -1;
m_retCode = wxID_CANCEL;
}
else if( selectedNetName == NO_NET ) else if( selectedNetName == NO_NET )
{
m_selectedNetcode = 0; m_selectedNetcode = 0;
m_retCode = wxID_OK ;
}
else else
{
m_selectedNetcode = m_netinfoList->GetNetItem( selectedNetName )->GetNet(); m_selectedNetcode = m_netinfoList->GetNetItem( selectedNetName )->GetNet();
m_retCode = wxID_OK ;
}
} }
else
m_retCode = aReason; m_retCode = aReason;
} }
} }
protected: protected:
void updateSize() void updateSize()
{ {
wxSize popupSize( m_popupWidth, m_maxPopupHeight ); int listTop = m_listBox->GetRect().y;
int listTop = m_netListBox->GetRect().y;
int itemHeight = GetTextSize( wxT( "Xy" ), this ).y + LIST_ITEM_PADDING; int itemHeight = GetTextSize( wxT( "Xy" ), this ).y + LIST_ITEM_PADDING;
int listHeight = m_netListBox->GetCount() * itemHeight + LIST_PADDING; int listHeight = m_listBox->GetCount() * itemHeight + LIST_PADDING;
if( listTop + listHeight >= m_maxPopupHeight ) if( listTop + listHeight >= m_maxPopupHeight )
listHeight = m_maxPopupHeight - listTop - 1; listHeight = m_maxPopupHeight - listTop - 1;
wxSize listSize( m_popupWidth, listHeight ); int listWidth = m_minPopupWidth;
m_netListBox->SetMinSize( listSize ); for( size_t i = 0; i < m_listBox->GetCount(); ++i )
m_netListBox->SetSize( listSize ); {
int itemWidth = GetTextSize( m_listBox->GetString( i ), m_listBox ).x;
listWidth = std::max( listWidth, itemWidth + LIST_PADDING * 3 );
}
popupSize.y = listTop + listHeight; m_listBox->SetMinSize( wxSize( listWidth, listHeight ) );
SetSize( popupSize ); m_listBox->SetSize( wxSize( listWidth, listHeight ) );
SetSize( wxSize( listWidth, listTop + listHeight ) );
} }
void rebuildList() void rebuildList()
@ -262,10 +263,10 @@ protected:
if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) ) if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
netNames.insert( netNames.begin(), NO_NET ); netNames.insert( netNames.begin(), NO_NET );
m_netListBox->Set( netNames ); m_listBox->Set( netNames );
updateSize(); updateSize();
m_netListBox->Refresh(); m_listBox->Refresh();
} }
void onIdle( wxIdleEvent& aEvent ) void onIdle( wxIdleEvent& aEvent )
@ -288,7 +289,7 @@ protected:
// platform-dependant. // platform-dependant.
wxWindow* focus = wxWindow::FindFocus(); wxWindow* focus = wxWindow::FindFocus();
if( m_initialized && focus != this && focus != m_netListBox && focus != m_filterCtrl ) if( m_initialized && focus != this && focus != m_listBox && focus != m_filterCtrl )
EndModal( wxID_CANCEL ); EndModal( wxID_CANCEL );
#endif #endif
} }
@ -296,15 +297,15 @@ protected:
// Hot-track the mouse (for focus and listbox selection) // Hot-track the mouse (for focus and listbox selection)
void onMouseMoved( const wxPoint aScreenPos ) void onMouseMoved( const wxPoint aScreenPos )
{ {
if( m_netListBox->GetScreenRect().Contains( aScreenPos ) ) if( m_listBox->GetScreenRect().Contains( aScreenPos ) )
{ {
doSetFocus( m_netListBox ); doSetFocus( m_listBox );
wxPoint relativePos = m_netListBox->ScreenToClient( aScreenPos ); wxPoint relativePos = m_listBox->ScreenToClient( aScreenPos );
int item = m_netListBox->HitTest( relativePos ); int item = m_listBox->HitTest( relativePos );
if( item >= 0 ) if( item >= 0 )
m_netListBox->SetSelection( item ); m_listBox->SetSelection( item );
} }
else if( m_filterCtrl->GetScreenRect().Contains( aScreenPos ) ) else if( m_filterCtrl->GetScreenRect().Contains( aScreenPos ) )
{ {
@ -312,7 +313,7 @@ protected:
} }
else if( !m_initialized ) else if( !m_initialized )
{ {
doSetFocus( m_netListBox ); doSetFocus( m_listBox );
m_initialized = true; m_initialized = true;
} }
} }
@ -333,19 +334,19 @@ protected:
break; break;
case WXK_RETURN: case WXK_RETURN:
EndModal( wxID_APPLY ); EndModal( wxID_OK );
break; break;
case WXK_DOWN: case WXK_DOWN:
case WXK_NUMPAD_DOWN: case WXK_NUMPAD_DOWN:
doSetFocus( m_netListBox ); doSetFocus( m_listBox );
m_netListBox->SetSelection( std::min( m_netListBox->GetSelection() + 1, (int) m_netListBox->GetCount() - 1 ) ); m_listBox->SetSelection( std::min( m_listBox->GetSelection() + 1, (int) m_listBox->GetCount() - 1 ) );
break; break;
case WXK_UP: case WXK_UP:
case WXK_NUMPAD_UP: case WXK_NUMPAD_UP:
doSetFocus( m_netListBox ); doSetFocus( m_listBox );
m_netListBox->SetSelection( std::max( m_netListBox->GetSelection() - 1, 0 ) ); m_listBox->SetSelection( std::max( m_listBox->GetSelection() - 1, 0 ) );
break; break;
default: default:
@ -361,8 +362,8 @@ protected:
void onListBoxMouseClick( wxMouseEvent& aEvent ) void onListBoxMouseClick( wxMouseEvent& aEvent )
{ {
m_netListBox->SetSelection( m_netListBox->HitTest( aEvent.GetPosition() ) ); m_listBox->SetSelection( m_listBox->HitTest( aEvent.GetPosition() ) );
EndModal( wxID_APPLY ); EndModal( wxID_OK );
} }
void doSetFocus( wxWindow* aWindow ) void doSetFocus( wxWindow* aWindow )
@ -376,12 +377,12 @@ protected:
protected: protected:
wxComboCtrl* m_parentCombobox; wxComboCtrl* m_parentCombobox;
int m_popupWidth; int m_minPopupWidth;
int m_maxPopupHeight; int m_maxPopupHeight;
NETINFO_LIST* m_netinfoList; NETINFO_LIST* m_netinfoList;
wxTextCtrl* m_filterCtrl; wxTextCtrl* m_filterCtrl;
wxListBox* m_netListBox; wxListBox* m_listBox;
bool m_initialized; bool m_initialized;
@ -405,7 +406,7 @@ NET_SELECTOR::~NET_SELECTOR()
} }
void NET_SELECTOR::DoSetPopupControl( wxComboPopup* aPopup ) void NET_SELECTOR::DoSetPopupControl( wxComboPopup* )
{ {
m_popup = nullptr; m_popup = nullptr;
} }