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 KiCad Developers, see AUTHORS.txt for contributors.
5  * @author Jon Evans <jon@craftyjon.com>
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 
25 #include <widgets/bitmap_toggle.h>
26 
27 
28 wxDEFINE_EVENT( TOGGLE_CHANGED, wxCommandEvent );
29 
30 
31 BITMAP_TOGGLE::BITMAP_TOGGLE( wxWindow *aParent, wxWindowID aId, const wxBitmap& aCheckedBitmap,
32  const wxBitmap& aUncheckedBitmap, bool aChecked ) :
33  wxPanel( aParent, aId ),
34  m_checked( aChecked ),
35  m_unchecked_bitmap( aUncheckedBitmap ),
36  m_checked_bitmap( aCheckedBitmap )
37 {
38  wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
39  SetSizer( sizer );
40 
41  const wxBitmap& bitmap = aChecked ? m_checked_bitmap : m_unchecked_bitmap;
42 
43  m_bitmap = new wxStaticBitmap( this, aId, bitmap, wxDefaultPosition, bitmap.GetSize() );
44 
45  sizer->Add( m_bitmap, 0, 0 );
46 
47  m_bitmap->Bind( wxEVT_LEFT_UP,
48  [&]( wxMouseEvent& )
49  {
50  SetValue( !GetValue() );
51  wxCommandEvent event( TOGGLE_CHANGED );
52  event.SetInt( m_checked );
53  event.SetEventObject( this );
54  wxPostEvent( this, event );
55  } );
56 
57  auto passOnEvent =
58  [&]( wxEvent& aEvent )
59  {
60  wxPostEvent( this, aEvent );
61  };
62 
63  m_bitmap->Bind( wxEVT_RIGHT_DOWN, passOnEvent );
64  m_bitmap->Bind( wxEVT_RIGHT_UP, passOnEvent );
65 }
66 
67 
68 void BITMAP_TOGGLE::SetValue( bool aValue )
69 {
70  m_checked = aValue;
71  m_bitmap->SetBitmap( aValue ? m_checked_bitmap : m_unchecked_bitmap );
72 }
void SetValue(bool aValue)
Set the checkbox state
wxDEFINE_EVENT(TOGGLE_CHANGED, wxCommandEvent)
wxStaticBitmap * m_bitmap
Definition: bitmap_toggle.h:58
wxBitmap m_checked_bitmap
Bitmap to display in checked state
Definition: bitmap_toggle.h:64
wxBitmap m_unchecked_bitmap
Bitmap to display in unchecked state
Definition: bitmap_toggle.h:61
bool GetValue() const
Read the checkbox state
Definition: bitmap_toggle.h:53