KiCad PCB EDA Suite
layer_box_selector.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) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2014-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26#include <wx/dcmemory.h>
27#include <wx/odcombo.h>
28#include <wx/menuitem.h>
29
30#include <layer_ids.h>
32
33
35{
36 m_layerhotkeys = true;
37}
38
39
41{
42 m_layerhotkeys = value;
43 return m_layerhotkeys;
44}
45
46
47void LAYER_SELECTOR::DrawColorSwatch( wxBitmap& aLayerbmp, const COLOR4D& aBackground,
48 const COLOR4D& aColor )
49{
50 wxMemoryDC bmpDC;
51 wxBrush brush;
52
53 // Prepare Bitmap
54 bmpDC.SelectObject( aLayerbmp );
55
56 brush.SetStyle( wxBRUSHSTYLE_SOLID );
57
58 if( aBackground != COLOR4D::UNSPECIFIED )
59 {
60 brush.SetColour( aBackground.WithAlpha( 1.0 ).ToColour() );
61 bmpDC.SetBrush( brush );
62 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
63 }
64
65 brush.SetColour( aColor.ToColour() );
66 bmpDC.SetBrush( brush );
67 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
68
69 bmpDC.SetBrush( *wxTRANSPARENT_BRUSH );
70 bmpDC.SetPen( *wxBLACK_PEN );
71 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
72}
73
74
75LAYER_BOX_SELECTOR::LAYER_BOX_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos,
76 const wxSize& size, int n, const wxString choices[] ) :
77 wxBitmapComboBox( parent, id, wxEmptyString, pos, size, n, choices, wxCB_READONLY ),
79{
80 if( choices != nullptr )
82
83 GetParent()->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
84 nullptr, this );
85}
86
87
88LAYER_BOX_SELECTOR::LAYER_BOX_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos,
89 const wxSize& size, const wxArrayString& choices ) :
90 wxBitmapComboBox( parent, id, wxEmptyString, pos, size, choices, wxCB_READONLY ),
92{
93 if( !choices.IsEmpty() )
95
96 GetParent()->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
97 nullptr, this );
98}
99
100
102{
103 GetParent()->Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
104 nullptr, this );
105}
106
107
109{
110 if( GetSelection() < 0 )
111 return UNDEFINED_LAYER;
112
113 return (int)(intptr_t) GetClientData( GetSelection() );
114}
115
116
118{
119 for( int i = 0; i < (int) GetCount(); i++ )
120 {
121 if( GetClientData( (unsigned) i ) == (void*)(intptr_t) layer )
122 {
123 if( GetSelection() != i ) // Element (i) is not selected
124 {
125 SetSelection( i );
126 return i;
127 }
128 else
129 {
130 return i; // If element already selected; do nothing
131 }
132 }
133 }
134
135 // Not Found
136 SetSelection( -1 );
137 return -1;
138}
139
140
142{
143 for( int i = 0; i < (int) GetCount(); ++i )
144 {
145 wxBitmap layerbmp( 14, 14 );
147 }
148}
149
150
151void LAYER_BOX_SELECTOR::onKeyDown( wxKeyEvent& aEvent )
152{
153#ifdef __WXOSX_MAC__
154 if( aEvent.GetKeyCode() == WXK_ESCAPE && IsPopupShown() )
155 {
156 Dismiss();
157 return;
158 }
159#endif
160
161 aEvent.Skip();
162}
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:321
void onKeyDown(wxKeyEvent &aEvent)
LAYER_BOX_SELECTOR(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, int n=0, const wxString choices[]=nullptr)
int SetLayerSelection(int layer)
Base class to build a layer list.
virtual COLOR4D getLayerColor(int aLayer) const =0
static void DrawColorSwatch(wxBitmap &aLayerbmp, const COLOR4D &aBackground, const COLOR4D &aColor)
bool SetLayersHotkeys(bool value)
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition: layer_ids.h:220
@ UNDEFINED_LAYER
Definition: layer_ids.h:60