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