You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.5 KiB

  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. #include <widgets/bitmap_toggle.h>
  25. wxDEFINE_EVENT( TOGGLE_CHANGED, wxCommandEvent );
  26. BITMAP_TOGGLE::BITMAP_TOGGLE( wxWindow *aParent, wxWindowID aId, const wxBitmap& aCheckedBitmap,
  27. const wxBitmap& aUncheckedBitmap, bool aChecked ) :
  28. wxPanel( aParent, aId ),
  29. m_checked( aChecked ),
  30. m_unchecked_bitmap( aUncheckedBitmap ),
  31. m_checked_bitmap( aCheckedBitmap )
  32. {
  33. wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
  34. SetSizer( sizer );
  35. const wxBitmap& bitmap = aChecked ? m_checked_bitmap : m_unchecked_bitmap;
  36. m_bitmap = new wxStaticBitmap( this, aId, bitmap, wxDefaultPosition, bitmap.GetSize() );
  37. sizer->Add( m_bitmap, 0, 0 );
  38. m_bitmap->Bind( wxEVT_LEFT_UP,
  39. [&]( wxMouseEvent& )
  40. {
  41. SetValue( !GetValue() );
  42. wxCommandEvent event( TOGGLE_CHANGED );
  43. event.SetInt( m_checked );
  44. event.SetEventObject( this );
  45. wxPostEvent( this, event );
  46. } );
  47. auto passOnEvent =
  48. [&]( wxEvent& aEvent )
  49. {
  50. wxPostEvent( this, aEvent );
  51. };
  52. m_bitmap->Bind( wxEVT_RIGHT_DOWN, passOnEvent );
  53. m_bitmap->Bind( wxEVT_RIGHT_UP, passOnEvent );
  54. }
  55. void BITMAP_TOGGLE::SetValue( bool aValue )
  56. {
  57. m_checked = aValue;
  58. m_bitmap->SetBitmap( aValue ? m_checked_bitmap : m_unchecked_bitmap );
  59. }