1 /* 2 * This program source code file is part of KiCad, a free EDA CAD application. 3 * 4 * Copyright (C) 2017-2021 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 #ifndef COLOR_SWATCH__H_ 25 #define COLOR_SWATCH__H_ 26 27 #include <wx/bitmap.h> 28 #include <wx/gdicmn.h> 29 #include <wx/panel.h> 30 #include <wx/statbmp.h> 31 #include <wx/window.h> 32 33 #include <functional> 34 35 #include <gal/color4d.h> 36 #include <dialogs/dialog_color_picker.h> 37 38 enum SWATCH_SIZE 39 { 40 SWATCH_SMALL, 41 SWATCH_MEDIUM, 42 SWATCH_LARGE, 43 SWATCH_EXPAND 44 }; 45 46 47 const static wxSize SWATCH_SIZE_SMALL_DU( 8, 6 ); 48 const static wxSize SWATCH_SIZE_MEDIUM_DU( 24, 10 ); 49 const static wxSize SWATCH_SIZE_LARGE_DU( 24, 16 ); 50 const static wxSize CHECKERBOARD_SIZE_DU( 3, 3 ); 51 52 53 /** 54 * A simple color swatch of the kind used to set layer colors. 55 */ 56 class COLOR_SWATCH: public wxPanel 57 { 58 public: 59 60 /** 61 * Construct a COLOR_SWATCH 62 * 63 * @param aParent parent window 64 * @param aColor initial swatch color 65 * @param aID id to use when sending swatch events 66 */ 67 COLOR_SWATCH( wxWindow* aParent, const KIGFX::COLOR4D& aColor, int aID, 68 const KIGFX::COLOR4D& aBackground, const KIGFX::COLOR4D& aDefault, 69 SWATCH_SIZE aSwatchType ); 70 71 /** 72 * constructor for wxFormBuilder 73 */ 74 COLOR_SWATCH( wxWindow *aParent, wxWindowID aId, const wxPoint &aPos = wxDefaultPosition, 75 const wxSize &aSize = wxDefaultSize, long aStyle = 0 ); 76 77 /** 78 * Set the current swatch color directly. 79 */ 80 void SetSwatchColor( const KIGFX::COLOR4D& aColor, bool aSendEvent ); 81 82 /** 83 * Sets the color that will be chosen with the "Reset to Default" button in the chooser 84 */ 85 void SetDefaultColor( const KIGFX::COLOR4D& aColor ); 86 87 /** 88 * Set the swatch background color. 89 */ 90 void SetSwatchBackground( const KIGFX::COLOR4D& aBackground ); 91 92 /** 93 * Fetch a reference to the user colors list. 94 */ SetUserColors(CUSTOM_COLORS_LIST * aUserColors)95 void SetUserColors( CUSTOM_COLORS_LIST* aUserColors ) { m_userColors = aUserColors; } 96 97 /** 98 * @return the current swatch color 99 */ 100 KIGFX::COLOR4D GetSwatchColor() const; 101 102 /** 103 * Updates the window ID of this control and its children 104 * @param aId new Window ID to set 105 */ SetWindowID(wxWindowID aId)106 void SetWindowID( wxWindowID aId ) 107 { 108 SetId( aId ); 109 m_swatch->SetId( aId ); 110 } 111 112 /** 113 * Prompt for a new colour, using the colour picker dialog. 114 * 115 * A colour change event will be sent if it's set. 116 */ 117 void GetNewSwatchColor(); 118 119 void SetReadOnly( bool aReadOnly = true ) { m_readOnly = aReadOnly; } IsReadOnly()120 bool IsReadOnly() const { return m_readOnly; } 121 SetSupportsOpacity(bool aSupportsOpacity)122 void SetSupportsOpacity( bool aSupportsOpacity ) { m_supportsOpacity = aSupportsOpacity; } 123 124 /// Registers a handler for when the user tries to interact with a read-only swatch SetReadOnlyCallback(std::function<void ()> aCallback)125 void SetReadOnlyCallback( std::function<void()> aCallback ) { m_readOnlyCallback = aCallback; } 126 127 static wxBitmap MakeBitmap( const KIGFX::COLOR4D& aColor, const KIGFX::COLOR4D& aBackground, 128 const wxSize& aSize, const wxSize& aCheckerboardSize, 129 const KIGFX::COLOR4D& aCheckerboardBackground ); 130 131 private: 132 void setupEvents(); 133 134 /** 135 * Pass unwanted events on to listeners of this object 136 */ 137 void rePostEvent( wxEvent& aEvent ); 138 139 KIGFX::COLOR4D m_color; 140 KIGFX::COLOR4D m_background; 141 KIGFX::COLOR4D m_default; 142 CUSTOM_COLORS_LIST* m_userColors; 143 144 wxStaticBitmap* m_swatch; 145 146 wxSize m_size; 147 wxSize m_checkerboardSize; 148 KIGFX::COLOR4D m_checkerboardBg; 149 150 /// A read-only swatch won't show the color chooser dialog but otherwise works normally 151 bool m_readOnly; 152 std::function<void()> m_readOnlyCallback; 153 154 /// If opacity is not supported the color chooser dialog will be displayed without it 155 bool m_supportsOpacity; 156 }; 157 158 159 /** 160 * Event signaling a swatch has changed color 161 */ 162 wxDECLARE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent); 163 164 #endif // COLOR_SWATCH__H_ 165