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 // LEGACY TODO: obsolete once legacy canvas removed
29 
30 
31 #include <fctsys.h>
32 #include <common.h>
33 #include <gal/color4d.h>
34 
35 #include <wx/statline.h>
36 
37 #include <algorithm>
38 
39 using KIGFX::COLOR4D;
40 
41 
42 enum colors_id {
43  ID_COLOR_BLACK = 2000 // colors_id = ID_COLOR_BLACK a ID_COLOR_BLACK + NBCOLORS-1
44 };
45 
46 
47 class CHOOSE_COLOR_DLG : public wxDialog
48 {
49 public:
50  CHOOSE_COLOR_DLG( wxWindow* aParent, COLOR4D aOldColor );
52 
54 
55 private:
56  void init_Dialog();
57  void selColor( wxCommandEvent& event );
58 
60 
61  DECLARE_EVENT_TABLE()
62 };
63 
64 
65 BEGIN_EVENT_TABLE( CHOOSE_COLOR_DLG, wxDialog )
66  EVT_COMMAND_RANGE( ID_COLOR_BLACK, ID_COLOR_BLACK + NBCOLORS,
67  wxEVT_COMMAND_BUTTON_CLICKED,
69 END_EVENT_TABLE()
70 
71 
72 COLOR4D DisplayColorFrame( wxWindow* aParent, COLOR4D aOldColor )
73 {
74  CHOOSE_COLOR_DLG dlg( aParent, aOldColor );
75 
76  if( dlg.ShowModal() == wxID_OK )
77  {
78  return dlg.GetSelectedColor();
79  }
80 
81  return COLOR4D::UNSPECIFIED;
82 }
83 
84 
85 CHOOSE_COLOR_DLG::CHOOSE_COLOR_DLG( wxWindow* aParent, COLOR4D aOldColor ) :
86  wxDialog( aParent, wxID_ANY, _( "Colors" ), wxDefaultPosition, wxDefaultSize,
87  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
88 {
90 
91  init_Dialog();
92  // Resize the dialog
93  GetSizer()->SetSizeHints( this );
94 
95  Centre();
96 }
97 
99 {
100  wxFlexGridSizer* FlexColumnBoxSizer = NULL;
101  wxBitmapButton* focusedButton = NULL;
102  const int w = 20, h = 20;
103 
104  wxBoxSizer* OuterBoxSizer = new wxBoxSizer( wxVERTICAL );
105  SetSizer( OuterBoxSizer );
106 
107  wxBoxSizer*MainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
108  OuterBoxSizer->Add( MainBoxSizer, 1, wxGROW | wxLEFT | wxRIGHT | wxTOP, 5 );
109 
110  for( int ii = 0; ii < NBCOLORS; ++ii )
111  {
112  // Provide a separate column for every six buttons (and their
113  // associated text strings), so provide a FlexGrid Sizer with
114  // six rows and two columns.
115  if( ii % 6 == 0 )
116  {
117  FlexColumnBoxSizer = new wxFlexGridSizer( 6, 2, 0, 0 );
118 
119  // Specify that all of the rows can be expanded.
120  for( int kk = 0; kk < 6; kk++ )
121  {
122  FlexColumnBoxSizer->AddGrowableRow( kk );
123  }
124 
125  // Specify that the second column can also be expanded.
126  FlexColumnBoxSizer->AddGrowableCol( 1 );
127 
128  MainBoxSizer->Add( FlexColumnBoxSizer, 1, wxGROW | wxTOP, 5 );
129  }
130 
131  int butt_ID = ID_COLOR_BLACK + ii;
132  wxMemoryDC iconDC;
133  wxBitmap ButtBitmap( w, h );
134  wxBrush brush;
135 
136  iconDC.SelectObject( ButtBitmap );
137 
138  COLOR4D buttcolor = COLOR4D( g_ColorRefs[ii].m_Numcolor );
139 
140  iconDC.SetPen( *wxBLACK_PEN );
141  brush.SetColour( buttcolor.ToColour() );
142  brush.SetStyle( wxBRUSHSTYLE_SOLID );
143 
144  iconDC.SetBrush( brush );
145  iconDC.SetBackground( *wxGREY_BRUSH );
146  iconDC.Clear();
147  iconDC.DrawRoundedRectangle( 0, 0, w, h, (double) h / 3 );
148 
149  wxBitmapButton* bitmapButton = new wxBitmapButton( this, butt_ID, ButtBitmap,
150  wxDefaultPosition, wxSize( w+8, h+6 ) );
151  FlexColumnBoxSizer->Add( bitmapButton, 0,
152  wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL |
153  wxLEFT | wxBOTTOM, 5 );
154 
155  // Set focus to this button if its color matches the
156  // color which had been selected previously (for
157  // whichever layer's color is currently being edited).
158  if( m_color == buttcolor )
159  focusedButton = bitmapButton;
160 
161  EDA_COLOR_T edaColor = ColorFindNearest( buttcolor.ToColour() );
162  wxStaticText* label = new wxStaticText( this, -1, wxGetTranslation( ColorGetName( edaColor ) ),
163  wxDefaultPosition, wxDefaultSize, 0 );
164  FlexColumnBoxSizer->Add( label, 1,
165  wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL |
166  wxLEFT | wxRIGHT | wxBOTTOM, 5 );
167  }
168 
169  // Provide a Cancel button as well, so that this dialog
170  // box can also be canceled by pressing the Esc key
171  // (and also provide a horizontal static line to separate
172  // that button from all of the other buttons).
173 
174  wxStaticLine* sline = new wxStaticLine( this, -1, wxDefaultPosition,
175  wxDefaultSize, wxLI_HORIZONTAL );
176  OuterBoxSizer->Add( sline, 0, wxGROW | wxLEFT | wxRIGHT | wxTOP, 5 );
177 
178  wxStdDialogButtonSizer* stdDialogButtonSizer = new wxStdDialogButtonSizer;
179  OuterBoxSizer->Add( stdDialogButtonSizer, 0, wxGROW | wxALL, 10 );
180 
181  wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ),
182  wxDefaultPosition, wxDefaultSize, 0 );
183  stdDialogButtonSizer->AddButton( cancelButton );
184 
185  stdDialogButtonSizer->Realize();
186 
187  // Set focus to the Cancel button if the currently selected color
188  // does not match any of the colors provided by this dialog box.
189  // (That shouldn't ever happen in practice though.)
190  if( focusedButton )
191  focusedButton->SetFocus();
192  else
193  cancelButton->SetFocus();
194 }
195 
196 
197 void CHOOSE_COLOR_DLG::selColor( wxCommandEvent& event )
198 {
199  int id = event.GetId();
200  m_color = EDA_COLOR_T( id - ID_COLOR_BLACK );
201 
202  // Close the dialog by calling the default dialog handler for a wxID_OK event
203  event.SetId( wxID_OK );
204  event.Skip();
205 }
COLOR4D aOldColor
Definition: selcolor.cpp:73
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:53
void selColor(wxCommandEvent &event)
Definition: selcolor.cpp:197
CHOOSE_COLOR_DLG(wxWindow *aParent, COLOR4D aOldColor)
Definition: selcolor.cpp:85
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:98
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:59
The common library.
colors_id
Definition: selcolor.cpp:42
Number of colors.
Definition: colors.h:75
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39