KiCad PCB EDA Suite
bitmap_toggle.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) 2020-2022 KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26
27#include <wx/sizer.h>
28#include <wx/statbmp.h>
29#include <wx/timer.h>
30
31wxDEFINE_EVENT( TOGGLE_CHANGED, wxCommandEvent );
32
33
34BITMAP_TOGGLE::BITMAP_TOGGLE( wxWindow *aParent, wxWindowID aId, const wxBitmap& aCheckedBitmap,
35 const wxBitmap& aUncheckedBitmap, bool aChecked ) :
36 wxPanel( aParent, aId ),
37 m_checked( aChecked ),
38 m_unchecked_bitmap( aUncheckedBitmap ),
39 m_checked_bitmap( aCheckedBitmap ),
40 m_debounce( 0 )
41{
42 wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
43 SetSizer( sizer );
44
45 const wxBitmap& bitmap = aChecked ? m_checked_bitmap : m_unchecked_bitmap;
46
47 m_bitmap = new wxStaticBitmap( this, aId, bitmap, wxDefaultPosition, bitmap.GetSize() );
48
49 sizer->Add( m_bitmap, 0, 0 );
50
51 m_bitmap->Bind( wxEVT_LEFT_UP,
52 [&]( wxMouseEvent& event )
53 {
54 wxLongLong now = wxGetLocalTimeMillis();
55
56 if( now - m_debounce < 50 )
57 return;
58 else
59 m_debounce = now;
60
61 SetValue( !GetValue() );
62
63 wxCommandEvent command( TOGGLE_CHANGED );
64 command.SetInt( m_checked );
65 command.SetEventObject( this );
66 wxPostEvent( this, command );
67 } );
68
69 auto passOnEvent =
70 [&]( wxEvent& aEvent )
71 {
72 wxPostEvent( this, aEvent );
73 };
74
75 m_bitmap->Bind( wxEVT_RIGHT_DOWN, passOnEvent );
76 m_bitmap->Bind( wxEVT_RIGHT_UP, passOnEvent );
77}
78
79
80void BITMAP_TOGGLE::SetValue( bool aValue )
81{
82 m_checked = aValue;
83 m_bitmap->SetBitmap( aValue ? m_checked_bitmap : m_unchecked_bitmap );
84}
wxDEFINE_EVENT(TOGGLE_CHANGED, wxCommandEvent)
wxLongLong m_debounce
Definition: bitmap_toggle.h:74
wxStaticBitmap * m_bitmap
Definition: bitmap_toggle.h:70
wxBitmap m_unchecked_bitmap
Definition: bitmap_toggle.h:71
void SetValue(bool aValue)
Read the checkbox state.
wxBitmap m_checked_bitmap
Definition: bitmap_toggle.h:72
bool GetValue() const
Definition: bitmap_toggle.h:55