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.2 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. #include <wx/event.h>
  24. #include <wx/settings.h>
  25. #include <wx/sizer.h>
  26. #include <wx/statbmp.h>
  27. INDICATOR_ICON::INDICATOR_ICON( wxWindow* aParent, ICON_PROVIDER& aIconProvider,
  28. ICON_ID aInitialIcon, int aID ):
  29. wxPanel( aParent, aID ),
  30. m_iconProvider( aIconProvider ),
  31. m_currentId( aInitialIcon )
  32. {
  33. wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
  34. SetSizer( sizer );
  35. const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
  36. m_bitmap = new wxStaticBitmap( this, aID, icon, wxDefaultPosition, icon.GetSize() );
  37. sizer->Add( m_bitmap, 0, 0 );
  38. auto evtSkipper = [this] ( wxEvent& aEvent )
  39. {
  40. wxPostEvent( this, aEvent );
  41. };
  42. m_bitmap->Bind( wxEVT_LEFT_DOWN, evtSkipper );
  43. }
  44. void INDICATOR_ICON::SetIndicatorState( ICON_ID aIconId )
  45. {
  46. if( aIconId == m_currentId )
  47. return;
  48. m_currentId = aIconId;
  49. const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
  50. m_bitmap->SetBitmap( icon );
  51. m_bitmap->SetSize( icon.GetSize() );
  52. }
  53. INDICATOR_ICON::ICON_ID INDICATOR_ICON::GetIndicatorState() const
  54. {
  55. return m_currentId;
  56. }
  57. wxImage createBlankImage( int size )
  58. {
  59. wxImage image( size, size );
  60. image.InitAlpha();
  61. for( int y = 0; y < size; ++y )
  62. for( int x = 0; x < size; ++x )
  63. image.SetAlpha( x, y, wxIMAGE_ALPHA_TRANSPARENT );
  64. #ifdef __WXMSW__
  65. // wxWidgets on Windows chokes on an empty fully transparent bitmap and draws it
  66. // as a black box
  67. image.SetRGB( size / 2, size / 2, 128, 128, 128 );
  68. image.SetAlpha( size / 2, size / 2, 10 );
  69. #endif
  70. return image;
  71. }
  72. // Create an arrow icon of a particular size, colour and direction. 0 points up, 1 points
  73. // right, and so forth.
  74. wxBitmap createArrow( int size, int aDirection, wxColour aColour )
  75. {
  76. wxImage image = createBlankImage( size );
  77. int startX = size / 2 - 1;
  78. int len = 1;
  79. int startY = aDirection % 2;
  80. for( int y = startY; y < startY + ( size / 2 ); ++y )
  81. {
  82. for( int x = startX; x < startX + len; ++x )
  83. {
  84. image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
  85. image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
  86. }
  87. // Next row will start one pixel back and be two pixels longer
  88. startX -= 1;
  89. len += 2;
  90. }
  91. for( int i = 0; i < aDirection; ++i )
  92. image = image.Rotate90();
  93. return wxBitmap( image );
  94. }
  95. // Create a diamond icon of a particular size and colour.
  96. wxBitmap createDiamond( int size, wxColour aColour )
  97. {
  98. wxImage image = createBlankImage( size );
  99. int startX = size / 2 - 1;
  100. int len = 1;
  101. int startY = 2;
  102. for( int y = startY; y < size && len > 0; ++y )
  103. {
  104. for( int x = startX; x < startX + len; ++x )
  105. {
  106. image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
  107. image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
  108. }
  109. // Next row will start one pixel back and be two pixels longer
  110. if( y < ( size / 2) - 1 )
  111. {
  112. startX -= 1;
  113. len += 2;
  114. }
  115. else
  116. {
  117. startX += 1;
  118. len -= 2;
  119. }
  120. }
  121. return wxBitmap( image );
  122. }
  123. ROW_ICON_PROVIDER::ROW_ICON_PROVIDER( int aSize )
  124. {
  125. m_blankBitmap = wxBitmap( createBlankImage( aSize ) );
  126. m_rightArrowBitmap = createArrow( aSize, 1, wxColour( 64, 72, 255 ) );
  127. m_upArrowBitmap = createArrow( aSize - 2, 0, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
  128. m_downArrowBitmap = createArrow( aSize - 2, 2, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
  129. m_dotBitmap = createDiamond( aSize, wxColour( 128, 144, 255 ) );
  130. }
  131. const wxBitmap& ROW_ICON_PROVIDER::GetIndicatorIcon( INDICATOR_ICON::ICON_ID aId ) const
  132. {
  133. switch( aId )
  134. {
  135. case STATE::UP: return m_upArrowBitmap;
  136. case STATE::DOWN: return m_downArrowBitmap;
  137. case STATE::ON: return m_rightArrowBitmap;
  138. case STATE::DIMMED: return m_dotBitmap;
  139. case STATE::OFF: return m_blankBitmap;
  140. default: return m_blankBitmap;
  141. }
  142. }