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 "color4Dpickerdlg.h"
27 #include <memory>
28 
29 wxDEFINE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent);
30 
31 using KIGFX::COLOR4D;
32 
33 
34 const static int SWATCH_SIZE_X = 14;
35 const static int SWATCH_SIZE_Y = 12;
36 
37 // See selcolor.cpp:
38 extern COLOR4D DisplayColorFrame( wxWindow* aParent, COLOR4D aOldColor );
39 
40 
44 static wxBitmap makeBitmap( COLOR4D aColor )
45 {
46  wxBitmap bitmap( SWATCH_SIZE_X, SWATCH_SIZE_Y );
47  wxBrush brush;
48  wxMemoryDC iconDC;
49 
50  iconDC.SelectObject( bitmap );
51 
52  brush.SetColour( aColor.ToColour() );
53  brush.SetStyle( wxBRUSHSTYLE_SOLID );
54 
55  iconDC.SetBrush( brush );
56 
57  iconDC.DrawRectangle( 0, 0, SWATCH_SIZE_X, SWATCH_SIZE_Y );
58 
59  return bitmap;
60 }
61 
62 
67 static std::unique_ptr<wxStaticBitmap> makeColorSwatch(
68  wxWindow* aParent, COLOR4D aColor, int aID )
69 {
70  // construct a bitmap of the right color and make the swatch from it
71  wxBitmap bitmap = makeBitmap( aColor );
72  auto ret = std::make_unique<wxStaticBitmap>( aParent, aID, bitmap );
73 
74  return ret;
75 }
76 
77 
78 COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, COLOR4D aColor, int aID,
79  bool aArbitraryColors ):
80  wxPanel( aParent, aID ),
81  m_arbitraryColors( aArbitraryColors ),
82  m_color( aColor )
83 {
84  auto sizer = new wxBoxSizer( wxHORIZONTAL );
85  SetSizer( sizer );
86 
87  auto swatch = makeColorSwatch( this, m_color, aID );
88  m_swatch = swatch.release(); // hold a handle
89 
90  sizer->Add( m_swatch, 0, 0 );
91 
92  // forward click to any other listeners, since we don't want them
93  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
94  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
95 
96  // bind the events that trigger the dialog
97  m_swatch->Bind( wxEVT_LEFT_DCLICK, [this] ( wxMouseEvent& aEvt ) {
99  } );
100 
101  m_swatch->Bind( wxEVT_MIDDLE_DOWN, [this] ( wxMouseEvent& aEvt ) {
103  } );
104 }
105 
106 
107 void COLOR_SWATCH::rePostEvent( wxEvent& aEvt )
108 {
109  wxPostEvent( this, aEvt );
110 }
111 
112 
113 static void sendSwatchChangeEvent( COLOR_SWATCH& aSender )
114 {
115  wxCommandEvent changeEvt( COLOR_SWATCH_CHANGED );
116 
117  // use this class as the object (alternative might be to
118  // set a custom event class but that's more work)
119  changeEvt.SetEventObject( &aSender );
120 
121  wxPostEvent( &aSender, changeEvt );
122 }
123 
124 
125 void COLOR_SWATCH::SetSwatchColor( COLOR4D aColor, bool sendEvent )
126 {
127  m_color = aColor;
128 
129  wxBitmap bm = makeBitmap( aColor );
130  m_swatch->SetBitmap( bm );
131 
132  if( sendEvent )
133  {
134  sendSwatchChangeEvent( *this );
135  }
136 }
137 
138 
140 {
141  return m_color;
142 }
143 
144 
146 {
147  COLOR4D newColor = COLOR4D::UNSPECIFIED;
148 
149  if( m_arbitraryColors )
150  {
151  COLOR4D_PICKER_DLG dialog( this, m_color, true );
152 
153  if( dialog.ShowModal() == wxID_OK )
154  newColor = dialog.GetColor();
155  }
156  else
157  newColor = DisplayColorFrame( this, m_color );
158 
159  if( newColor != COLOR4D::UNSPECIFIED )
160  {
161  m_color = newColor;
162 
163  wxBitmap bm = makeBitmap( newColor );
164  m_swatch->SetBitmap( bm );
165 
166  sendSwatchChangeEvent( *this );
167  }
168 }
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 for legacy canvas?
Definition: color_swatch.h:79
void SetSwatchColor(KIGFX::COLOR4D aColor, bool sendEvent)
Set the current swatch color directly.
KIGFX::COLOR4D GetColor()
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:85
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:82
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39