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.

367 lines
10 KiB

3 years ago
3 years ago
  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 (C) 2020-2022 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. #include <kiplatform/ui.h>
  25. #include <widgets/bitmap_button.h>
  26. #include <wx/button.h>
  27. #include <wx/dcclient.h>
  28. #include <wx/renderer.h>
  29. #include <wx/settings.h>
  30. #define wxCONTROL_SEPARATOR wxCONTROL_SPECIAL
  31. BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
  32. const wxSize& aSize, int aStyles ) :
  33. wxPanel( aParent, aId, aPos, aSize, aStyles ),
  34. m_isRadioButton( false ),
  35. m_showBadge( false ),
  36. m_badgeColor( wxColor( 210, 0, 0 ) ), // dark red
  37. m_badgeTextColor( wxColor( wxT( "white" ) ) ),
  38. m_buttonState( 0 ),
  39. m_padding( 0 ),
  40. m_acceptDraggedInClicks( false )
  41. {
  42. if( aSize == wxDefaultSize )
  43. SetMinSize( wxButton::GetDefaultSize() + wxSize( m_padding * 2, m_padding * 2) );
  44. m_badgeFont = GetFont().Smaller().MakeBold();
  45. setupEvents();
  46. }
  47. BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxBitmap& aDummyBitmap,
  48. const wxPoint& aPos, const wxSize& aSize, int aStyles ) :
  49. wxPanel( aParent, aId, aPos, aSize, aStyles ),
  50. m_isRadioButton( false ),
  51. m_showBadge( false ),
  52. m_buttonState( 0 ),
  53. m_padding( 5 ),
  54. m_acceptDraggedInClicks( false )
  55. {
  56. if( aSize == wxDefaultSize )
  57. SetMinSize( wxButton::GetDefaultSize() + wxSize( m_padding * 2, m_padding * 2) );
  58. setupEvents();
  59. }
  60. void BITMAP_BUTTON::setupEvents()
  61. {
  62. Bind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this );
  63. Bind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this );
  64. Bind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this );
  65. Bind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this );
  66. Bind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this );
  67. Bind( wxEVT_KILL_FOCUS, &BITMAP_BUTTON::OnKillFocus, this );
  68. Bind( wxEVT_SET_FOCUS, &BITMAP_BUTTON::OnSetFocus, this );
  69. }
  70. BITMAP_BUTTON::~BITMAP_BUTTON()
  71. {
  72. }
  73. void BITMAP_BUTTON::SetPadding( int aPadding )
  74. {
  75. m_padding = aPadding;
  76. SetMinSize( m_unadjustedMinSize + wxSize( aPadding * 2, aPadding * 2 ) );
  77. }
  78. void BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
  79. {
  80. m_normalBitmap = aBmp;
  81. m_unadjustedMinSize = aBmp.GetPreferredBitmapSizeFor( this );
  82. SetMinSize( wxSize( m_unadjustedMinSize.GetWidth() + ( m_padding * 2 ),
  83. m_unadjustedMinSize.GetHeight() + ( m_padding * 2 ) ) );
  84. }
  85. void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmapBundle& aBmp )
  86. {
  87. m_disabledBitmap = aBmp;
  88. }
  89. void BITMAP_BUTTON::AcceptDragInAsClick( bool aAcceptDragIn )
  90. {
  91. m_acceptDraggedInClicks = aAcceptDragIn;
  92. }
  93. void BITMAP_BUTTON::OnMouseLeave( wxEvent& aEvent )
  94. {
  95. if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED ) )
  96. {
  97. clearFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED );
  98. Refresh();
  99. }
  100. aEvent.Skip();
  101. }
  102. void BITMAP_BUTTON::OnMouseEnter( wxEvent& aEvent )
  103. {
  104. if( !hasFlag( wxCONTROL_CURRENT ) )
  105. {
  106. setFlag( wxCONTROL_CURRENT );
  107. Refresh();
  108. }
  109. aEvent.Skip();
  110. }
  111. void BITMAP_BUTTON::OnKillFocus( wxEvent& aEvent )
  112. {
  113. if( hasFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED ) )
  114. {
  115. clearFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED );
  116. Refresh();
  117. }
  118. aEvent.Skip();
  119. }
  120. void BITMAP_BUTTON::OnSetFocus( wxEvent& aEvent )
  121. {
  122. if( !hasFlag( wxCONTROL_CHECKABLE ) )
  123. {
  124. if( !hasFlag( wxCONTROL_FOCUSED ) )
  125. {
  126. setFlag( wxCONTROL_FOCUSED );
  127. Refresh();
  128. }
  129. }
  130. aEvent.Skip();
  131. }
  132. void BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
  133. {
  134. // Only create a button event when the control is enabled
  135. // and only accept clicks that came without prior mouse-down if configured
  136. if( !hasFlag( wxCONTROL_DISABLED )
  137. && ( m_acceptDraggedInClicks || hasFlag( wxCONTROL_PRESSED | wxCONTROL_FOCUSED ) ) )
  138. {
  139. GetEventHandler()->CallAfter( [=]()
  140. {
  141. wxCommandEvent evt( wxEVT_BUTTON, GetId() );
  142. evt.SetEventObject( this );
  143. GetEventHandler()->ProcessEvent( evt );
  144. } );
  145. }
  146. clearFlag( wxCONTROL_PRESSED );
  147. Refresh();
  148. aEvent.Skip();
  149. }
  150. void BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
  151. {
  152. if( hasFlag( wxCONTROL_CHECKABLE ) )
  153. {
  154. if( hasFlag( wxCONTROL_CHECKED ) && !m_isRadioButton )
  155. {
  156. clearFlag( wxCONTROL_CHECKED );
  157. GetEventHandler()->CallAfter(
  158. [=]()
  159. {
  160. wxCommandEvent evt( wxEVT_BUTTON, GetId() );
  161. evt.SetEventObject( this );
  162. evt.SetInt( 0 );
  163. GetEventHandler()->ProcessEvent( evt );
  164. } );
  165. }
  166. else
  167. {
  168. setFlag( wxCONTROL_CHECKED );
  169. GetEventHandler()->CallAfter(
  170. [=]()
  171. {
  172. wxCommandEvent evt( wxEVT_BUTTON, GetId() );
  173. evt.SetEventObject( this );
  174. evt.SetInt( 1 );
  175. GetEventHandler()->ProcessEvent( evt );
  176. } );
  177. }
  178. }
  179. else
  180. {
  181. setFlag( wxCONTROL_PRESSED );
  182. }
  183. Refresh();
  184. aEvent.Skip();
  185. }
  186. void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent )
  187. {
  188. bool darkMode = KIPLATFORM::UI::IsDarkTheme();
  189. wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
  190. // The drawing rectangle
  191. wxRect rect( wxPoint( 0, 0 ), GetSize() );
  192. wxPaintDC dc( this );
  193. if( hasFlag( wxCONTROL_SEPARATOR ) )
  194. {
  195. dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) ) );
  196. dc.DrawLine( wxPoint( GetSize().x / 2, 0 ), wxPoint( GetSize().x / 2, GetSize().y ) );
  197. return;
  198. }
  199. // This drawing is done so the button looks the same as an AUI toolbar button
  200. if( !hasFlag( wxCONTROL_DISABLED ) )
  201. {
  202. if( hasFlag( wxCONTROL_PRESSED ) )
  203. {
  204. dc.SetPen( wxPen( highlightColor ) );
  205. dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
  206. dc.DrawRectangle( rect );
  207. }
  208. else if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_FOCUSED ) )
  209. {
  210. dc.SetPen( wxPen( highlightColor ) );
  211. dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
  212. // Checked items need a lighter hover rectangle
  213. if( hasFlag( wxCONTROL_CHECKED ) )
  214. dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
  215. dc.DrawRectangle( rect );
  216. }
  217. else if( hasFlag( wxCONTROL_CHECKED ) )
  218. {
  219. dc.SetPen( wxPen( highlightColor ) );
  220. dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
  221. dc.DrawRectangle( rect );
  222. }
  223. }
  224. const wxBitmapBundle& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
  225. // Draw the bitmap with the upper-left corner offset by the padding
  226. if( bmp.IsOk() )
  227. dc.DrawBitmap( bmp.GetBitmapFor( this ), m_padding, m_padding, true );
  228. // Draw the badge
  229. if( m_showBadge )
  230. {
  231. dc.SetFont( m_badgeFont );
  232. wxSize box_size = dc.GetTextExtent( m_badgeText ) + wxSize( 6, 2 );
  233. wxSize box_offset = box_size + wxSize( m_padding - 2, m_padding );
  234. wxSize text_offset = box_offset - wxSize( 3, 1 );
  235. dc.SetPen( wxPen( m_badgeColor ) );
  236. dc.SetBrush( wxBrush( m_badgeColor ) );
  237. dc.DrawRoundedRectangle( rect.GetRightBottom() - box_offset, box_size, -0.25 );
  238. dc.SetTextForeground( m_badgeTextColor );
  239. dc.DrawText( m_badgeText, rect.GetRightBottom() - text_offset );
  240. }
  241. }
  242. bool BITMAP_BUTTON::Enable( bool aEnable )
  243. {
  244. // If the requested state is already the current state, don't do anything
  245. if( aEnable != hasFlag( wxCONTROL_DISABLED ) )
  246. return false;
  247. wxPanel::Enable( aEnable );
  248. if( aEnable && hasFlag( wxCONTROL_DISABLED ) )
  249. {
  250. clearFlag( wxCONTROL_DISABLED );
  251. Refresh();
  252. }
  253. if( !aEnable && !hasFlag( wxCONTROL_DISABLED ) )
  254. {
  255. setFlag( wxCONTROL_DISABLED );
  256. Refresh();
  257. }
  258. return true;
  259. }
  260. void BITMAP_BUTTON::SetIsCheckButton()
  261. {
  262. setFlag( wxCONTROL_CHECKABLE );
  263. }
  264. void BITMAP_BUTTON::SetIsRadioButton()
  265. {
  266. setFlag( wxCONTROL_CHECKABLE );
  267. m_isRadioButton = true;
  268. }
  269. void BITMAP_BUTTON::SetIsSeparator()
  270. {
  271. setFlag( wxCONTROL_SEPARATOR | wxCONTROL_DISABLED );
  272. SetMinSize( wxSize( m_padding * 2, wxButton::GetDefaultSize().y ) );
  273. }
  274. void BITMAP_BUTTON::Check( bool aCheck )
  275. {
  276. wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), wxS( "Button is not a checkButton." ) );
  277. if( aCheck && !hasFlag( wxCONTROL_CHECKED ) )
  278. {
  279. setFlag( wxCONTROL_CHECKED );
  280. Refresh();
  281. }
  282. if( !aCheck && hasFlag( wxCONTROL_CHECKED ) )
  283. {
  284. clearFlag( wxCONTROL_CHECKED );
  285. Refresh();
  286. }
  287. }
  288. bool BITMAP_BUTTON::IsChecked() const
  289. {
  290. wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), wxS( "Button is not a checkButton." ) );
  291. return hasFlag( wxCONTROL_CHECKED );
  292. }