KiCad PCB EDA Suite
Loading...
Searching...
No Matches
color_swatch.h
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 The 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#pragma once
25
26#include <wx/bitmap.h>
27#include <wx/gdicmn.h>
28#include <wx/panel.h>
29#include <wx/statbmp.h>
30#include <wx/window.h>
31
32#include <functional>
33
34#include <gal/color4d.h>
36
44
45
46const static wxSize SWATCH_SIZE_SMALL_DU( 8, 6 );
47const static wxSize SWATCH_SIZE_MEDIUM_DU( 24, 10 );
48const static wxSize SWATCH_SIZE_LARGE_DU( 24, 16 );
49const static wxSize CHECKERBOARD_SIZE_DU( 3, 3 );
50
51
55class COLOR_SWATCH: public wxPanel
56{
57public:
58
66 COLOR_SWATCH( wxWindow* aParent, const KIGFX::COLOR4D& aColor, int aID,
67 const KIGFX::COLOR4D& aBackground, const KIGFX::COLOR4D& aDefault,
68 SWATCH_SIZE aSwatchType, bool aTriggerWithSingleClick = false );
69
73 COLOR_SWATCH( wxWindow *aParent, wxWindowID aId, const wxPoint &aPos = wxDefaultPosition,
74 const wxSize &aSize = wxDefaultSize, long aStyle = 0 );
75
79 void SetSwatchColor( const KIGFX::COLOR4D& aColor, bool aSendEvent );
80
84 void SetDefaultColor( const KIGFX::COLOR4D& aColor );
85
89 void SetSwatchBackground( const KIGFX::COLOR4D& aBackground );
90
95
101 void SetWindowID( wxWindowID aId )
102 {
103 SetId( aId );
104 m_swatch->SetId( aId );
105 }
106
112 void GetNewSwatchColor();
113
114 void SetReadOnly( bool aReadOnly = true ) { m_readOnly = aReadOnly; }
115 bool IsReadOnly() const { return m_readOnly; }
116
117 void SetSupportsOpacity( bool aSupportsOpacity ) { m_supportsOpacity = aSupportsOpacity; }
118
120 void SetReadOnlyCallback( std::function<void()> aCallback ) { m_readOnlyCallback = aCallback; }
121
123 void OnDarkModeToggle();
124
125 static wxBitmap MakeBitmap( const KIGFX::COLOR4D& aColor, const KIGFX::COLOR4D& aBackground,
126 const wxSize& aSize, const wxSize& aCheckerboardSize,
127 const KIGFX::COLOR4D& aCheckerboardBackground,
128 const std::vector<int>& aMargins = { 0, 0, 0, 0 } );
129
130 static void RenderToDC( wxDC* aDC, const KIGFX::COLOR4D& aColor, const KIGFX::COLOR4D& aBackground,
131 const wxRect& aRect, const wxSize& aCheckerboardSize,
132 const KIGFX::COLOR4D& aCheckerboardBackground,
133 const std::vector<int>& aMargins = { 0, 0, 0, 0 } );
134
135private:
136 void setupEvents( bool aTriggerWithSingleClick );
137
138 wxBitmap makeBitmap();
139
143 void rePostEvent( wxEvent& aEvent );
144
149
150 wxStaticBitmap* m_swatch;
151
152 wxSize m_size;
155
158 std::function<void()> m_readOnlyCallback;
159
162};
163
164
168wxDECLARE_EVENT( COLOR_SWATCH_CHANGED, wxCommandEvent );
void SetSwatchColor(const KIGFX::COLOR4D &aColor, bool aSendEvent)
Set the current swatch color directly.
wxSize m_checkerboardSize
KIGFX::COLOR4D m_checkerboardBg
KIGFX::COLOR4D m_default
bool m_supportsOpacity
If opacity is not supported the color chooser dialog will be displayed without it.
wxStaticBitmap * m_swatch
static void RenderToDC(wxDC *aDC, const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxRect &aRect, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground, const std::vector< int > &aMargins={ 0, 0, 0, 0 })
void OnDarkModeToggle()
Respond to a change in the OS's DarkMode setting.
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.
static wxBitmap MakeBitmap(const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground, const std::vector< int > &aMargins={ 0, 0, 0, 0 })
void SetSupportsOpacity(bool aSupportsOpacity)
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
KIGFX::COLOR4D GetSwatchColor() const
wxBitmap makeBitmap()
void SetReadOnlyCallback(std::function< void()> aCallback)
Register a handler for when the user tries to interact with a read-only swatch.
bool IsReadOnly() const
COLOR_SWATCH(wxWindow *aParent, const KIGFX::COLOR4D &aColor, int aID, const KIGFX::COLOR4D &aBackground, const KIGFX::COLOR4D &aDefault, SWATCH_SIZE aSwatchType, bool aTriggerWithSingleClick=false)
Construct a COLOR_SWATCH.
KIGFX::COLOR4D m_color
KIGFX::COLOR4D m_background
void setupEvents(bool aTriggerWithSingleClick)
void SetReadOnly(bool aReadOnly=true)
std::function< void()> m_readOnlyCallback
void SetDefaultColor(const KIGFX::COLOR4D &aColor)
Sets the color that will be chosen with the "Reset to Default" button in the chooser.
void SetSwatchBackground(const KIGFX::COLOR4D &aBackground)
Set the swatch background color.
CUSTOM_COLORS_LIST * m_userColors
void rePostEvent(wxEvent &aEvent)
Pass unwanted events on to listeners of this object.
void SetWindowID(wxWindowID aId)
Update the window ID of this control and its children.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
static const wxSize SWATCH_SIZE_LARGE_DU(24, 16)
static const wxSize SWATCH_SIZE_MEDIUM_DU(24, 10)
static const wxSize CHECKERBOARD_SIZE_DU(3, 3)
static const wxSize SWATCH_SIZE_SMALL_DU(8, 6)
SWATCH_SIZE
@ SWATCH_MEDIUM
@ SWATCH_LARGE
@ SWATCH_EXPAND
@ SWATCH_SMALL
wxDECLARE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent)
Event signaling a swatch has changed color.
std::vector< CUSTOM_COLOR_ITEM > CUSTOM_COLORS_LIST