KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <kiplatform/ui.h>
26#include <wx/event.h>
27#include <wx/settings.h>
28#include <wx/sizer.h>
29#include <wx/statbmp.h>
30
31INDICATOR_ICON::INDICATOR_ICON( wxWindow* aParent, ICON_PROVIDER& aIconProvider,
32 ICON_ID aInitialIcon, int aID ):
33 wxPanel( aParent, aID ),
34 m_iconProvider( aIconProvider ),
35 m_currentId( aInitialIcon )
36{
37 wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
38 SetSizer( sizer );
39
40 const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
41
42 m_bitmap = new wxStaticBitmap( this, aID, icon, wxDefaultPosition, icon.GetLogicalSize() );
43
44 sizer->Add( m_bitmap, 0, 0 );
45
46 auto evtSkipper = [this] ( wxEvent& aEvent )
47 {
48 wxPostEvent( this, aEvent );
49 };
50
51 m_bitmap->Bind( wxEVT_LEFT_DOWN, evtSkipper );
52}
53
54
56{
57 if( aIconId == m_currentId )
58 return;
59
60 m_currentId = aIconId;
61
62 const wxBitmap& icon = m_iconProvider.GetIndicatorIcon( m_currentId );
63 m_bitmap->SetBitmap( icon );
64 m_bitmap->SetSize( icon.GetLogicalSize() );
65}
66
67
69{
70 return m_currentId;
71}
72
73
74wxImage createBlankImage( int size )
75{
76 wxImage image( size, size );
77
78 image.InitAlpha();
79
80 for( int y = 0; y < size; ++y )
81 {
82 for( int x = 0; x < size; ++x )
83 image.SetAlpha( x, y, wxIMAGE_ALPHA_TRANSPARENT );
84 }
85
86#ifdef __WXMSW__
87 // wxWidgets on Windows chokes on an empty fully transparent bitmap and draws it
88 // as a black box
89 image.SetRGB( size / 2, size / 2, 128, 128, 128 );
90 image.SetAlpha( size / 2, size / 2, 10 );
91#endif
92
93 return image;
94}
95
96
97// Create an arrow icon of a particular size, colour and direction. 0 points up, 1 points
98// right, and so forth.
99wxBitmap createArrow( int size, double aScaleFactor, int aDirection, wxColour aColour )
100{
101 wxImage image = createBlankImage( size );
102
103 int startX = size / 2 - 1;
104 int len = 1;
105
106 int startY = aDirection % 2;
107
108 for( int y = startY; y < startY + ( size / 2 ); ++y )
109 {
110 for( int x = startX; x < startX + len; ++x )
111 {
112 image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
113 image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
114 }
115
116 // Next row will start one pixel back and be two pixels longer
117 startX -= 1;
118 len += 2;
119 }
120
121 for( int i = 0; i < aDirection; ++i )
122 image = image.Rotate90();
123
124 wxBitmap bmp( image );
125 bmp.SetScaleFactor( aScaleFactor );
126 return bmp;
127}
128
129
130// Create a diamond icon of a particular size and colour.
131wxBitmap createDiamond( int size, double aScaleFactor, wxColour aColour )
132{
133 wxImage image = createBlankImage( size );
134
135 int startX = size / 2 - 1;
136 int len = 1;
137
138 int startY = 2;
139
140 for( int y = startY; y < size && len > 0; ++y )
141 {
142 for( int x = startX; x < startX + len; ++x )
143 {
144 image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
145 image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
146 }
147
148 // Next row will start one pixel back and be two pixels longer
149 if( y < ( size / 2) - 1 )
150 {
151 startX -= 1;
152 len += 2;
153 }
154 else
155 {
156 startX += 1;
157 len -= 2;
158 }
159 }
160
161 wxBitmap bmp( image );
162 bmp.SetScaleFactor( aScaleFactor );
163 return bmp;
164}
165
166
167ROW_ICON_PROVIDER::ROW_ICON_PROVIDER( int aSizeDIP, wxWindow* aWindow )
168{
169 auto toPhys =
170 [&]( int dip )
171 {
172 return aWindow->ToPhys( aWindow->FromDIP( dip ) );
173 };
174
175 double scale = aWindow->GetDPIScaleFactor();
176 wxColour shadowColor = wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW );
177
178 m_blankBitmap = wxBitmap( createBlankImage( toPhys( aSizeDIP ) ) );
179 m_blankBitmap.SetScaleFactor( scale );
180
181 m_rightArrowBitmap = createArrow( toPhys( aSizeDIP ), scale, 1, wxColour( 64, 72, 255 ) );
182 m_upArrowBitmap = createArrow( toPhys( aSizeDIP - 2 ), scale, 0, shadowColor );
183 m_downArrowBitmap = createArrow( toPhys( aSizeDIP - 2 ), scale, 2, shadowColor );
184 m_dotBitmap = createDiamond( toPhys( aSizeDIP ), scale, wxColour( 128, 144, 255 ) );
185}
186
187
189{
190 switch( aId )
191 {
192 case STATE::UP: return m_upArrowBitmap;
193 case STATE::DOWN: return m_downArrowBitmap;
194 case STATE::ON: return m_rightArrowBitmap;
195 case STATE::DIMMED: return m_dotBitmap;
196 case STATE::OFF: return m_blankBitmap;
197 default: return m_blankBitmap;
198 }
199}
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
@ UP
Row above design alpha.
@ DIMMED
Row "dimmed".
@ DOWN
Row below design alpha.
@ OFF
Row "off" or "deselected".
@ ON
Row "on" or "selected".
wxBitmap m_upArrowBitmap
ROW_ICON_PROVIDER(int aSizeDIP, wxWindow *aWindow)
wxBitmap m_downArrowBitmap
wxBitmap createArrow(int size, double aScaleFactor, int aDirection, wxColour aColour)
wxBitmap createDiamond(int size, double aScaleFactor, wxColour aColour)
wxImage createBlankImage(int size)
const int scale