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-2018 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 wxSize SWATCH_SIZE_DU( 8, 6 );
35 
36 // See selcolor.cpp:
37 extern COLOR4D DisplayColorFrame( wxWindow* aParent, COLOR4D aOldColor );
38 
39 
45 static wxBitmap makeBitmap( COLOR4D aColor, COLOR4D aBackground, wxWindow *aWindow )
46 {
47  wxSize size( aWindow->ConvertDialogToPixels( SWATCH_SIZE_DU ) );
48  wxBitmap bitmap( size );
49  wxBrush brush;
50  wxMemoryDC iconDC;
51 
52  iconDC.SelectObject( bitmap );
53 
54  brush.SetStyle( wxBRUSHSTYLE_SOLID );
55  brush.SetColour( aBackground.WithAlpha(1.0).ToColour() );
56  iconDC.SetBrush( brush );
57  iconDC.DrawRectangle( 0, 0, size.x, size.y );
58 
59  brush.SetColour( aColor.ToColour() );
60  iconDC.SetBrush( brush );
61  iconDC.DrawRectangle( 0, 0, size.x, size.y );
62 
63  return bitmap;
64 }
65 
66 
71 static std::unique_ptr<wxStaticBitmap> makeColorSwatch(
72  wxWindow* aParent, COLOR4D aColor, COLOR4D aBackground, int aID )
73 {
74  wxBitmap bitmap = makeBitmap( aColor, aBackground, aParent );
75  auto ret = std::make_unique<wxStaticBitmap>( aParent, aID, bitmap );
76 
77  return ret;
78 }
79 
80 
81 COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, COLOR4D aColor, int aID,
82  bool aArbitraryColors, COLOR4D aBackground ):
83  wxPanel( aParent, aID ),
84  m_arbitraryColors( aArbitraryColors ),
85  m_color( aColor ),
86  m_background( aBackground )
87 {
88  auto sizer = new wxBoxSizer( wxHORIZONTAL );
89  SetSizer( sizer );
90 
91  auto swatch = makeColorSwatch( this, m_color, m_background, aID );
92  m_swatch = swatch.release(); // hold a handle
93 
94  sizer->Add( m_swatch, 0, 0 );
95 
96  // forward click to any other listeners, since we don't want them
97  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
98  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
99 
100  // bind the events that trigger the dialog
101  m_swatch->Bind( wxEVT_LEFT_DCLICK, [this] ( wxMouseEvent& aEvt ) {
103  } );
104 
105  m_swatch->Bind( wxEVT_MIDDLE_DOWN, [this] ( wxMouseEvent& aEvt ) {
107  } );
108 }
109 
110 
111 void COLOR_SWATCH::rePostEvent( wxEvent& aEvt )
112 {
113  wxPostEvent( this, aEvt );
114 }
115 
116 
117 static void sendSwatchChangeEvent( COLOR_SWATCH& aSender )
118 {
119  wxCommandEvent changeEvt( COLOR_SWATCH_CHANGED );
120 
121  // use this class as the object (alternative might be to
122  // set a custom event class but that's more work)
123  changeEvt.SetEventObject( &aSender );
124 
125  wxPostEvent( &aSender, changeEvt );
126 }
127 
128 
129 void COLOR_SWATCH::SetSwatchColor( COLOR4D aColor, bool sendEvent )
130 {
131  m_color = aColor;
132 
133  wxBitmap bm = makeBitmap( m_color, m_background, GetParent() );
134  m_swatch->SetBitmap( bm );
135 
136  if( sendEvent )
137  {
138  sendSwatchChangeEvent( *this );
139  }
140 }
141 
142 
144 {
145  m_background = aBackground;
146  wxBitmap bm = makeBitmap( m_color, m_background, (wxWindow*) this );
147  m_swatch->SetBitmap( bm );
148 }
149 
150 
152 {
153  return m_color;
154 }
155 
156 
158 {
159  COLOR4D newColor = COLOR4D::UNSPECIFIED;
160 
161  if( m_arbitraryColors )
162  {
163  COLOR4D_PICKER_DLG dialog( this, m_color, true );
164 
165  if( dialog.ShowModal() == wxID_OK )
166  newColor = dialog.GetColor();
167  }
168  else
169  newColor = DisplayColorFrame( this, m_color );
170 
171  if( newColor != COLOR4D::UNSPECIFIED )
172  {
173  m_color = newColor;
174 
175  wxBitmap bm = makeBitmap( newColor, m_background, (wxWindow*) this );
176  m_swatch->SetBitmap( bm );
177 
178  sendSwatchChangeEvent( *this );
179  }
180 }
COLOR4D aOldColor
Definition: selcolor.cpp:72
COLOR4D DisplayColorFrame(wxWindow *aParent, COLOR4D aOldColor)
static wxBitmap makeBitmap(COLOR4D aColor, COLOR4D aBackground, wxWindow *aWindow)
Make a simple color swatch bitmap.
bool m_arbitraryColors
Can the swatch have any color, or only preset ones for legacy canvas?
Definition: color_swatch.h:84
void SetSwatchColor(KIGFX::COLOR4D aColor, bool sendEvent)
Set the current swatch color directly.
KIGFX::COLOR4D GetColor()
void SetSwatchBackground(KIGFX::COLOR4D aBackground)
Set the swatch background color.
KIGFX::COLOR4D GetSwatchColor() const
static const wxSize SWATCH_SIZE_DU(8, 6)
void rePostEvent(wxEvent &aEvt)
Pass unwanted events on to listeners of this object.
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:90
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:237
COLOR_SWATCH(wxWindow *aParent, KIGFX::COLOR4D aColor, int aID, bool aArbitraryColors, KIGFX::COLOR4D aBackground)
Construct a COLOR_SWATCH.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:93
static std::unique_ptr< wxStaticBitmap > makeColorSwatch(wxWindow *aParent, COLOR4D aColor, COLOR4D aBackground, int aID)
Function makeColorButton creates a wxStaticBitmap and assigns it a solid color and a control ID...
wxDEFINE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent)
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.
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:87
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39