1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004-2021 KiCad Developers, see change_log.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 #include <bitmaps.h>
25 #include <cstddef>
26 #include <core/arraydim.h>
27 #include <lib_pin.h>
28 #include <pin_type.h>
29 
30 
31 // These are true singletons so it's OK for them to be globals.
32 
33 static std::vector<BITMAPS> g_typeIcons;
34 static wxArrayString        g_typeNames;
35 
36 static std::vector<BITMAPS> g_shapeIcons;
37 static wxArrayString        g_shapeNames;
38 
39 static std::vector<BITMAPS> g_orientationIcons;
40 static wxArrayString        g_orientationNames;
41 
42 
43 struct pinTypeStruct
44 {
45     wxString name;
46     BITMAPS  bitmap;
47 };
48 
49 // clang-format off
50 const std::map<ELECTRICAL_PINTYPE, struct pinTypeStruct> pinTypes = {
51     { ELECTRICAL_PINTYPE::PT_INPUT,        { _( "Input" ),          BITMAPS::pintype_input } },
52     { ELECTRICAL_PINTYPE::PT_OUTPUT,       { _( "Output" ),         BITMAPS::pintype_output } },
53     { ELECTRICAL_PINTYPE::PT_BIDI,         { _( "Bidirectional" ),  BITMAPS::pintype_bidi } },
54     { ELECTRICAL_PINTYPE::PT_TRISTATE,     { _( "Tri-state" ),      BITMAPS::pintype_3states } },
55     { ELECTRICAL_PINTYPE::PT_PASSIVE,      { _( "Passive" ),        BITMAPS::pintype_passive } },
56     { ELECTRICAL_PINTYPE::PT_NIC,          { _( "Free" ),           BITMAPS::pintype_nic } },
57     { ELECTRICAL_PINTYPE::PT_UNSPECIFIED,  { _( "Unspecified" ),    BITMAPS::pintype_notspecif } },
58     { ELECTRICAL_PINTYPE::PT_POWER_IN,     { _( "Power input" ),    BITMAPS::pintype_powerinput } },
59     { ELECTRICAL_PINTYPE::PT_POWER_OUT,    { _( "Power output" ),   BITMAPS::pintype_poweroutput } },
60     { ELECTRICAL_PINTYPE::PT_OPENCOLLECTOR,{ _( "Open collector" ), BITMAPS::pintype_opencoll } },
61     { ELECTRICAL_PINTYPE::PT_OPENEMITTER,  { _( "Open emitter" ),   BITMAPS::pintype_openemit } },
62     { ELECTRICAL_PINTYPE::PT_NC,           { _( "Unconnected" ),    BITMAPS::pintype_noconnect } },
63 };
64 // clang-format on
65 
66 
67 struct pinShapeStruct
68 {
69     wxString name;
70     BITMAPS  bitmap;
71 };
72 
73 
74 // clang-format off
75 const std::map<GRAPHIC_PINSHAPE, struct pinShapeStruct> pinShapes = {
76     { GRAPHIC_PINSHAPE::LINE,               { _( "Line" ),               BITMAPS::pinshape_normal } },
77     { GRAPHIC_PINSHAPE::INVERTED,           { _( "Inverted" ),           BITMAPS::pinshape_invert } },
78     { GRAPHIC_PINSHAPE::CLOCK,              { _( "Clock" ),              BITMAPS::pinshape_clock_normal } },
79     { GRAPHIC_PINSHAPE::INVERTED_CLOCK,     { _( "Inverted clock" ),     BITMAPS::pinshape_clock_invert } },
80     { GRAPHIC_PINSHAPE::INPUT_LOW,          { _( "Input low" ),          BITMAPS::pinshape_active_low_input } },
81     { GRAPHIC_PINSHAPE::CLOCK_LOW,          { _( "Clock low" ),          BITMAPS::pinshape_clock_active_low } },
82     { GRAPHIC_PINSHAPE::OUTPUT_LOW,         { _( "Output low" ),         BITMAPS::pinshape_active_low_output } },
83     { GRAPHIC_PINSHAPE::FALLING_EDGE_CLOCK, { _( "Falling edge clock" ), BITMAPS::pinshape_clock_fall } },
84     { GRAPHIC_PINSHAPE::NONLOGIC,           { _( "NonLogic" ),           BITMAPS::pinshape_nonlogic } },
85 };
86 // clang-format on
87 
88 
89 static const int pin_orientation_codes[] =
90 {
91     PIN_RIGHT,
92     PIN_LEFT,
93     PIN_UP,
94     PIN_DOWN
95 };
96 
97 
98 // bitmaps to show pins orientations in dialog editor
99 // must have same order than pin_orientation_names
100 static const BITMAPS iconsPinsOrientations[] =
101 {
102     BITMAPS::pinorient_right,
103     BITMAPS::pinorient_left,
104     BITMAPS::pinorient_up,
105     BITMAPS::pinorient_down,
106 };
107 
108 
109 #define PIN_ORIENTATION_CNT arrayDim( pin_orientation_codes )
110 
111 
112 // Helper functions to get the pin orientation name from pin_orientation_codes
113 // Note: the strings are *not* static because they are translated and must be built
114 // on the fly, to be properly translated
115 
PinOrientationName(unsigned aPinOrientationCode)116 wxString PinOrientationName( unsigned aPinOrientationCode )
117 {
118     /* Note: The following name lists are sentence capitalized per the GNOME UI
119      *       standards for list controls.  Please do not change the capitalization
120      *       of these strings unless the GNOME UI standards are changed.
121      */
122     const wxString pin_orientation_names[] =
123     {
124         _( "Right" ),
125         _( "Left" ),
126         _( "Up" ),
127         _( "Down" ),
128         wxT( "???" )
129     };
130 
131     if( aPinOrientationCode > PIN_ORIENTATION_CNT )
132         aPinOrientationCode = PIN_ORIENTATION_CNT;
133 
134     return pin_orientation_names[ aPinOrientationCode ];
135 }
136 
137 
PinOrientationCode(int index)138 int PinOrientationCode( int index )
139 {
140     if( index >= 0 && index < (int) PIN_ORIENTATION_CNT )
141         return pin_orientation_codes[ index ];
142 
143     return PIN_RIGHT;
144 }
145 
146 
PinOrientationIndex(int code)147 int PinOrientationIndex( int code )
148 {
149     size_t i;
150 
151     for( i = 0; i < PIN_ORIENTATION_CNT; i++ )
152     {
153         if( pin_orientation_codes[i] == code )
154             return (int) i;
155     }
156 
157     return wxNOT_FOUND;
158 }
159 
160 
InitTables()161 void InitTables()
162 {
163     for( unsigned i = 0; i < ELECTRICAL_PINTYPES_TOTAL; ++i )
164     {
165         g_typeIcons.push_back( ElectricalPinTypeGetBitmap( static_cast<ELECTRICAL_PINTYPE>( i ) ) );
166         g_typeNames.push_back( ElectricalPinTypeGetText( static_cast<ELECTRICAL_PINTYPE>( i ) ) );
167     }
168 
169     for( unsigned i = 0; i < GRAPHIC_PINSHAPES_TOTAL; ++i )
170     {
171         g_shapeIcons.push_back( PinShapeGetBitmap( static_cast<GRAPHIC_PINSHAPE>( i ) ) );
172         g_shapeNames.push_back( PinShapeGetText( static_cast<GRAPHIC_PINSHAPE>( i ) ) );
173     }
174 
175     for( unsigned i = 0; i < PIN_ORIENTATION_CNT; ++i )
176     {
177         g_orientationIcons.push_back( iconsPinsOrientations[ i ] );
178         g_orientationNames.push_back( PinOrientationName( i ) );
179     }
180 }
181 
182 
PinTypeNames()183 const wxArrayString& PinTypeNames()
184 {
185     if( g_typeNames.empty() )
186         InitTables();
187 
188     return g_typeNames;
189 }
190 
191 
PinTypeIcons()192 const std::vector<BITMAPS>& PinTypeIcons()
193 {
194     if( g_typeIcons.empty() )
195         InitTables();
196 
197     return g_typeIcons;
198 }
199 
200 
PinShapeNames()201 const wxArrayString& PinShapeNames()
202 {
203     if( g_shapeNames.empty() )
204         InitTables();
205 
206     return g_shapeNames;
207 }
208 
209 
PinShapeIcons()210 const std::vector<BITMAPS>& PinShapeIcons()
211 {
212     if( g_shapeIcons.empty() )
213         InitTables();
214 
215     return g_shapeIcons;
216 }
217 
218 
PinOrientationNames()219 const wxArrayString& PinOrientationNames()
220 {
221     if( g_orientationNames.empty() )
222         InitTables();
223 
224     return g_orientationNames;
225 }
226 
227 
PinOrientationIcons()228 const std::vector<BITMAPS>& PinOrientationIcons()
229 {
230     if( g_orientationIcons.empty() )
231         InitTables();
232 
233     return g_orientationIcons;
234 }
235 
236 
ElectricalPinTypeGetText(ELECTRICAL_PINTYPE aType)237 wxString ElectricalPinTypeGetText( ELECTRICAL_PINTYPE aType )
238 {
239     auto findIt = pinTypes.find( aType );
240 
241     wxCHECK_MSG( findIt != pinTypes.end(), wxT( "???" ), "Could not find pin type in lookup map" );
242 
243     return findIt->second.name;
244 }
245 
246 
ElectricalPinTypeGetBitmap(ELECTRICAL_PINTYPE aType)247 BITMAPS ElectricalPinTypeGetBitmap( ELECTRICAL_PINTYPE aType )
248 {
249     auto findIt = pinTypes.find( aType );
250 
251     wxCHECK_MSG( findIt != pinTypes.end(), BITMAPS::INVALID_BITMAP,
252                  "Could not find pin type in lookup map" );
253 
254     return findIt->second.bitmap;
255 }
256 
257 
PinShapeGetText(GRAPHIC_PINSHAPE aShape)258 wxString PinShapeGetText( GRAPHIC_PINSHAPE aShape )
259 {
260     auto findIt = pinShapes.find( aShape );
261 
262     wxCHECK_MSG( findIt != pinShapes.end(), wxT( "?" ), "Could not find pinshape in lookup map" );
263 
264     return findIt->second.name;
265 }
266 
267 
PinShapeGetBitmap(GRAPHIC_PINSHAPE aShape)268 BITMAPS PinShapeGetBitmap( GRAPHIC_PINSHAPE aShape )
269 {
270     auto findIt = pinShapes.find( aShape );
271 
272     wxCHECK_MSG( findIt != pinShapes.end(), BITMAPS::INVALID_BITMAP,
273                  "Could not find pinshape in lookup map" );
274 
275     return findIt->second.bitmap;
276 }
277 
278 
279