KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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,
35 const wxBitmapBundle& aCheckedBitmap,
36 const wxBitmapBundle& aUncheckedBitmap, bool aChecked ) :
37 wxPanel( aParent, aId ),
38 m_checked( aChecked ),
39 m_unchecked_bitmap( aUncheckedBitmap ),
40 m_checked_bitmap( aCheckedBitmap ),
41 m_debounce( 0 )
42{
43 wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
44 SetSizer( sizer );
45
46 const wxBitmapBundle& bundle = aChecked ? m_checked_bitmap : m_unchecked_bitmap;
47
48 m_bitmap = new wxStaticBitmap( this, aId, bundle, wxDefaultPosition );
49
50 sizer->Add( m_bitmap, 0, 0 );
51
52 m_bitmap->Bind( wxEVT_LEFT_UP,
53 [&]( wxMouseEvent& event )
54 {
55 wxLongLong now = wxGetLocalTimeMillis();
56
57 if( now - m_debounce < 200 )
58 return;
59 else
60 m_debounce = now;
61
62 SetValue( !GetValue() );
63
64 wxCommandEvent command( TOGGLE_CHANGED );
65 command.SetInt( m_checked );
66 command.SetEventObject( this );
67 wxPostEvent( this, command );
68 } );
69
70 auto passOnEvent =
71 [&]( wxEvent& aEvent )
72 {
73 wxPostEvent( this, aEvent );
74 };
75
76 m_bitmap->Bind( wxEVT_RIGHT_DOWN, passOnEvent );
77 m_bitmap->Bind( wxEVT_RIGHT_UP, passOnEvent );
78}
79
80
81void BITMAP_TOGGLE::SetValue( bool aValue )
82{
83 m_checked = aValue;
84 m_bitmap->SetBitmap( aValue ? m_checked_bitmap : m_unchecked_bitmap );
85}
wxDEFINE_EVENT(TOGGLE_CHANGED, wxCommandEvent)
wxLongLong m_debounce
Definition: bitmap_toggle.h:75
wxStaticBitmap * m_bitmap
Definition: bitmap_toggle.h:71
wxBitmapBundle m_unchecked_bitmap
Definition: bitmap_toggle.h:72
void SetValue(bool aValue)
Read the checkbox state.
wxBitmapBundle m_checked_bitmap
Definition: bitmap_toggle.h:73
bool GetValue() const
Definition: bitmap_toggle.h:56