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.

180 lines
5.3 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Ian McInerney <ian.s.mcinerney at ieee dot org>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  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. #ifndef BITMAP_BUTTON_H_
  25. #define BITMAP_BUTTON_H_
  26. #include <kicommon.h>
  27. #include <wx/bmpbndl.h>
  28. #include <wx/panel.h>
  29. #include <wx/colour.h>
  30. /**
  31. * A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
  32. * Specifically:
  33. * * It has no border
  34. * * It has a rectangle highlight when the mouse is hovering/pressed
  35. * * It has the ability to be checked/toggled
  36. */
  37. class KICOMMON_API BITMAP_BUTTON : public wxPanel
  38. {
  39. public:
  40. BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos = wxDefaultPosition,
  41. const wxSize& aSize = wxDefaultSize,
  42. int aStyles = wxBORDER_NONE | wxTAB_TRAVERSAL );
  43. // For use with wxFormBuilder on a sub-classed wxBitmapButton
  44. BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxBitmap& aDummyBitmap,
  45. const wxPoint& aPos = wxDefaultPosition, const wxSize& aSize = wxDefaultSize,
  46. int aStyles = wxBORDER_NONE | wxTAB_TRAVERSAL );
  47. ~BITMAP_BUTTON();
  48. /**
  49. * Set the amount of padding present on each side of the bitmap.
  50. *
  51. * @param aPadding is the amount in px of padding for each side.
  52. */
  53. void SetPadding( int aPadding );
  54. /**
  55. * Set the bitmap shown when the button is enabled.
  56. *
  57. * @param aBmp is the enabled bitmap.
  58. */
  59. void SetBitmap( const wxBitmapBundle& aBmp );
  60. /**
  61. * Set the bitmap shown when the button is disabled.
  62. *
  63. * @param aBmp is the disabled bitmap.
  64. */
  65. void SetDisabledBitmap( const wxBitmapBundle& aBmp );
  66. /**
  67. * Enable the button.
  68. */
  69. bool Enable( bool aEnable = true ) override;
  70. /**
  71. * Setup the control as a two-state button (checked or unchecked).
  72. */
  73. void SetIsCheckButton();
  74. void SetIsRadioButton();
  75. /**
  76. * Check the control. This is the equivalent to toggling a toolbar button.
  77. */
  78. void Check( bool aCheck = true );
  79. bool IsChecked() const;
  80. /**
  81. * Render button as a toolbar separator.
  82. *
  83. * Also disables the button. Bitmap, if set, is ignored.
  84. */
  85. void SetIsSeparator();
  86. /**
  87. * Accept mouse-up as click even if mouse-down happened outside of the control
  88. *
  89. * @param aAcceptDragIn is true to allow drag in, false to ignore lone mouse-up events
  90. */
  91. void AcceptDragInAsClick( bool aAcceptDragIn = true );
  92. void SetShowBadge( bool aShowBadge ) { m_showBadge = aShowBadge; }
  93. void SetBadgeText( const wxString& aText ) { m_badgeText = aText; }
  94. void SetBadgeColors( const wxColor& aBadgeColor, const wxColor& aBadgeTextColor )
  95. {
  96. m_badgeColor = aBadgeColor;
  97. m_badgeTextColor = aBadgeTextColor;
  98. }
  99. void SetBitmapCentered( bool aCentered = true )
  100. {
  101. m_centerBitmap = aCentered;
  102. }
  103. void SetIsToolbarButton( bool aIsToolbar = true ) { m_isToolbarButton = aIsToolbar; }
  104. bool IsToolbarButton() const { return m_isToolbarButton; }
  105. protected:
  106. void setupEvents();
  107. void OnMouseLeave( wxEvent& aEvent );
  108. void OnMouseEnter( wxEvent& aEvent );
  109. void OnKillFocus( wxEvent& aEvent );
  110. void OnSetFocus( wxEvent& aEvent );
  111. void OnLeftButtonUp( wxMouseEvent& aEvent );
  112. void OnLeftButtonDown( wxMouseEvent& aEvent );
  113. void OnPaint( wxPaintEvent& aEvent );
  114. void OnDPIChanged( wxDPIChangedEvent& aEvent );
  115. virtual wxSize DoGetBestSize() const override;
  116. void setFlag( int aFlag )
  117. {
  118. m_buttonState |= aFlag;
  119. }
  120. void clearFlag( int aFlag )
  121. {
  122. m_buttonState &= ~aFlag;
  123. }
  124. bool hasFlag( int aFlag ) const
  125. {
  126. return m_buttonState & aFlag;
  127. }
  128. void invalidateBestSize();
  129. private:
  130. wxBitmapBundle m_normalBitmap;
  131. wxBitmapBundle m_disabledBitmap;
  132. bool m_isRadioButton;
  133. bool m_showBadge;
  134. wxString m_badgeText;
  135. wxColor m_badgeColor;
  136. wxColor m_badgeTextColor;
  137. wxFont m_badgeFont;
  138. int m_buttonState;
  139. int m_padding;
  140. wxSize m_unadjustedMinSize;
  141. bool m_isToolbarButton;
  142. /// Accept mouse-up as click even if mouse-down happened outside of the control.
  143. bool m_acceptDraggedInClicks;
  144. /// Draw bitmap centered in the control.
  145. bool m_centerBitmap;
  146. };
  147. #endif /*BITMAP_BUTTON_H_*/