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
25#include <wx/event.h>
26#include <wx/settings.h>
27#include <wx/sizer.h>
28#include <wx/statbmp.h>
29
30INDICATOR_ICON::INDICATOR_ICON( wxWindow* aParent, ICON_PROVIDER& aIconProvider,
31 ICON_ID aInitialIcon, int aID ):
32 wxPanel( aParent, aID ),
33 m_iconProvider( aIconProvider ),
34 m_currentId( aInitialIcon )
35{
36 wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
37 SetSizer( sizer );
38
39 const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
40
41 m_bitmap = new wxStaticBitmap( this, aID, icon, wxDefaultPosition, icon.GetSize() );
42
43 sizer->Add( m_bitmap, 0, 0 );
44
45 auto evtSkipper = [this] ( wxEvent& aEvent )
46 {
47 wxPostEvent( this, aEvent );
48 };
49
50 m_bitmap->Bind( wxEVT_LEFT_DOWN, evtSkipper );
51}
52
53
55{
56 if( aIconId == m_currentId )
57 return;
58
59 m_currentId = aIconId;
60
61 const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
62 m_bitmap->SetBitmap( icon );
63 m_bitmap->SetSize( icon.GetSize() );
64}
65
66
68{
69 return m_currentId;
70}
71
72
73wxImage createBlankImage( int size )
74{
75 wxImage image( size, size );
76
77 image.InitAlpha();
78 for( int y = 0; y < size; ++y )
79 for( int x = 0; x < size; ++x )
80 image.SetAlpha( x, y, wxIMAGE_ALPHA_TRANSPARENT );
81
82#ifdef __WXMSW__
83 // wxWidgets on Windows chokes on an empty fully transparent bitmap and draws it
84 // as a black box
85 image.SetRGB( size / 2, size / 2, 128, 128, 128 );
86 image.SetAlpha( size / 2, size / 2, 10 );
87#endif
88
89 return image;
90}
91
92
93// Create an arrow icon of a particular size, colour and direction. 0 points up, 1 points
94// right, and so forth.
95wxBitmap createArrow( int size, int aDirection, wxColour aColour )
96{
97 wxImage image = createBlankImage( size );
98
99 int startX = size / 2 - 1;
100 int len = 1;
101
102 int startY = aDirection % 2;
103
104 for( int y = startY; y < startY + ( size / 2 ); ++y )
105 {
106 for( int x = startX; x < startX + len; ++x )
107 {
108 image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
109 image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
110 }
111
112 // Next row will start one pixel back and be two pixels longer
113 startX -= 1;
114 len += 2;
115 }
116
117 for( int i = 0; i < aDirection; ++i )
118 image = image.Rotate90();
119
120 return wxBitmap( image );
121}
122
123
124// Create a diamond icon of a particular size and colour.
125wxBitmap createDiamond( int size, wxColour aColour )
126{
127 wxImage image = createBlankImage( size );
128
129 int startX = size / 2 - 1;
130 int len = 1;
131
132 int startY = 2;
133
134 for( int y = startY; y < size && len > 0; ++y )
135 {
136 for( int x = startX; x < startX + len; ++x )
137 {
138 image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
139 image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
140 }
141
142 // Next row will start one pixel back and be two pixels longer
143 if( y < ( size / 2) - 1 )
144 {
145 startX -= 1;
146 len += 2;
147 }
148 else
149 {
150 startX += 1;
151 len -= 2;
152 }
153 }
154
155 return wxBitmap( image );
156}
157
158
160{
161 m_blankBitmap = wxBitmap( createBlankImage( aSize ) );
162 m_rightArrowBitmap = createArrow( aSize, 1, wxColour( 64, 72, 255 ) );
163 m_upArrowBitmap = createArrow( aSize - 2, 0, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
164 m_downArrowBitmap = createArrow( aSize - 2, 2, wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW ) );
165 m_dotBitmap = createDiamond( aSize, wxColour( 128, 144, 255 ) );
166}
167
168
170{
171 switch( aId )
172 {
173 case STATE::UP: return m_upArrowBitmap;
174 case STATE::DOWN: return m_downArrowBitmap;
175 case STATE::ON: return m_rightArrowBitmap;
176 case STATE::DIMMED: return m_dotBitmap;
177 case STATE::OFF: return m_blankBitmap;
178 default: return m_blankBitmap;
179 }
180}
#define ON
A simple object that can provide fixed bitmaps for use as row indicators.
virtual const wxBitmap & GetIndicatorIcon(ICON_ID aIconId) const =0
Get a reference to the row icon in the given mode.
ICON_PROVIDER & m_iconProvider
< An class that delivers icons for the indicator (currently just uses a default implementation).
int ICON_ID
An id that refers to a certain icon state.
INDICATOR_ICON(wxWindow *aParent, ICON_PROVIDER &aIconProvider, ICON_ID aInitialIcon, int aID)
void SetIndicatorState(ICON_ID aIconId)
Set the row indicator to the given state.
wxStaticBitmap * m_bitmap
Is the icon currently "on".
ICON_ID GetIndicatorState() const
const wxBitmap & GetIndicatorIcon(INDICATOR_ICON::ICON_ID aIconId) const override
Get a reference to the row icon in the given mode.
wxBitmap m_rightArrowBitmap
ROW_ICON_PROVIDER(int aSize)
wxBitmap m_upArrowBitmap
wxBitmap m_downArrowBitmap
wxBitmap createDiamond(int size, wxColour aColour)
wxImage createBlankImage(int size)
wxBitmap createArrow(int size, int aDirection, wxColour aColour)