KiCad PCB EDA Suite
selcolor.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) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2014 KiCad Developers, see CHANGELOG.TXT for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 /* Dialog for selecting color from the palette of available colors.
27  */
28 
29 
30 #include <fctsys.h>
31 #include <common.h>
32 #include <gal/color4d.h>
33 
34 #include <wx/statline.h>
35 
36 #include <algorithm>
37 
38 using KIGFX::COLOR4D;
39 
40 
41 enum colors_id {
42  ID_COLOR_BLACK = 2000 // colors_id = ID_COLOR_BLACK a ID_COLOR_BLACK + NBCOLORS-1
43 };
44 
45 
46 class CHOOSE_COLOR_DLG : public wxDialog
47 {
48 public:
49  CHOOSE_COLOR_DLG( wxWindow* aParent, COLOR4D aOldColor );
51 
53 
54 private:
55  void init_Dialog();
56  void selColor( wxCommandEvent& event );
57 
59 
60  DECLARE_EVENT_TABLE()
61 };
62 
63 
64 BEGIN_EVENT_TABLE( CHOOSE_COLOR_DLG, wxDialog )
65  EVT_COMMAND_RANGE( ID_COLOR_BLACK, ID_COLOR_BLACK + NBCOLORS,
66  wxEVT_COMMAND_BUTTON_CLICKED,
67  CHOOSE_COLOR_DLG::selColor )
68 END_EVENT_TABLE()
69 
70 
71 COLOR4D DisplayColorFrame( wxWindow* aParent, COLOR4D aOldColor )
72 {
73  CHOOSE_COLOR_DLG dlg( aParent, aOldColor );
74 
75  if( dlg.ShowModal() == wxID_OK )
76  {
77  return dlg.GetSelectedColor();
78  }
79 
80  return COLOR4D::UNSPECIFIED;
81 }
82 
83 
84 CHOOSE_COLOR_DLG::CHOOSE_COLOR_DLG( wxWindow* aParent, COLOR4D aOldColor ) :
85  wxDialog( aParent, wxID_ANY, _( "Colors" ), wxDefaultPosition, wxDefaultSize,
86  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
87 {
89 
90  init_Dialog();
91  // Resize the dialog
92  GetSizer()->SetSizeHints( this );
93 
94  Centre();
95 }
96 
98 {
99  wxFlexGridSizer* FlexColumnBoxSizer = NULL;
100  wxBitmapButton* focusedButton = NULL;
101  const int w = 20, h = 20;
102 
103  wxBoxSizer* OuterBoxSizer = new wxBoxSizer( wxVERTICAL );
104  SetSizer( OuterBoxSizer );
105 
106  wxBoxSizer*MainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
107  OuterBoxSizer->Add( MainBoxSizer, 1, wxGROW | wxLEFT | wxRIGHT | wxTOP, 5 );
108 
109  for( int ii = 0; ii < NBCOLORS; ++ii )
110  {
111  // Provide a separate column for every six buttons (and their
112  // associated text strings), so provide a FlexGrid Sizer with
113  // six rows and two columns.
114  if( ii % 6 == 0 )
115  {
116  FlexColumnBoxSizer = new wxFlexGridSizer( 6, 2, 0, 0 );
117 
118  // Specify that all of the rows can be expanded.
119  for( int kk = 0; kk < 6; kk++ )
120  {
121  FlexColumnBoxSizer->AddGrowableRow( kk );
122  }
123 
124  // Specify that the second column can also be expanded.
125  FlexColumnBoxSizer->AddGrowableCol( 1 );
126 
127  MainBoxSizer->Add( FlexColumnBoxSizer, 1, wxGROW | wxTOP, 5 );
128  }
129 
130  int butt_ID = ID_COLOR_BLACK + ii;
131  wxMemoryDC iconDC;
132  wxBitmap ButtBitmap( w, h );
133  wxBrush brush;
134 
135  iconDC.SelectObject( ButtBitmap );
136 
137  COLOR4D buttcolor = COLOR4D( g_ColorRefs[ii].m_Numcolor );
138 
139  iconDC.SetPen( *wxBLACK_PEN );
140  brush.SetColour( buttcolor.ToColour() );
141  brush.SetStyle( wxBRUSHSTYLE_SOLID );
142 
143  iconDC.SetBrush( brush );
144  iconDC.SetBackground( *wxGREY_BRUSH );
145  iconDC.Clear();
146  iconDC.DrawRoundedRectangle( 0, 0, w, h, (double) h / 3 );
147 
148  wxBitmapButton* bitmapButton = new wxBitmapButton( this, butt_ID, ButtBitmap,
149  wxDefaultPosition, wxSize( w+8, h+6 ) );
150  FlexColumnBoxSizer->Add( bitmapButton, 0,
151  wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL |
152  wxLEFT | wxBOTTOM, 5 );
153 
154  // Set focus to this button if its color matches the
155  // color which had been selected previously (for
156  // whichever layer's color is currently being edited).
157  if( m_color == buttcolor )
158  focusedButton = bitmapButton;
159 
160  EDA_COLOR_T edaColor = ColorFindNearest( buttcolor.ToColour() );
161  wxStaticText* label = new wxStaticText( this, -1, wxGetTranslation( ColorGetName( edaColor ) ),
162  wxDefaultPosition, wxDefaultSize, 0 );
163  FlexColumnBoxSizer->Add( label, 1,
164  wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL |
165  wxLEFT | wxRIGHT | wxBOTTOM, 5 );
166  }
167 
168  // Provide a Cancel button as well, so that this dialog
169  // box can also be canceled by pressing the Esc key
170  // (and also provide a horizontal static line to separate
171  // that button from all of the other buttons).
172 
173  wxStaticLine* sline = new wxStaticLine( this, -1, wxDefaultPosition,
174  wxDefaultSize, wxLI_HORIZONTAL );
175  OuterBoxSizer->Add( sline, 0, wxGROW | wxLEFT | wxRIGHT | wxTOP, 5 );
176 
177  wxStdDialogButtonSizer* stdDialogButtonSizer = new wxStdDialogButtonSizer;
178  OuterBoxSizer->Add( stdDialogButtonSizer, 0, wxGROW | wxALL, 10 );
179 
180  wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ),
181  wxDefaultPosition, wxDefaultSize, 0 );
182  stdDialogButtonSizer->AddButton( cancelButton );
183 
184  stdDialogButtonSizer->Realize();
185 
186  // Set focus to the Cancel button if the currently selected color
187  // does not match any of the colors provided by this dialog box.
188  // (That shouldn't ever happen in practice though.)
189  if( focusedButton )
190  focusedButton->SetFocus();
191  else
192  cancelButton->SetFocus();
193 }
194 
195 
196 void CHOOSE_COLOR_DLG::selColor( wxCommandEvent& event )
197 {
198  int id = event.GetId();
199  m_color = EDA_COLOR_T( id - ID_COLOR_BLACK );
200 
201  // Close the dialog by calling the default dialog handler for a wxID_OK event
202  event.SetId( wxID_OK );
203  event.Skip();
204 }
COLOR4D aOldColor
Definition: selcolor.cpp:72
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
EDA_COLOR_T ColorFindNearest(const wxColour &aColor)
Find the nearest color match.
Definition: colors.cpp:96
COLOR4D DisplayColorFrame(wxWindow *aParent, COLOR4D aOldColor)
COLOR4D GetSelectedColor()
Definition: selcolor.cpp:52
void selColor(wxCommandEvent &event)
Definition: selcolor.cpp:196
CHOOSE_COLOR_DLG(wxWindow *aParent, COLOR4D aOldColor)
Definition: selcolor.cpp:84
EVT_COMMAND_RANGE(ID_COLOR_BLACK, ID_COLOR_BLACK+NBCOLORS, wxEVT_COMMAND_BUTTON_CLICKED, CHOOSE_COLOR_DLG::selColor) COLOR4D DisplayColorFrame(wxWindow *aParent
void init_Dialog()
Definition: selcolor.cpp:97
const wxChar * ColorGetName(EDA_COLOR_T aColor)
Definition: colors.h:185
const StructColors g_ColorRefs[NBCOLORS]
The predefined colors used in KiCad.
Definition: colors.cpp:37
COLOR4D m_color
Definition: selcolor.cpp:58
The common library.
colors_id
Definition: selcolor.cpp:41
Number of colors.
Definition: colors.h:75
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39