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 
26 #include <bitmaps.h>
27 
28 #include <wx/dcmemory.h>
29 #include <wx/odcombo.h>
30 #include <wx/menuitem.h>
31 
33 
34 
36 {
37  m_layerhotkeys = true;
38 }
39 
40 
42 {
43  m_layerhotkeys = value;
44  return m_layerhotkeys;
45 }
46 
47 
48 void LAYER_SELECTOR::DrawColorSwatch( wxBitmap& aLayerbmp, COLOR4D aBackground, 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 
75 LAYER_BOX_SELECTOR::LAYER_BOX_SELECTOR( wxWindow* parent, wxWindowID id,
76  const wxPoint& pos, const wxSize& size,
77  int n, const wxString choices[] ) :
78  wxBitmapComboBox( parent, id, wxEmptyString, pos, size, n, choices, wxCB_READONLY ),
80 {
81  if( choices != nullptr )
83 
84  GetParent()->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
85  nullptr, this );
86 }
87 
88 
89 LAYER_BOX_SELECTOR::LAYER_BOX_SELECTOR( wxWindow* parent, wxWindowID id,
90  const wxPoint& pos, const wxSize& size,
91  const wxArrayString& choices ) :
92  wxBitmapComboBox( parent, id, wxEmptyString, pos, size, choices, wxCB_READONLY ),
94 {
95  if( !choices.IsEmpty() )
97 
98  GetParent()->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
99  nullptr, this );
100 }
101 
102 
104 {
105  GetParent()->Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
106  nullptr, this );
107 }
108 
109 
111 {
112  return GetSelection();
113 }
114 
115 
117 {
118  if( GetSelection() < 0 )
119  return UNDEFINED_LAYER;
120 
121  return (LAYER_NUM)(intptr_t) GetClientData( GetSelection() );
122 }
123 
124 
126 {
127  int elements = GetCount();
128 
129  for( int i = 0; i < elements; i++ )
130  {
131  if( GetClientData( (unsigned) i ) == (void*)(intptr_t) layer )
132  {
133  if( GetSelection() != i ) // Element (i) is not selected
134  {
135  SetSelection( i );
136  return i;
137  }
138  else
139  return i; //If element already selected; do nothing
140  }
141  }
142 
143  // Not Found
144  SetSelection( -1 );
145  return -1;
146 }
147 
148 
150 {
151  int elements = GetCount();
152 
153  for( LAYER_NUM i = 0; i < elements; ++i )
154  {
155  wxBitmap layerbmp( 14, 14 );
157  }
158 }
159 
160 
161 void LAYER_BOX_SELECTOR::onKeyDown( wxKeyEvent& aEvent )
162 {
163 #ifdef __WXOSX_MAC__
164  if( aEvent.GetKeyCode() == WXK_ESCAPE && IsPopupShown() )
165  {
166  Dismiss();
167  return;
168  }
169 #endif
170 
171  aEvent.Skip();
172 }
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:305
virtual COLOR4D getLayerColor(LAYER_NUM aLayer) const =0
LAYER_NUM GetLayerSelection() const
Base class to build a layer list.
int SetLayerSelection(LAYER_NUM layer)
bool SetLayersHotkeys(bool value)
int LAYER_NUM
This can be replaced with int and removed.
static void DrawColorSwatch(wxBitmap &aLayerbmp, COLOR4D aBackground, COLOR4D aColor)
Board layer functions and definitions.
LAYER_BOX_SELECTOR(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, int n=0, const wxString choices[]=nullptr)
void onKeyDown(wxKeyEvent &aEvent)
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103