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