2020-08-18 18:32:43 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-07-21 21:37:49 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-08-18 18:32:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-12-17 16:43:46 +00:00
|
|
|
#include <dialogs/eda_view_switcher.h>
|
2020-08-18 18:32:43 +00:00
|
|
|
|
|
|
|
|
2020-08-19 15:25:57 +00:00
|
|
|
#ifdef __WXGTK__
|
|
|
|
#define LIST_BOX_H_PADDING 20
|
|
|
|
#define LIST_BOX_V_PADDING 8
|
|
|
|
#else
|
2020-08-19 10:31:20 +00:00
|
|
|
#define LIST_BOX_H_PADDING 10
|
|
|
|
#define LIST_BOX_V_PADDING 5
|
2020-08-19 15:25:57 +00:00
|
|
|
#endif
|
2020-08-18 18:32:43 +00:00
|
|
|
|
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
EDA_VIEW_SWITCHER::EDA_VIEW_SWITCHER( wxWindow* aParent, const wxArrayString& aItems ) :
|
|
|
|
EDA_VIEW_SWITCHER_BASE( aParent ),
|
2020-08-19 12:12:48 +00:00
|
|
|
m_tabState( true ),
|
2020-08-28 17:40:32 +00:00
|
|
|
m_receivingEvents( false )
|
2020-08-18 18:32:43 +00:00
|
|
|
{
|
2020-08-19 10:31:20 +00:00
|
|
|
m_listBox->InsertItems( aItems, 0 );
|
|
|
|
m_listBox->SetSelection( std::min( 1, (int) m_listBox->GetCount() - 1 ) );
|
2020-08-18 18:32:43 +00:00
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
2020-08-18 18:32:43 +00:00
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
for( const wxString& item : aItems )
|
|
|
|
{
|
|
|
|
wxSize extents = m_listBox->GetTextExtent( item );
|
|
|
|
width = std::max( width, extents.x );
|
|
|
|
height += extents.y + LIST_BOX_V_PADDING;
|
|
|
|
}
|
2020-08-18 18:32:43 +00:00
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
m_listBox->SetMinSize( wxSize( width + LIST_BOX_H_PADDING, height ) );
|
|
|
|
SetInitialFocus( m_listBox );
|
2020-08-18 18:32:43 +00:00
|
|
|
|
|
|
|
// this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
|
|
|
|
// and works fine on all systems
|
2020-08-19 10:31:20 +00:00
|
|
|
GetSizer()->Fit( this );
|
2020-08-18 18:32:43 +00:00
|
|
|
|
|
|
|
Centre();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-28 17:40:32 +00:00
|
|
|
bool EDA_VIEW_SWITCHER::Show( bool aShow )
|
|
|
|
{
|
|
|
|
if( !aShow )
|
|
|
|
m_receivingEvents = false;
|
|
|
|
|
|
|
|
bool ret = DIALOG_SHIM::Show( aShow );
|
|
|
|
|
|
|
|
if( aShow )
|
2020-10-17 19:54:28 +00:00
|
|
|
{
|
2020-08-28 17:40:32 +00:00
|
|
|
m_receivingEvents = true;
|
|
|
|
|
2020-10-17 19:54:28 +00:00
|
|
|
// Force the dialog to always be centered over the window
|
|
|
|
Centre();
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:40:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
// OK, this is *really* annoying, but wxWidgets doesn't give us key-down events while the
|
|
|
|
// control key is being held down. So we can't use OnKeyDown() or OnCharHook() and instead
|
|
|
|
// must rely on watching key states in TryBefore().
|
|
|
|
//
|
|
|
|
// Just checking the state of the tab key is tempting, but then we'll think it's been hit
|
|
|
|
// several times when it's actually just a key-down followed by a redraw or idle event.
|
|
|
|
//
|
|
|
|
// So we have to keep a state machine of the tab key.
|
|
|
|
//
|
|
|
|
bool EDA_VIEW_SWITCHER::TryBefore( wxEvent& aEvent )
|
2020-08-18 18:32:43 +00:00
|
|
|
{
|
2020-08-19 12:12:48 +00:00
|
|
|
if( !m_receivingEvents )
|
|
|
|
{
|
|
|
|
return DIALOG_SHIM::TryBefore( aEvent );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for tab key leading edge
|
|
|
|
if( !m_tabState && wxGetKeyState( WXK_TAB ) )
|
2020-08-18 18:32:43 +00:00
|
|
|
{
|
2020-08-19 10:31:20 +00:00
|
|
|
m_tabState = true;
|
2020-08-18 18:32:43 +00:00
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
int idx = m_listBox->GetSelection();
|
2020-08-18 18:32:43 +00:00
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
if( wxGetKeyState( WXK_SHIFT ) )
|
2020-08-18 18:32:43 +00:00
|
|
|
{
|
2020-08-19 23:44:15 +00:00
|
|
|
if( --idx < 0 )
|
2020-08-19 10:31:20 +00:00
|
|
|
m_listBox->SetSelection( (int) m_listBox->GetCount() - 1 );
|
2020-08-18 18:32:43 +00:00
|
|
|
else
|
2020-08-19 10:31:20 +00:00
|
|
|
m_listBox->SetSelection( idx );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ++idx >= (int) m_listBox->GetCount() )
|
|
|
|
m_listBox->SetSelection( 0 );
|
|
|
|
else
|
|
|
|
m_listBox->SetSelection( idx );
|
2020-08-18 18:32:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-08-18 18:32:43 +00:00
|
|
|
|
2020-08-19 12:12:48 +00:00
|
|
|
// Check for tab key trailing edge
|
|
|
|
if( m_tabState && !wxGetKeyState( WXK_TAB ) )
|
|
|
|
{
|
|
|
|
m_tabState = false;
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:40:32 +00:00
|
|
|
// Check for control key trailing edge
|
|
|
|
if( !wxGetKeyState( WXK_RAW_CONTROL ) )
|
|
|
|
{
|
2021-07-21 21:37:49 +00:00
|
|
|
wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
|
2020-08-28 17:40:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 10:31:20 +00:00
|
|
|
return DIALOG_SHIM::TryBefore( aEvent );
|
2020-08-18 18:32:43 +00:00
|
|
|
}
|