KiCad PCB EDA Suite
grid_color_swatch_helpers.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) 2018-2020 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 
25 
28 #include <wx/checkbox.h>
29 #include <wx/dc.h>
30 
31 
32 //-------- Custom wxGridCellRenderers --------------------------------------------------
33 
34 
36  const KIGFX::COLOR4D& aBackground ) :
37  wxGridCellRenderer(),
38  m_parent( aParent ),
39  m_background( aBackground )
40 {
41  switch( aSize )
42  {
43  case SWATCH_MEDIUM: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU ); break;
44  case SWATCH_SMALL: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_SMALL_DU ); break;
45  case SWATCH_LARGE: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
46  case SWATCH_EXPAND: m_size = wxDefaultSize; break;
47  }
48 
49  m_checkerboardSize = m_parent->ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
50  m_checkerboardBg = m_parent->GetBackgroundColour();
51 }
52 
53 
55 {
56  m_parent = aOther.m_parent;
57  m_background = aOther.m_background;
58  m_size = aOther.m_size;
61 }
62 
63 
65 {
66 }
67 
68 
69 wxGridCellRenderer* GRID_CELL_COLOR_RENDERER::Clone() const
70 {
71  return new GRID_CELL_COLOR_RENDERER( *this );
72 }
73 
74 
75 wxSize GRID_CELL_COLOR_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
76  int row, int col )
77 {
78  if( m_size != wxDefaultSize )
79  return m_size;
80 
81  wxSize bestSize;
82 
83  dc.SetFont( attr.GetFont() );
84  dc.GetTextExtent( "WWW", &bestSize.x, &bestSize.y );
85 
86  return bestSize;
87 }
88 
89 
90 void GRID_CELL_COLOR_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
91  const wxRect& aRect, int aRow, int aCol, bool isSelected )
92 {
93  wxRect rect = aRect;
94 
95  // erase background
96  wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
97 
98  // draw the swatch
99  COLOR4D color( aGrid.GetTable()->GetValue( aRow, aCol ) );
100  wxSize size = ( m_size == wxDefaultSize ) ? aRect.GetSize() : m_size;
103 
104  wxPoint origin = rect.GetTopLeft();
105 
106  if( m_size != wxDefaultSize )
107  {
108  int x = std::max( 0, ( aRect.GetWidth() - m_size.x ) / 2 );
109  int y = std::max( 0, ( aRect.GetHeight() - m_size.y ) / 2 );
110  origin += wxPoint( x, y );
111  }
112 
113  aDC.DrawBitmap( bitmap, origin, true );
114 }
115 
116 
117 
118 //-------- Custom wxGridCellEditors ----------------------------------------------------
119 //
120 // Note: this implementation is an adaptation of wxGridCellBoolEditor
121 
122 
123 GRID_CELL_COLOR_SELECTOR::GRID_CELL_COLOR_SELECTOR( wxWindow* aParent, wxGrid* aGrid ) :
124  m_parent( aParent ),
125  m_grid( aGrid ),
126  m_value( COLOR4D::UNSPECIFIED )
127 {
128 }
129 
130 
131 wxGridCellEditor* GRID_CELL_COLOR_SELECTOR::Clone() const
132 {
134 }
135 
136 
137 void GRID_CELL_COLOR_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
138  wxEvtHandler* aEventHandler )
139 {
140  // wxWidgets needs a control to hold on to the event handler
141  m_control = new wxCheckBox( aParent, wxID_ANY, wxEmptyString );
142 
143  wxGridCellEditor::Create( aParent, aId, aEventHandler );
144 }
145 
146 
148 {
149  return m_value.ToWxString( wxC2S_CSS_SYNTAX );
150 }
151 
152 
153 void GRID_CELL_COLOR_SELECTOR::BeginEdit( int row, int col, wxGrid* grid )
154 {
155  m_value.SetFromWxString( grid->GetTable()->GetValue( row, col ) );
156 
157  DIALOG_COLOR_PICKER dialog( m_parent, m_value, false );
158 
159  if( dialog.ShowModal() == wxID_OK )
160  m_value = dialog.GetColor();
161 
162  m_grid->GetTable()->SetValue( row, col, GetValue() );
163 
164  // That's it; we're all done
165  m_grid->HideCellEditControl();
166  m_grid->ForceRefresh();
167 }
168 
169 
170 bool GRID_CELL_COLOR_SELECTOR::EndEdit( int row, int col, const wxGrid* grid,
171  const wxString& oldval, wxString *newval )
172 {
173  if ( newval )
174  *newval = GetValue();
175 
176  return true;
177 }
178 
179 
180 void GRID_CELL_COLOR_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
181 {
182  aGrid->GetTable()->SetValue( aRow, aCol, GetValue() );
183 }
184 
185 
187 {
188 }
189 
190 
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override
wxGridCellEditor * Clone() const override
static wxBitmap MakeBitmap(const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground)
int color
Definition: DXF_plotter.cpp:57
static const wxSize SWATCH_SIZE_LARGE_DU(24, 16)
wxString GetValue() const override
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
static const wxSize SWATCH_SIZE_MEDIUM_DU(24, 10)
GRID_CELL_COLOR_SELECTOR(wxWindow *aParent, wxGrid *aGrid)
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
GRID_CELL_COLOR_RENDERER(wxWindow *aParent=nullptr, SWATCH_SIZE aSize=SWATCH_EXPAND, const KIGFX::COLOR4D &aBackground=KIGFX::COLOR4D::UNSPECIFIED)
static const wxSize CHECKERBOARD_SIZE_DU(3, 3)
wxGridCellRenderer * Clone() const override
KIGFX::COLOR4D GetColor()
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
static const wxSize SWATCH_SIZE_SMALL_DU(8, 6)
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *newval) override
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
SWATCH_SIZE
Definition: color_swatch.h:38
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103