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 The 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
68wxImage createBlankImage( int size )
69{
70 wxImage image( size, size );
71
72 image.InitAlpha();
73
74 for( int y = 0; y < size; ++y )
75 {
76 for( int x = 0; x < size; ++x )
77 image.SetAlpha( x, y, wxIMAGE_ALPHA_TRANSPARENT );
78 }
79
80#ifdef __WXMSW__
81 // wxWidgets on Windows chokes on an empty fully transparent bitmap and draws it
82 // as a black box
83 image.SetRGB( size / 2, size / 2, 128, 128, 128 );
84 image.SetAlpha( size / 2, size / 2, 10 );
85#endif
86
87 return image;
88}
89
90
91// Create an arrow icon of a particular size, colour and direction. 0 points up, 1 points
92// right, and so forth.
93wxBitmap createArrow( int size, double aScaleFactor, int aDirection, const wxColour& aColour )
94{
95 wxImage image = createBlankImage( size );
96
97 int startX = ( size - 1 ) / 2;
98 int len = 1;
99
100 int startY = aDirection % 2;
101
102 for( int y = startY; y < startY + ( size / 2 ); ++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
110 // Next row will start one pixel back and be two pixels longer
111 startX -= 1;
112 len += 2;
113 }
114
115 for( int i = 0; i < aDirection; ++i )
116 image = image.Rotate90();
117
118 wxBitmap bmp( image );
119 bmp.SetScaleFactor( aScaleFactor );
120 return bmp;
121}
122
123
124// Create a turndown icon of a particular size, colour and direction. 0 points up,
125// 1 points right, and so forth.
126wxBitmap createTurndown( int size, double aScaleFactor, int aDirection, const wxColour& aColour )
127{
128 wxImage image = createBlankImage( size );
129
130 const unsigned char opacity = 0.70 * wxIMAGE_ALPHA_OPAQUE;
131 const int padding_start = 0 * aScaleFactor + 0.5;
132 const int padding_end = 2 * aScaleFactor + 0.5;
133
134 int startX = ( size - 1 ) / 2;
135 int len = 1;
136
137 int startY = padding_start;
138
139 for( int y = 0; y < size - padding_end; ++y )
140 {
141 for( int x = 0; x < len; ++x )
142 {
143 image.SetRGB( x + startX, y + startY, aColour.Red(), aColour.Green(), aColour.Blue() );
144 image.SetAlpha( x + startX, y + startY, ( y % 2 ) || ( ( x > 0 ) && ( x < len - 1 ) ) ? opacity : opacity / 2);
145 }
146
147 // Next row will start one pixel back and be two pixels longer
148 if( y % 2 )
149 {
150 startX -= 1;
151 len += 2;
152 }
153 }
154
155 for( int i = 0; i < aDirection; ++i )
156 image = image.Rotate90();
157
158 wxBitmap bmp( image );
159 bmp.SetScaleFactor( aScaleFactor );
160 return bmp;
161}
162
163
164// Create a diamond icon of a particular size and colour.
165wxBitmap createDiamond( int size, double aScaleFactor, wxColour aColour )
166{
167 wxImage image = createBlankImage( size );
168
169 int startX = size / 2 - 1;
170 int len = 1;
171
172 int startY = 2;
173
174 for( int y = startY; y < size && len > 0; ++y )
175 {
176 for( int x = startX; x < startX + len; ++x )
177 {
178 image.SetRGB( x, y, aColour.Red(), aColour.Green(), aColour.Blue() );
179 image.SetAlpha( x, y, wxIMAGE_ALPHA_OPAQUE );
180 }
181
182 // Next row will start one pixel back and be two pixels longer
183 if( y < ( size / 2) - 1 )
184 {
185 startX -= 1;
186 len += 2;
187 }
188 else
189 {
190 startX += 1;
191 len -= 2;
192 }
193 }
194
195 wxBitmap bmp( image );
196 bmp.SetScaleFactor( aScaleFactor );
197 return bmp;
198}
199
200
201ROW_ICON_PROVIDER::ROW_ICON_PROVIDER( int aSizeDIP, wxWindow* aWindow )
202{
203 auto toPhys =
204 [&]( int dip )
205 {
206 return aWindow->ToPhys( aWindow->FromDIP( dip ) );
207 };
208
209 double scale = aWindow->GetDPIScaleFactor();
210 wxColour shadowColor = wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW );
211 wxColour textColor = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXTEXT );
212
213 m_blankBitmap = wxBitmap( createBlankImage( toPhys( aSizeDIP ) ) );
214 m_blankBitmap.SetScaleFactor( scale );
215
216 m_rightArrowBitmap = createArrow( toPhys( aSizeDIP ), scale, 1, wxColour( 64, 72, 255 ) );
217 m_upArrowBitmap = createArrow( toPhys( aSizeDIP - 2 ), scale, 0, shadowColor );
218 m_downArrowBitmap = createArrow( toPhys( aSizeDIP - 2 ), scale, 2, shadowColor );
219 m_dotBitmap = createDiamond( toPhys( aSizeDIP ), scale, wxColour( 128, 144, 255 ) );
220 m_closedBitmap = createTurndown( toPhys( aSizeDIP ), scale, 1, textColor );
221 m_openBitmap = createTurndown( toPhys( aSizeDIP ), scale, 2, textColor );
222}
223
224
226{
227 switch( aId )
228 {
229 case STATE::UP: return m_upArrowBitmap;
230 case STATE::DOWN: return m_downArrowBitmap;
231 case STATE::ON: return m_rightArrowBitmap;
232 case STATE::DIMMED: return m_dotBitmap;
233 case STATE::OFF: return m_blankBitmap;
234 case STATE::OPEN: return m_openBitmap;
235 case STATE::CLOSED: return m_closedBitmap;
236 default: return m_blankBitmap;
237 }
238}
A simple object that can provide fixed bitmaps for use as row indicators.
ICON_PROVIDER & m_iconProvider
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.
ICON_ID m_currentId
Handle on the bitmap widget.
wxStaticBitmap * m_bitmap
Object that delivers icons for the indicator.
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.
@ OPEN
Tree control open.
@ CLOSED
Tree control closed.
@ DIMMED
Row "dimmed".
@ DOWN
Row below design alpha.
@ OFF
Row "off" or "deselected".
@ ON
Row "on" or "selected".
ROW_ICON_PROVIDER(int aSizeDIP, wxWindow *aWindow)
wxBitmap createTurndown(int size, double aScaleFactor, int aDirection, const wxColour &aColour)
wxBitmap createArrow(int size, double aScaleFactor, int aDirection, const wxColour &aColour)
wxBitmap createDiamond(int size, double aScaleFactor, wxColour aColour)
wxImage createBlankImage(int size)
const int scale