2020-07-11 17:42:00 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-02-23 17:39:06 +00:00
|
|
|
* Copyright (C) 2020-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-07-11 17:42:00 +00:00
|
|
|
* @author Jon Evans <jon@craftyjon.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <widgets/bitmap_toggle.h>
|
|
|
|
|
2021-06-03 01:19:20 +00:00
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/statbmp.h>
|
2022-02-23 17:39:06 +00:00
|
|
|
#include <wx/timer.h>
|
2020-07-11 17:42:00 +00:00
|
|
|
|
|
|
|
wxDEFINE_EVENT( TOGGLE_CHANGED, wxCommandEvent );
|
|
|
|
|
|
|
|
|
2023-10-21 19:39:51 +00:00
|
|
|
BITMAP_TOGGLE::BITMAP_TOGGLE( wxWindow *aParent, wxWindowID aId,
|
|
|
|
const wxBitmapBundle& aCheckedBitmap,
|
|
|
|
const wxBitmapBundle& aUncheckedBitmap, bool aChecked ) :
|
2020-07-11 17:42:00 +00:00
|
|
|
wxPanel( aParent, aId ),
|
|
|
|
m_checked( aChecked ),
|
|
|
|
m_unchecked_bitmap( aUncheckedBitmap ),
|
2022-02-23 17:39:06 +00:00
|
|
|
m_checked_bitmap( aCheckedBitmap ),
|
|
|
|
m_debounce( 0 )
|
2020-07-11 17:42:00 +00:00
|
|
|
{
|
|
|
|
wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
|
|
|
|
SetSizer( sizer );
|
|
|
|
|
2023-10-21 19:39:51 +00:00
|
|
|
const wxBitmapBundle& bundle = aChecked ? m_checked_bitmap : m_unchecked_bitmap;
|
2020-07-11 17:42:00 +00:00
|
|
|
|
2023-10-21 19:39:51 +00:00
|
|
|
m_bitmap = new wxStaticBitmap( this, aId, bundle, wxDefaultPosition );
|
2020-07-11 17:42:00 +00:00
|
|
|
|
|
|
|
sizer->Add( m_bitmap, 0, 0 );
|
|
|
|
|
|
|
|
m_bitmap->Bind( wxEVT_LEFT_UP,
|
2022-02-23 17:39:06 +00:00
|
|
|
[&]( wxMouseEvent& event )
|
2020-07-11 17:42:00 +00:00
|
|
|
{
|
2022-02-23 17:39:06 +00:00
|
|
|
wxLongLong now = wxGetLocalTimeMillis();
|
|
|
|
|
2023-08-11 17:40:13 +00:00
|
|
|
if( now - m_debounce < 200 )
|
2022-02-23 17:39:06 +00:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
m_debounce = now;
|
|
|
|
|
2020-07-11 17:42:00 +00:00
|
|
|
SetValue( !GetValue() );
|
2022-02-23 17:39:06 +00:00
|
|
|
|
|
|
|
wxCommandEvent command( TOGGLE_CHANGED );
|
|
|
|
command.SetInt( m_checked );
|
|
|
|
command.SetEventObject( this );
|
|
|
|
wxPostEvent( this, command );
|
2020-07-11 17:42:00 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
auto passOnEvent =
|
|
|
|
[&]( wxEvent& aEvent )
|
|
|
|
{
|
|
|
|
wxPostEvent( this, aEvent );
|
|
|
|
};
|
|
|
|
|
|
|
|
m_bitmap->Bind( wxEVT_RIGHT_DOWN, passOnEvent );
|
|
|
|
m_bitmap->Bind( wxEVT_RIGHT_UP, passOnEvent );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BITMAP_TOGGLE::SetValue( bool aValue )
|
|
|
|
{
|
|
|
|
m_checked = aValue;
|
|
|
|
m_bitmap->SetBitmap( aValue ? m_checked_bitmap : m_unchecked_bitmap );
|
|
|
|
}
|