KiCad PCB EDA Suite
color_swatch.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 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 <widgets/color_swatch.h>
25 
26 #include <wx/colour.h>
27 #include <wx/colordlg.h>
28 
29 #include <memory>
30 
31 wxDEFINE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent);
32 
33 using KIGFX::COLOR4D;
34 
35 
36 const static int SWATCH_SIZE_X = 14;
37 const static int SWATCH_SIZE_Y = 12;
38 
39 // See selcolor.cpp:
40 extern COLOR4D DisplayColorFrame( wxWindow* aParent, COLOR4D aOldColor );
41 
42 
46 static wxBitmap makeBitmap( COLOR4D aColor )
47 {
48  wxBitmap bitmap( SWATCH_SIZE_X, SWATCH_SIZE_Y );
49  wxBrush brush;
50  wxMemoryDC iconDC;
51 
52  iconDC.SelectObject( bitmap );
53 
54  brush.SetColour( aColor.ToColour() );
55  brush.SetStyle( wxBRUSHSTYLE_SOLID );
56 
57  iconDC.SetBrush( brush );
58 
59  iconDC.DrawRectangle( 0, 0, SWATCH_SIZE_X, SWATCH_SIZE_Y );
60 
61  return bitmap;
62 }
63 
64 
69 static std::unique_ptr<wxStaticBitmap> makeColorSwatch(
70  wxWindow* aParent, COLOR4D aColor, int aID )
71 {
72  // construct a bitmap of the right color and make the swatch from it
73  wxBitmap bitmap = makeBitmap( aColor );
74  auto ret = std::make_unique<wxStaticBitmap>( aParent, aID, bitmap );
75 
76  return ret;
77 }
78 
79 
80 COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, COLOR4D aColor, int aID,
81  bool aArbitraryColors ):
82  wxPanel( aParent, aID ),
83  m_arbitraryColors( aArbitraryColors ),
84  m_color( aColor )
85 {
86  auto sizer = new wxBoxSizer( wxHORIZONTAL );
87  SetSizer( sizer );
88 
89  auto swatch = makeColorSwatch( this, m_color, aID );
90  m_swatch = swatch.release(); // hold a handle
91 
92  sizer->Add( m_swatch, 0, 0 );
93 
94  // forward click to any other listeners, since we don't want them
95  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
96  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
97 
98  // bind the events that trigger the dialog
99  m_swatch->Bind( wxEVT_LEFT_DCLICK, [this] ( wxMouseEvent& aEvt ) {
101  } );
102 
103  m_swatch->Bind( wxEVT_MIDDLE_DOWN, [this] ( wxMouseEvent& aEvt ) {
105  } );
106 }
107 
108 
109 void COLOR_SWATCH::rePostEvent( wxEvent& aEvt )
110 {
111  wxPostEvent( this, aEvt );
112 }
113 
114 
115 static void sendSwatchChangeEvent( COLOR_SWATCH& aSender )
116 {
117  wxCommandEvent changeEvt( COLOR_SWATCH_CHANGED );
118 
119  // use this class as the object (alternative might be to
120  // set a custom event class but that's more work)
121  changeEvt.SetEventObject( &aSender );
122 
123  wxPostEvent( &aSender, changeEvt );
124 }
125 
126 
127 void COLOR_SWATCH::SetSwatchColor( COLOR4D aColor, bool sendEvent )
128 {
129  m_color = aColor;
130 
131  wxBitmap bm = makeBitmap( aColor );
132  m_swatch->SetBitmap( bm );
133 
134  if( sendEvent )
135  {
136  sendSwatchChangeEvent( *this );
137  }
138 }
139 
140 
142 {
143  return m_color;
144 }
145 
146 
148 {
149  COLOR4D newColor = COLOR4D::UNSPECIFIED;
150 
151  if( m_arbitraryColors )
152  {
153  wxColourData colourData;
154  colourData.SetColour( m_color.ToColour() );
155  wxColourDialog dialog( this, &colourData );
156 
157  if( dialog.ShowModal() == wxID_OK )
158  {
159  newColor = COLOR4D( dialog.GetColourData().GetColour() );
160  }
161  }
162  else
163  {
164  newColor = DisplayColorFrame( this, m_color );
165  }
166 
167  if( newColor != COLOR4D::UNSPECIFIED )
168  {
169  m_color = newColor;
170 
171  wxBitmap bm = makeBitmap( newColor );
172  m_swatch->SetBitmap( bm );
173 
174  sendSwatchChangeEvent( *this );
175  }
176 }
COLOR4D aOldColor
Definition: selcolor.cpp:72
COLOR4D DisplayColorFrame(wxWindow *aParent, COLOR4D aOldColor)
bool m_arbitraryColors
Can the swatch have any color, or only preset ones?
Definition: color_swatch.h:77
void SetSwatchColor(KIGFX::COLOR4D aColor, bool sendEvent)
Set the current swatch color directly.
static const int SWATCH_SIZE_Y
static std::unique_ptr< wxStaticBitmap > makeColorSwatch(wxWindow *aParent, COLOR4D aColor, int aID)
Function makeColorButton creates a wxStaticBitmap and assigns it a solid color and a control ID...
KIGFX::COLOR4D GetSwatchColor() const
void rePostEvent(wxEvent &aEvt)
Pass unwanted events on to listeners of this object.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:83
COLOR_SWATCH(wxWindow *aParent, KIGFX::COLOR4D aColor, int aID, bool aArbitraryColors)
Construct a COLOR_SWATCH.
wxDEFINE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent)
static wxBitmap makeBitmap(COLOR4D aColor)
Make a simple color swatch bitmap.
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
Class representing a simple color swatch, of the kind used to set layer colors.
Definition: color_swatch.h:38
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.
static const int SWATCH_SIZE_X
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:80
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39