KiCad PCB EDA Suite
indicator_icon.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3 
4  * Copyright (C) 2017-2018 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <widgets/indicator_icon.h>
25 
26 
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  auto sizer = new wxBoxSizer( wxHORIZONTAL );
34  SetSizer( sizer );
35 
36  const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
37 
38  m_bitmap = new wxStaticBitmap( this, aID, icon, wxDefaultPosition, icon.GetSize() );
39 
40  sizer->Add( m_bitmap, 0, 0 );
41 
42  auto evtSkipper = [this] ( wxEvent& aEvent ) {
43  wxPostEvent( this, aEvent );
44  };
45 
46  m_bitmap->Bind( wxEVT_LEFT_DOWN, evtSkipper );
47 }
48 
49 
51 {
52  if( aIconId == m_currentId )
53  return;
54 
55  m_currentId = aIconId;
56 
57  const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
58  m_bitmap->SetBitmap( icon );
59  m_bitmap->SetSize( icon.GetSize() );
60 }
61 
62 
64 {
65  return m_currentId;
66 }
67 
68 
69 wxImage createBlankImage( int size )
70 {
71  wxImage image( size, size );
72 
73  image.InitAlpha();
74  for( int y = 0; y < size; ++y )
75  for( int x = 0; x < size; ++x )
76  image.SetAlpha( x, y, wxIMAGE_ALPHA_TRANSPARENT );
77 
78 #ifdef __WXWINDOWS__
79  // wxWidgets on Windows chokes on an empty fully transparent bitmap and draws it
80  // as a black box
81  image.SetRGB( size / 2, size / 2, 128, 128, 128 );
82  image.SetAlpha( size / 2, size / 2, 10 );
83 #endif
84 
85  return image;
86 }
87 
88 
89 // Create an arrow icon of a particular size, colour and direction. 0 points up, 1 points
90 // right, and so forth.
91 wxBitmap createArrow( int size, int aDirection, wxColour aColour )
92 {
93  wxImage image = createBlankImage( size );
94 
95  int startX = size / 2 - 1;
96  int len = 1;
97 
98  int startY = aDirection % 2;
99 
100  for( int y = startY; y < startY + ( size / 2 ); ++y )
101  {
102  for( int x = startX; x < startX + len; ++x )
103  {
104  image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
105  image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
106  }
107 
108  // Next row will start one pixel back and be two pixels longer
109  startX -= 1;
110  len += 2;
111  }
112 
113  for( int i = 0; i < aDirection; ++i )
114  image = image.Rotate90();
115 
116  return wxBitmap( image );
117 }
118 
119 
120 // Create a diamond icon of a particular size and colour.
121 wxBitmap createDiamond( int size, wxColour aColour )
122 {
123  wxImage image = createBlankImage( size );
124 
125  int startX = size / 2 - 1;
126  int len = 1;
127 
128  int startY = 2;
129 
130  for( int y = startY; y < size && len > 0; ++y )
131  {
132  for( int x = startX; x < startX + len; ++x )
133  {
134  image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
135  image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
136  }
137 
138  // Next row will start one pixel back and be two pixels longer
139  if( y < ( size / 2) - 1 )
140  {
141  startX -= 1;
142  len += 2;
143  }
144  else
145  {
146  startX += 1;
147  len -= 2;
148  }
149  }
150 
151  return wxBitmap( image );
152 }
153 
154 
156 {
157  m_blankBitmap = wxBitmap( createBlankImage( aSize ) );
158  m_rightArrowBitmap = createArrow( aSize, 1, wxColour( 64, 72, 255 ) );
159  m_upArrowBitmap = createArrow( aSize - 2, 0, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
160  m_downArrowBitmap = createArrow( aSize - 2, 2, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
161  m_dotBitmap = createDiamond( aSize, wxColour( 128, 144, 255 ) );
162 }
163 
164 
166 {
167  switch( aId )
168  {
169  case STATE::UP:
170  return m_upArrowBitmap;
171  case STATE::DOWN:
172  return m_downArrowBitmap;
173  case STATE::ON:
174  return m_rightArrowBitmap;
175  case STATE::DIMMED:
176  return m_dotBitmap;
177  case STATE::OFF:
178  default:
179  return m_blankBitmap;
180  }
181 }
wxBitmap createArrow(int size, int aDirection, wxColour aColour)
void SetIndicatorState(ICON_ID aIconId)
Sets the row indiciator to the given state.
virtual const wxBitmap & GetIndicatorIcon(ICON_ID aIconId) const =0
Gets a reference to the row icon in the given mode.
wxStaticBitmap * m_bitmap
Handle on the bitmap widget
int ICON_ID
An id that refers to a certain icon state.
INDICATOR_ICON(wxWindow *aParent, ICON_PROVIDER &aIconProvider, ICON_ID aInitialIcon, int aID)
#define OFF
ICON_ID GetIndicatorState() const
ICON_PROVIDER & m_iconProvider
An class that delivers icons for the indictor (currently just uses a default implementation).
const wxBitmap & GetIndicatorIcon(INDICATOR_ICON::ICON_ID aIconId) const override
>
A simple object that can provide fixed bitmaps for use as row indicators.
ROW_ICON_PROVIDER(int aSize)
ICON_ID m_currentId
Is the icon currently "on"
size_t i
Definition: json11.cpp:597
wxBitmap createDiamond(int size, wxColour aColour)
wxImage createBlankImage(int size)
#define ON