1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2021 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 
24 #ifndef LISTBOXES_H
25 #define LISTBOXES_H
26 
27 #include <wx/listctrl.h>
28 #include <footprint_filter.h>
29 
30 /*  Forward declarations of all top-level window classes. */
31 class CVPCB_MAINFRAME;
32 class COMPONENT;
33 class FOOTPRINT_LIST;
34 
35 #define LISTBOX_STYLE     ( wxBORDER_NONE | wxLC_NO_HEADER | wxLC_REPORT | wxLC_VIRTUAL | \
36                             wxVSCROLL | wxHSCROLL )
37 
38 /**
39  * Base class to display symbol and footprint lists.
40  */
41 class ITEMS_LISTBOX_BASE : public wxListView
42 {
43 public:
44     ITEMS_LISTBOX_BASE( CVPCB_MAINFRAME* aParent, wxWindowID aId,
45                         const wxPoint& aLocation = wxDefaultPosition,
46                         const wxSize& aSize = wxDefaultSize, long aStyle = 0 );
47 
48     ~ITEMS_LISTBOX_BASE();
49 
50     /**
51      * @return the index of the selected item in lists allowing only one item selected
52      * and the index of the first selected item in lists allowing many selection
53      */
54     int GetSelection();
55 
56     /**
57      * Remove all selection in lists which can have more than one item selected.
58      */
59     void DeselectAll();
60 
61     virtual CVPCB_MAINFRAME* GetParent() const;
62 
63     /**
64      * Update the width of the column based on its contents.
65      *
66      * @param aLine is the line to calculate the width from. If positive, the
67      * width will only be increased if needed. If negative, we start from
68      * scratch and all lines are considered, i.e., the column may be shrunk.
69      */
70     void UpdateWidth( int aLine = -1 );
71 
72 private:
73     /**
74      * Calculate the width of the given line, and increase the column width
75      * if needed. This is effectively the wxListCtrl code for autosizing.
76      * NB. it relies on the caller checking the given line number is valid.
77      */
78     void UpdateLineWidth( unsigned aLine );
79 
80     int columnWidth;
81 };
82 
83 
84 class FOOTPRINTS_LISTBOX : public ITEMS_LISTBOX_BASE
85 {
86 public:
87     /**
88      * Filter setting constants. The filter type is a bitwise OR of these flags,
89      * and only footprints matching all selected filter types are shown.
90      */
91     enum FP_FILTER_T: int
92     {
93         UNFILTERED_FP_LIST                = 0,
94         FILTERING_BY_COMPONENT_FP_FILTERS = 0x0001,
95         FILTERING_BY_PIN_COUNT            = 0x0002,
96         FILTERING_BY_LIBRARY              = 0x0004
97     };
98 
99     FOOTPRINTS_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id );
100     ~FOOTPRINTS_LISTBOX();
101 
102     int      GetCount();
103     void     SetSelection( int index, bool State = true );
104     void     SetSelectedFootprint( const LIB_ID& aFPID );
105     void     SetString( unsigned linecount, const wxString& text );
106     void     AppendLine( const wxString& text );
107 
108     /**
109      * Populate the wxListCtrl with the footprints from \a aList that meet the filter
110      * criteria defined by \a aFilterType.
111      *
112      * @param aList is a #FOOTPRINT_LIST item containing the footprints.
113      * @param aLibName is wxString containing the name of the selected library.  Can be
114      *                 wxEmptyString.
115      * @param aComponent is the #COMPONENT used by the filtering criteria.  Can be NULL.
116      * @param aFootPrintFilterPattern is the filter used to filter list by names.
117      * @param aFilterType defines the criteria to filter \a aList.
118      */
119     void     SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName, COMPONENT* aComponent,
120                             const wxString& aFootPrintFilterPattern, int aFilterType );
121 
122     wxString GetSelectedFootprint();
123 
124     /**
125      * This overloaded function MUST be provided for the wxLC_VIRTUAL mode
126      * because real data is not handled by ITEMS_LISTBOX_BASE.
127      */
128     wxString OnGetItemText( long item, long column ) const override;
129 
130     // Events functions:
131     void     OnLeftClick( wxListEvent& event );
132     void     OnLeftDClick( wxListEvent& event );
133     void     OnChar( wxKeyEvent& event );
134 
135     DECLARE_EVENT_TABLE();
136 
137 private:
138     wxArrayString  m_footprintList;
139 };
140 
141 
142 class LIBRARY_LISTBOX : public ITEMS_LISTBOX_BASE
143 {
144 public:
145     LIBRARY_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id );
146     ~LIBRARY_LISTBOX();
147 
148     int      GetCount();
149     void     SetSelection( int index, bool State = true );
150     void     SetString( unsigned linecount, const wxString& text );
151     void     AppendLine( const wxString& text );
152     void     SetLibraryList( const wxArrayString& aList );
153 
154     wxString GetSelectedLibrary();
155     wxString OnGetItemText( long item, long column ) const override;
156 
157     void     OnSelectLibrary( wxListEvent& event );
158 
159     /**
160      * Called on a key press.
161      *
162      * Call default handler for some special keys, and for "ASCII" keys, select the first
163      * footprint that the name starts by the letter.
164      *
165      * This is the default behavior of a listbox, but because we use virtual lists, the
166      * listbox does not know anything to what is displayed, we must handle this behavior
167      * here.  Furthermore the footprint name is not at the beginning of displayed lines
168      * (the first word is the line number).
169      */
170     void     OnChar( wxKeyEvent& event );
171 
172     DECLARE_EVENT_TABLE();
173 
174 private:
175     wxArrayString  m_libraryList;
176 };
177 
178 
179 class SYMBOLS_LISTBOX : public ITEMS_LISTBOX_BASE
180 {
181 public:
182     SYMBOLS_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id );
183 
184     ~SYMBOLS_LISTBOX();
185 
186     void     Clear();
187     int      GetCount();
188 
189     /**
190      * This overloaded function MUST be provided for the wxLC_VIRTUAL mode
191      * because real data is not handled by #ITEMS_LISTBOX_BASE.
192      */
193     wxString OnGetItemText( long item, long column ) const override;
194 
195     /*
196      * Enable or disable an item
197      */
198     void     SetSelection( int index, bool State = true );
199     void     SetString( unsigned linecount, const wxString& text );
200     void     AppendLine( const wxString& text );
201 
202     // Events functions:
203 
204     /**
205      * Called on a key press.
206      *
207      * Call default handler for some special keys, and for "ASCII" keys, select the first
208      * component that the name starts by the letter.
209      *
210      * This is the default behavior of a listbox, but because we use virtual lists, the
211      * listbox does not know anything to what is displayed, we must handle this behavior
212      * here.  Furthermore the reference of components is not at the beginning of displayed
213      * lines (the first word is the line number).
214      */
215     void     OnChar( wxKeyEvent& event );
216 
217     void     OnSelectComponent( wxListEvent& event );
218 
219     DECLARE_EVENT_TABLE();
220 
221 public:
222     wxArrayString      m_SymbolList;
223 };
224 
225 
226 #endif  //#ifndef LISTBOXES_H
227