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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <wx/sizer.h>
24#include <wx/statbmp.h>
25#include <wx/timer.h>
26
27wxDEFINE_EVENT( TOGGLE_CHANGED, wxCommandEvent );
28
29
30BITMAP_TOGGLE::BITMAP_TOGGLE( wxWindow *aParent, wxWindowID aId,
31 const wxBitmapBundle& aCheckedBitmap,
32 const wxBitmapBundle& aUncheckedBitmap, bool aChecked ) :
33 wxPanel( aParent, aId ),
34 m_checked( aChecked ),
35 m_unchecked_bitmap( aUncheckedBitmap ),
36 m_checked_bitmap( aCheckedBitmap ),
37 m_debounce( 0 )
38{
39 wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
40 SetSizer( sizer );
41
42 const wxBitmapBundle& bundle = aChecked ? m_checked_bitmap : m_unchecked_bitmap;
43
44 m_bitmap = new wxStaticBitmap( this, aId, bundle, wxDefaultPosition );
45
46 sizer->Add( m_bitmap, 0, 0 );
47
48 m_bitmap->Bind( wxEVT_LEFT_UP,
49 [&]( wxMouseEvent& event )
50 {
51 wxLongLong now = wxGetLocalTimeMillis();
52
53 if( now - m_debounce < 200 )
54 return;
55 else
56 m_debounce = now;
57
58 SetValue( !GetValue() );
59
60 wxCommandEvent command( TOGGLE_CHANGED );
61 command.SetInt( m_checked );
62 command.SetEventObject( this );
63 wxPostEvent( this, command );
64 } );
65
66 auto passOnEvent =
67 [&]( wxEvent& aEvent )
68 {
69 wxPostEvent( this, aEvent );
70 };
71
72 m_bitmap->Bind( wxEVT_RIGHT_DOWN, passOnEvent );
73 m_bitmap->Bind( wxEVT_RIGHT_UP, passOnEvent );
74}
75
76
77void BITMAP_TOGGLE::SetValue( bool aValue )
78{
79 m_checked = aValue;
80 m_bitmap->SetBitmap( aValue ? m_checked_bitmap : m_unchecked_bitmap );
81}
wxDEFINE_EVENT(TOGGLE_CHANGED, wxCommandEvent)
wxLongLong m_debounce
Timestamp for debouncing events.
wxStaticBitmap * m_bitmap
wxBitmapBundle m_unchecked_bitmap
void SetValue(bool aValue)
Set the checkbox state.
wxBitmapBundle m_checked_bitmap
bool GetValue() const
Read the checkbox state.