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.

177 lines
5.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. * Copyright (C) 2017-2018 KiCad Developers, see AUTHORS.txt for contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, you may find one here:
  17. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  18. * or you may search the http://www.gnu.org website for the version 2 license,
  19. * or you may write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include <widgets/indicator_icon.h>
  23. INDICATOR_ICON::INDICATOR_ICON( wxWindow* aParent, ICON_PROVIDER& aIconProvider,
  24. ICON_ID aInitialIcon, int aID ):
  25. wxPanel( aParent, aID ),
  26. m_iconProvider( aIconProvider ),
  27. m_currentId( aInitialIcon )
  28. {
  29. auto sizer = new wxBoxSizer( wxHORIZONTAL );
  30. SetSizer( sizer );
  31. const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
  32. m_bitmap = new wxStaticBitmap( this, aID, icon, wxDefaultPosition, icon.GetSize() );
  33. sizer->Add( m_bitmap, 0, 0 );
  34. auto evtSkipper = [this] ( wxEvent& aEvent )
  35. {
  36. wxPostEvent( this, aEvent );
  37. };
  38. m_bitmap->Bind( wxEVT_LEFT_DOWN, evtSkipper );
  39. }
  40. void INDICATOR_ICON::SetIndicatorState( ICON_ID aIconId )
  41. {
  42. if( aIconId == m_currentId )
  43. return;
  44. m_currentId = aIconId;
  45. const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
  46. m_bitmap->SetBitmap( icon );
  47. m_bitmap->SetSize( icon.GetSize() );
  48. }
  49. INDICATOR_ICON::ICON_ID INDICATOR_ICON::GetIndicatorState() const
  50. {
  51. return m_currentId;
  52. }
  53. wxImage createBlankImage( int size )
  54. {
  55. wxImage image( size, size );
  56. image.InitAlpha();
  57. for( int y = 0; y < size; ++y )
  58. for( int x = 0; x < size; ++x )
  59. image.SetAlpha( x, y, wxIMAGE_ALPHA_TRANSPARENT );
  60. #ifdef __WXMSW__
  61. // wxWidgets on Windows chokes on an empty fully transparent bitmap and draws it
  62. // as a black box
  63. image.SetRGB( size / 2, size / 2, 128, 128, 128 );
  64. image.SetAlpha( size / 2, size / 2, 10 );
  65. #endif
  66. return image;
  67. }
  68. // Create an arrow icon of a particular size, colour and direction. 0 points up, 1 points
  69. // right, and so forth.
  70. wxBitmap createArrow( int size, int aDirection, wxColour aColour )
  71. {
  72. wxImage image = createBlankImage( size );
  73. int startX = size / 2 - 1;
  74. int len = 1;
  75. int startY = aDirection % 2;
  76. for( int y = startY; y < startY + ( size / 2 ); ++y )
  77. {
  78. for( int x = startX; x < startX + len; ++x )
  79. {
  80. image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
  81. image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
  82. }
  83. // Next row will start one pixel back and be two pixels longer
  84. startX -= 1;
  85. len += 2;
  86. }
  87. for( int i = 0; i < aDirection; ++i )
  88. image = image.Rotate90();
  89. return wxBitmap( image );
  90. }
  91. // Create a diamond icon of a particular size and colour.
  92. wxBitmap createDiamond( int size, wxColour aColour )
  93. {
  94. wxImage image = createBlankImage( size );
  95. int startX = size / 2 - 1;
  96. int len = 1;
  97. int startY = 2;
  98. for( int y = startY; y < size && len > 0; ++y )
  99. {
  100. for( int x = startX; x < startX + len; ++x )
  101. {
  102. image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
  103. image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
  104. }
  105. // Next row will start one pixel back and be two pixels longer
  106. if( y < ( size / 2) - 1 )
  107. {
  108. startX -= 1;
  109. len += 2;
  110. }
  111. else
  112. {
  113. startX += 1;
  114. len -= 2;
  115. }
  116. }
  117. return wxBitmap( image );
  118. }
  119. ROW_ICON_PROVIDER::ROW_ICON_PROVIDER( int aSize )
  120. {
  121. m_blankBitmap = wxBitmap( createBlankImage( aSize ) );
  122. m_rightArrowBitmap = createArrow( aSize, 1, wxColour( 64, 72, 255 ) );
  123. m_upArrowBitmap = createArrow( aSize - 2, 0, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
  124. m_downArrowBitmap = createArrow( aSize - 2, 2, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
  125. m_dotBitmap = createDiamond( aSize, wxColour( 128, 144, 255 ) );
  126. }
  127. const wxBitmap& ROW_ICON_PROVIDER::GetIndicatorIcon( INDICATOR_ICON::ICON_ID aId ) const
  128. {
  129. switch( aId )
  130. {
  131. case STATE::UP: return m_upArrowBitmap;
  132. case STATE::DOWN: return m_downArrowBitmap;
  133. case STATE::ON: return m_rightArrowBitmap;
  134. case STATE::DIMMED: return m_dotBitmap;
  135. case STATE::OFF: return m_blankBitmap;
  136. default: return m_blankBitmap;
  137. }
  138. }